From 5ca6d61a695ffb18c1d686857f603458301db741 Mon Sep 17 00:00:00 2001 From: maxluli Date: Mon, 3 Apr 2023 12:38:47 +0200 Subject: [PATCH] Improved the old way of manually selecting windows, Added all the windows types and implemented them, You can now export to a bad json file --- OCR_tester/DriverDrsWindow.cs | 17 ++++++ OCR_tester/DriverGapToLeaderWindow.cs | 18 ++++++ OCR_tester/DriverLapTimeWindow.cs | 17 ++++++ OCR_tester/DriverNameWindow.cs | 17 ++++++ OCR_tester/DriverPositionWindow.cs | 17 ++++++ OCR_tester/DriverSector1Window.cs | 17 ++++++ OCR_tester/DriverSector2Window.cs | 17 ++++++ OCR_tester/DriverSector3Window.cs | 17 ++++++ OCR_tester/DriverTyresWindow.cs | 17 ++++++ OCR_tester/DriverZone.cs | 87 +++++++++++++++++++++++++++ OCR_tester/Form1.Designer.cs | 25 ++++++-- OCR_tester/Form1.cs | 20 +++++- OCR_tester/MainZone.cs | 7 ++- OCR_tester/OCR_tester.csproj | 10 +++ OCR_tester/Window.cs | 18 +++++- OCR_tester/Zone.cs | 61 ++++++++++++++++++- 16 files changed, 369 insertions(+), 13 deletions(-) create mode 100644 OCR_tester/DriverDrsWindow.cs create mode 100644 OCR_tester/DriverGapToLeaderWindow.cs create mode 100644 OCR_tester/DriverLapTimeWindow.cs create mode 100644 OCR_tester/DriverNameWindow.cs create mode 100644 OCR_tester/DriverPositionWindow.cs create mode 100644 OCR_tester/DriverSector1Window.cs create mode 100644 OCR_tester/DriverSector2Window.cs create mode 100644 OCR_tester/DriverSector3Window.cs create mode 100644 OCR_tester/DriverTyresWindow.cs create mode 100644 OCR_tester/DriverZone.cs diff --git a/OCR_tester/DriverDrsWindow.cs b/OCR_tester/DriverDrsWindow.cs new file mode 100644 index 0000000..e8f731f --- /dev/null +++ b/OCR_tester/DriverDrsWindow.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverDrsWindow : Window + { + public DriverDrsWindow(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "Drs"; + } + } +} diff --git a/OCR_tester/DriverGapToLeaderWindow.cs b/OCR_tester/DriverGapToLeaderWindow.cs new file mode 100644 index 0000000..ed51974 --- /dev/null +++ b/OCR_tester/DriverGapToLeaderWindow.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverGapToLeaderWindow : Window + { + + public DriverGapToLeaderWindow(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "GapToLeader"; + } + } +} diff --git a/OCR_tester/DriverLapTimeWindow.cs b/OCR_tester/DriverLapTimeWindow.cs new file mode 100644 index 0000000..105aa24 --- /dev/null +++ b/OCR_tester/DriverLapTimeWindow.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverLapTimeWindow : Window + { + public DriverLapTimeWindow(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "LapTime"; + } + } +} diff --git a/OCR_tester/DriverNameWindow.cs b/OCR_tester/DriverNameWindow.cs new file mode 100644 index 0000000..7fa6909 --- /dev/null +++ b/OCR_tester/DriverNameWindow.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverNameWindow : Window + { + public DriverNameWindow(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "Name"; + } + } +} diff --git a/OCR_tester/DriverPositionWindow.cs b/OCR_tester/DriverPositionWindow.cs new file mode 100644 index 0000000..3cf18bc --- /dev/null +++ b/OCR_tester/DriverPositionWindow.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverPositionWindow : Window + { + public DriverPositionWindow(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "Position"; + } + } +} diff --git a/OCR_tester/DriverSector1Window.cs b/OCR_tester/DriverSector1Window.cs new file mode 100644 index 0000000..bdb3412 --- /dev/null +++ b/OCR_tester/DriverSector1Window.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverSector1Window : Window + { + public DriverSector1Window(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "Sector1"; + } + } +} diff --git a/OCR_tester/DriverSector2Window.cs b/OCR_tester/DriverSector2Window.cs new file mode 100644 index 0000000..0ac6a7d --- /dev/null +++ b/OCR_tester/DriverSector2Window.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverSector2Window : Window + { + public DriverSector2Window(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "Sector2"; + } + } +} diff --git a/OCR_tester/DriverSector3Window.cs b/OCR_tester/DriverSector3Window.cs new file mode 100644 index 0000000..8b4445a --- /dev/null +++ b/OCR_tester/DriverSector3Window.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverSector3Window : Window + { + public DriverSector3Window(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "Sector3"; + } + } +} diff --git a/OCR_tester/DriverTyresWindow.cs b/OCR_tester/DriverTyresWindow.cs new file mode 100644 index 0000000..1a6fbc3 --- /dev/null +++ b/OCR_tester/DriverTyresWindow.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + internal class DriverTyresWindow : Window + { + public DriverTyresWindow(Bitmap fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "Tyres"; + } + } +} diff --git a/OCR_tester/DriverZone.cs b/OCR_tester/DriverZone.cs new file mode 100644 index 0000000..3610643 --- /dev/null +++ b/OCR_tester/DriverZone.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_tester +{ + public class DriverZone : Zone + { + public DriverZone(Image fullImage, Rectangle bounds) : base(fullImage, bounds) + { + Name = "DriverZone"; + } + public override void AddWindow(Rectangle bounds) + { + //base.AddWindow(bounds); + + int count = Windows.Count(); + Rectangle newBounds = new Rectangle(bounds.X, 0, bounds.Width, ZoneImage.Height); + switch (count) + { + case 0: + if (Fits(bounds)) + { + Windows.Add(new DriverPositionWindow(ZoneImage, newBounds)); + } + break; + case 1: + if (Fits(bounds)) + { + Windows.Add(new DriverGapToLeaderWindow(ZoneImage, newBounds)); + } + break; + case 2: + if (Fits(bounds)) + { + Windows.Add(new DriverLapTimeWindow(ZoneImage, newBounds)); + } + break; + case 3: + if (Fits(bounds)) + { + Windows.Add(new DriverDrsWindow(ZoneImage, newBounds)); + } + break; + case 4: + if (Fits(bounds)) + { + Windows.Add(new DriverTyresWindow(ZoneImage, newBounds)); + } + break; + case 5: + if (Fits(bounds)) + { + Windows.Add(new DriverNameWindow(ZoneImage, newBounds)); + } + break; + case 6: + if (Fits(bounds)) + { + Windows.Add(new DriverSector1Window(ZoneImage, newBounds)); + } + break; + case 7: + if (Fits(bounds)) + { + Windows.Add(new DriverSector2Window(ZoneImage, newBounds)); + } + break; + case 8: + if (Fits(bounds)) + { + Windows.Add(new DriverSector3Window(ZoneImage, newBounds)); + } + break; + default: + if (Fits(bounds)) + { + Windows.Add(new Window(ZoneImage, newBounds)); + } + break; + } + } + } +} diff --git a/OCR_tester/Form1.Designer.cs b/OCR_tester/Form1.Designer.cs index f8bf10c..b9a4b50 100644 --- a/OCR_tester/Form1.Designer.cs +++ b/OCR_tester/Form1.Designer.cs @@ -36,6 +36,7 @@ this.btnDeleteWindow = new System.Windows.Forms.Button(); this.panel2 = new System.Windows.Forms.Panel(); this.pbxWindow = new System.Windows.Forms.PictureBox(); + this.btnExport = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.pbxInput)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pbxWindow)).BeginInit(); this.SuspendLayout(); @@ -64,12 +65,12 @@ // this.panel1.Location = new System.Drawing.Point(984, 118); this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(268, 154); + this.panel1.Size = new System.Drawing.Size(268, 100); this.panel1.TabIndex = 4; // // btnCreateWindow // - this.btnCreateWindow.Location = new System.Drawing.Point(978, 292); + this.btnCreateWindow.Location = new System.Drawing.Point(978, 224); this.btnCreateWindow.Name = "btnCreateWindow"; this.btnCreateWindow.Size = new System.Drawing.Size(280, 47); this.btnCreateWindow.TabIndex = 5; @@ -89,7 +90,7 @@ // // btnDeleteWindow // - this.btnDeleteWindow.Location = new System.Drawing.Point(978, 345); + this.btnDeleteWindow.Location = new System.Drawing.Point(978, 277); this.btnDeleteWindow.Name = "btnDeleteWindow"; this.btnDeleteWindow.Size = new System.Drawing.Size(280, 47); this.btnDeleteWindow.TabIndex = 7; @@ -98,7 +99,7 @@ // // panel2 // - this.panel2.Location = new System.Drawing.Point(984, 398); + this.panel2.Location = new System.Drawing.Point(984, 330); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(268, 154); this.panel2.TabIndex = 5; @@ -107,17 +108,28 @@ // this.pbxWindow.Location = new System.Drawing.Point(12, 558); this.pbxWindow.Name = "pbxWindow"; - this.pbxWindow.Size = new System.Drawing.Size(960, 42); + this.pbxWindow.Size = new System.Drawing.Size(1240, 59); this.pbxWindow.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.pbxWindow.TabIndex = 8; this.pbxWindow.TabStop = false; this.pbxWindow.Click += new System.EventHandler(this.pbxWindow_Click); // + // btnExport + // + this.btnExport.Location = new System.Drawing.Point(978, 490); + this.btnExport.Name = "btnExport"; + this.btnExport.Size = new System.Drawing.Size(280, 62); + this.btnExport.TabIndex = 9; + this.btnExport.Text = "Export"; + this.btnExport.UseVisualStyleBackColor = true; + this.btnExport.Click += new System.EventHandler(this.btnExport_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1264, 612); + this.ClientSize = new System.Drawing.Size(1264, 623); + this.Controls.Add(this.btnExport); this.Controls.Add(this.pbxWindow); this.Controls.Add(this.panel2); this.Controls.Add(this.btnDeleteWindow); @@ -144,6 +156,7 @@ private System.Windows.Forms.Button btnDeleteWindow; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.PictureBox pbxWindow; + private System.Windows.Forms.Button btnExport; } } diff --git a/OCR_tester/Form1.cs b/OCR_tester/Form1.cs index ea97dce..409f6d9 100644 --- a/OCR_tester/Form1.cs +++ b/OCR_tester/Form1.cs @@ -114,7 +114,7 @@ namespace OCR_tester Size newSize = new Size(zonePoints[1].X - zonePoints[0].X, zonePoints[1].Y - zonePoints[0].Y); mainZone = new MainZone(fullImage, new Rectangle(zonePoints[0], newSize)); pbxInput.Image = mainZone.Draw(); - pbxWindow.Image = mainZone.Zones[0].ZoneImage; + pbxWindow.Image = mainZone.Zones[1].ZoneImage; SwitchCreateZoneButton(); } else @@ -150,7 +150,7 @@ namespace OCR_tester z.AddWindow(new Rectangle(position,newSize)); } pbxInput.Image = mainZone.Draw(); - pbxWindow.Image = mainZone.Zones[0].Draw(); + pbxWindow.Image = mainZone.Zones[1].Draw(); SwitchCreateWindowButton(); } else @@ -164,5 +164,21 @@ namespace OCR_tester { reload(); } + + private void btnExport_Click(object sender, EventArgs e) + { + string path = @"C:\Users\Moi\Desktop\imgDump\"; + string name = "Dump.json"; + if (File.Exists(path + name)) + { + File.Delete(path+name); + } + using (StreamWriter writer = new StreamWriter(path+name)) + { + writer.WriteLine("{"); + writer.WriteLine(mainZone.ToJSON()); + writer.WriteLine("}"); + } + } } } diff --git a/OCR_tester/MainZone.cs b/OCR_tester/MainZone.cs index 658e6f9..7b3640b 100644 --- a/OCR_tester/MainZone.cs +++ b/OCR_tester/MainZone.cs @@ -16,6 +16,7 @@ namespace OCR_tester public MainZone(Image fullImage, Rectangle bounds) : base(fullImage, bounds) { AutoCalibrate(); + Name = "Main"; } public void AutoCalibrate() { @@ -69,7 +70,7 @@ namespace OCR_tester Point windowLocation = new Point(0, (Rectangle.Y + Rectangle.Height / 2) - windowSize.Height / 2); windowRectangle = new Rectangle(windowLocation, windowSize); - Zones.Add(new Zone(ZoneImage, windowRectangle)); + Zones.Add(new DriverZone(ZoneImage, windowRectangle)); } } public override Bitmap Draw() @@ -92,8 +93,8 @@ namespace OCR_tester int count2 = 0; foreach (Window w in z.Windows) { - g.DrawRectangle(Pens.Blue,w.Bounds); - w.WindowImage.Save(dir+"data"+count2+".png"); + g.DrawRectangle(Pens.Blue,new Rectangle(w.Bounds.X,w.Bounds.Y + count * z.Bounds.Height,w.Bounds.Width,w.Bounds.Height)); + w.WindowImage.Save(dir+w.Name+".png"); count2++; } count++; diff --git a/OCR_tester/OCR_tester.csproj b/OCR_tester/OCR_tester.csproj index 24d60cd..7cad6b2 100644 --- a/OCR_tester/OCR_tester.csproj +++ b/OCR_tester/OCR_tester.csproj @@ -51,6 +51,16 @@ + + + + + + + + + + Form diff --git a/OCR_tester/Window.cs b/OCR_tester/Window.cs index c084b94..ff1d195 100644 --- a/OCR_tester/Window.cs +++ b/OCR_tester/Window.cs @@ -10,9 +10,12 @@ namespace OCR_tester { public class Window { - private Bitmap FullImage; + protected Bitmap FullImage; + private string _name; private Rectangle _bounds; + + public string Name { get => _name; protected set => _name = value; } public Rectangle Bounds { get => _bounds; protected set => _bounds = value; } public static DirectoryInfo tessDataFolder = new DirectoryInfo(@"C:\Users\Moi\Pictures\SeleniumScreens\TessData"); @@ -27,6 +30,7 @@ namespace OCR_tester return sample; } } + public Window(Bitmap fullImage, Rectangle bounds) { FullImage = fullImage; @@ -44,5 +48,17 @@ namespace OCR_tester return stream.ToArray(); } } + public virtual string ToJSON() + { + string result = ""; + + result += "\"" + Name + "\"" + ":{" + Environment.NewLine; + result += "\t" + "\"x\":" + Bounds.X + "," + Environment.NewLine; + result += "\t" + "\"y\":" + Bounds.Y + "," + Environment.NewLine; + result += "\t" + "\"width\":" + Bounds.Width + Environment.NewLine; + result += "}"; + + return result; + } } } diff --git a/OCR_tester/Zone.cs b/OCR_tester/Zone.cs index 41e4615..db7f41b 100644 --- a/OCR_tester/Zone.cs +++ b/OCR_tester/Zone.cs @@ -12,6 +12,7 @@ namespace OCR_tester protected Bitmap FullImage; private List _zones; private List _windows; + private string _name; protected Rectangle _bounds; public List Zones { get => _zones; protected set => _zones = value; } @@ -29,6 +30,8 @@ namespace OCR_tester } } + public string Name { get => _name; set => _name = value; } + public Zone(Image fullImage, Rectangle bounds) { FullImage = (Bitmap)fullImage; @@ -50,7 +53,7 @@ namespace OCR_tester if (Fits(bounds)) Zones.Add(new Zone(ZoneImage, bounds)); } - public void AddWindow(Rectangle bounds) + public virtual void AddWindow(Rectangle bounds) { if (Fits(bounds)) Windows.Add(new Window(ZoneImage, bounds)); @@ -81,5 +84,61 @@ namespace OCR_tester } return (Bitmap)img; } + public virtual string ToJSON() + { + string result = ""; + result += "\"" + Name + "\":{" + Environment.NewLine; + result += "\t" + "\"x\":" + Bounds.X + "," + Environment.NewLine; + result += "\t" + "\"y\":" + Bounds.Y + "," + Environment.NewLine; + result += "\t" + "\"width\":" + Bounds.Width + "," + Environment.NewLine; + result += "\t" + "\"height\":" + Bounds.Height; + + if (Windows.Count != 0) + { + result += "," +Environment.NewLine; + + result += "\t" + "\"Windows\":["+Environment.NewLine; + result += "\t\t{" + Environment.NewLine; + int Wcount = 0; + foreach (Window w in Windows) + { + result += "\t\t" + w.ToJSON(); + Wcount++; + if (Wcount != Windows.Count) + result += ","; + } + result += "\t\t}"+Environment.NewLine; + result += "\t" + "]"+Environment.NewLine; + } + else + { + result += Environment.NewLine; + } + if (Zones.Count != 0) + { + result += "," + Environment.NewLine; + + result += "\t" + "\"Zones\":[" + Environment.NewLine; + result += "\t\t{" + Environment.NewLine; + int Zcount = 0; + foreach (Zone z in Zones) + { + result += "\t\t" + z.ToJSON(); + Zcount++; + if (Zcount != Zones.Count) + result += ","; + } + result += "\t\t}" + Environment.NewLine; + result += "\t" + "]" + Environment.NewLine; + } + else + { + result += Environment.NewLine; + } + + result += "}"; + + return result; + } } }