From 2c1340780e5d4025e950638b7cc5e218ecb3a557 Mon Sep 17 00:00:00 2001 From: maxluli Date: Wed, 10 May 2023 16:00:01 +0200 Subject: [PATCH] Now the OCR applies and only crashes sometimes. But its complete trash and the images cant change for whatever reason --- Test_Merge/F1TVEmulator.cs | 2 +- Test_Merge/Form1.Designer.cs | 26 ++++++++++++++++++++++++ Test_Merge/Form1.cs | 32 +++++++++++++++++++++++++++-- Test_Merge/OcrImage.cs | 7 ++++--- Test_Merge/Reader.cs | 20 ++++++++++++++---- Test_Merge/Window.cs | 39 ++++++++++++++++++++++++++++-------- Test_Merge/Zone.cs | 30 +++++++++++++++++++++++++++ 7 files changed, 138 insertions(+), 18 deletions(-) diff --git a/Test_Merge/F1TVEmulator.cs b/Test_Merge/F1TVEmulator.cs index fc1677c..c656c3e 100644 --- a/Test_Merge/F1TVEmulator.cs +++ b/Test_Merge/F1TVEmulator.cs @@ -273,7 +273,7 @@ namespace Test_Merge try { //Screenshot scrsht = ((ITakesScreenshot)Driver).GetScreenshot(); - //profileriver.SetPreference("layout.css.devPixelsPerPx", "1.0"); + //profileriver.SetPreferencC:\Users\Moi\source\repos\Test_Merge\README.mde("layout.css.devPixelsPerPx", "1.0"); //Screenshot scrsht = Driver.GetFullPageScreenshot(); Screenshot scrsht = Driver.GetScreenshot(); diff --git a/Test_Merge/Form1.Designer.cs b/Test_Merge/Form1.Designer.cs index b4ec23e..3c289cb 100644 --- a/Test_Merge/Form1.Designer.cs +++ b/Test_Merge/Form1.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.btnSettings = new System.Windows.Forms.Button(); + this.tbxResult = new System.Windows.Forms.TextBox(); + this.btnUpdate = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnSettings @@ -41,21 +43,45 @@ this.btnSettings.UseVisualStyleBackColor = true; this.btnSettings.Click += new System.EventHandler(this.btnSettings_Click); // + // tbxResult + // + this.tbxResult.Location = new System.Drawing.Point(12, 71); + this.tbxResult.Multiline = true; + this.tbxResult.Name = "tbxResult"; + this.tbxResult.Size = new System.Drawing.Size(310, 249); + this.tbxResult.TabIndex = 1; + // + // btnUpdate + // + this.btnUpdate.Location = new System.Drawing.Point(120, 12); + this.btnUpdate.Name = "btnUpdate"; + this.btnUpdate.Size = new System.Drawing.Size(102, 53); + this.btnUpdate.TabIndex = 2; + this.btnUpdate.Text = "Update"; + this.btnUpdate.UseVisualStyleBackColor = true; + this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.btnUpdate); + this.Controls.Add(this.tbxResult); this.Controls.Add(this.btnSettings); this.Name = "Form1"; this.Text = "Form1"; + this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); + this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnSettings; + private System.Windows.Forms.TextBox tbxResult; + private System.Windows.Forms.Button btnUpdate; } } diff --git a/Test_Merge/Form1.cs b/Test_Merge/Form1.cs index 16474f0..9852314 100644 --- a/Test_Merge/Form1.cs +++ b/Test_Merge/Form1.cs @@ -11,17 +11,45 @@ using System.Windows.Forms; namespace Test_Merge { public partial class Form1 : Form - { + { + Reader Reader = null; + F1TVEmulator Emulator = null; public Form1() { InitializeComponent(); } - + public async void RefreshUI() + { + if(Emulator != null && Reader != null) + { + Bitmap screen = Emulator.Screenshot(); + screen.Save("SCREEEEEEEEN.png"); + Reader.ChangeImage(screen); + tbxResult.Text = await Reader.Decode(Reader.MainZones,Reader.Drivers,screen); + } + } private void btnSettings_Click(object sender, EventArgs e) { Settings settingsForm = new Settings(); settingsForm.ShowDialog(); MessageBox.Show(settingsForm.GrandPrixUrl + Environment.NewLine + settingsForm.GrandPrixName + Environment.NewLine + settingsForm.GrandPrixYear); } + + private async void Form1_Load(object sender, EventArgs e) + { + string configFile = "./Presets/Clean_2023.json"; + string gpUrl = "https://f1tv.formula1.com/detail/1000006688/2023-azerbaijan-grand-prix?action=play"; + + Emulator = new F1TVEmulator(gpUrl); + await Emulator.Start(); + Reader = new Reader(configFile,Emulator.Screenshot(),true); + } + + private void btnUpdate_Click(object sender, EventArgs e) + { + btnUpdate.Enabled = false; + RefreshUI(); + btnUpdate.Enabled = true; + } } } diff --git a/Test_Merge/OcrImage.cs b/Test_Merge/OcrImage.cs index 7d55e6d..5f31f5d 100644 --- a/Test_Merge/OcrImage.cs +++ b/Test_Merge/OcrImage.cs @@ -48,14 +48,14 @@ namespace Test_Merge { case WindowType.LapTime: outputBitmap = Tresholding(outputBitmap, 185); - outputBitmap = Resize(outputBitmap, 2); + //outputBitmap = Resize(outputBitmap, 2); outputBitmap = Dilatation(outputBitmap, 1); outputBitmap = Erode(outputBitmap, 1); break; case WindowType.Text: outputBitmap = InvertColors(outputBitmap); outputBitmap = Tresholding(outputBitmap, 165); - outputBitmap = Resize(outputBitmap, 2); + //outputBitmap = Resize(outputBitmap, 2); outputBitmap = Dilatation(outputBitmap, 1); break; case WindowType.Tyre: @@ -65,7 +65,8 @@ namespace Test_Merge break; default: outputBitmap = Tresholding(outputBitmap, 165); - outputBitmap = Resize(outputBitmap, 4); + //outputBitmap = Resize(outputBitmap, 4); + outputBitmap = Resize(outputBitmap, 2); outputBitmap = Erode(outputBitmap, 1); break; } diff --git a/Test_Merge/Reader.cs b/Test_Merge/Reader.cs index e806492..13f49b2 100644 --- a/Test_Merge/Reader.cs +++ b/Test_Merge/Reader.cs @@ -34,7 +34,6 @@ namespace Test_Merge { List mainZones = new List(); Bitmap fullImage = image; - List drivers; Zone mainZone; try @@ -56,7 +55,7 @@ namespace Test_Merge Point MainPosition = new Point(mainProperty.GetProperty("x").GetInt32(), mainProperty.GetProperty("y").GetInt32()); Size MainSize = new Size(mainProperty.GetProperty("width").GetInt32(), mainProperty.GetProperty("height").GetInt32()); Rectangle MainRectangle = new Rectangle(MainPosition, MainSize); - mainZone = new Zone(image, MainRectangle,"Main"); + mainZone = new Zone(fullImage, MainRectangle,"Main"); var zones = mainProperty.GetProperty("Zones"); var driverZone = zones[0].GetProperty("DriverZone"); @@ -155,7 +154,7 @@ namespace Test_Merge /// /// The id of the image we are working with /// a string representation of all the returns - public async Task Decode(List mainZones,List drivers) + public async Task Decode(List mainZones,List drivers, Bitmap imageToDecode) { string result = ""; List mainResults = new List(); @@ -167,9 +166,14 @@ namespace Test_Merge { case 0: //Main Zone + int driverID = 0; foreach (Zone z in mainZones[mainZoneId].Zones) { - mainResults.Add(await z.Decode(Drivers)); + driverID++; + if (driverID == 9) + Console.WriteLine("AAAAA"); + z.ZoneImage.Save("PUTAIN_DE_PILOTE_N"+driverID+".png"); + mainResults.Add(await z.Decode(drivers)); } break; //Next there could be a Title Zone and TrackInfoZone @@ -185,6 +189,14 @@ namespace Test_Merge return result; } + + public void ChangeImage(Bitmap Image) + { + foreach (Zone BigZone in MainZones) + { + BigZone.Image = Image; + } + } /// /// Method that can be used to convert an amount of miliseconds into a more readable human form /// diff --git a/Test_Merge/Window.cs b/Test_Merge/Window.cs index fe8a7ca..8210cde 100644 --- a/Test_Merge/Window.cs +++ b/Test_Merge/Window.cs @@ -161,15 +161,38 @@ namespace Test_Merge //ss:ms result = (Convert.ToInt32(rawNumbers[0]) * 1000) + Convert.ToInt32(rawNumbers[1]); - if (result > 999999) + if (result > (60000 + 999)) { - //We know that we have way too much seconds to make a minut - //Its usually because the ":" have been interpreted as a number - int minuts = (int)(rawNumbers[0][0] - '0'); - // rawNumbers[0][1] should contain the : that has been mistaken - int seconds = Convert.ToInt32(rawNumbers[0][2].ToString() + rawNumbers[0][3].ToString()); - int ms = Convert.ToInt32(rawNumbers[1]); - result = (Convert.ToInt32(minuts) * 1000 * 60) + (Convert.ToInt32(seconds) * 1000) + Convert.ToInt32(ms); + if (windowType == OcrImage.WindowType.LapTime) + { + result = 0; + if (rawNumbers[0].Length == 4) + { + result += Convert.ToInt32(rawNumbers[0][0]) * 60000; + result += Convert.ToInt32(rawNumbers[0][2].ToString() + rawNumbers[0][3].ToString()) * 1000; + result += Convert.ToInt32(rawNumbers[1]); + } + else + { + //Fuck it + result = 0; + } + } + if (windowType == OcrImage.WindowType.Sector) + { + int seconds = 0; + if (rawNumbers[0].Length == 3) + { + //We have one char that we need to delete + //For no apparent reason im going to delete the first + seconds = Convert.ToInt32(rawNumbers[0][1].ToString() + rawNumbers[0][2].ToString()); + } + if (rawNumbers[0].Length == 2) { + seconds = Convert.ToInt32(rawNumbers[0][0].ToString() + rawNumbers[0][1].ToString()); + } + int ms = Convert.ToInt32(rawNumbers[1][0].ToString() + rawNumbers[1][1].ToString() + rawNumbers[1][2].ToString()); + result = seconds * 1000 + ms; + } } } else diff --git a/Test_Merge/Zone.cs b/Test_Merge/Zone.cs index d73167a..3d3d1fe 100644 --- a/Test_Merge/Zone.cs +++ b/Test_Merge/Zone.cs @@ -88,6 +88,7 @@ namespace Test_Merge /// A driver data object that contains all the infos about a driver public virtual async Task Decode(List driverList) { + /* int sectorCount = 0; DriverData result = new DriverData(); Parallel.ForEach(Windows, async w => @@ -116,6 +117,35 @@ namespace Test_Merge if (w is DriverTyresWindow) result.CurrentTyre = (Tyre)await (w as DriverTyresWindow).DecodePng(); }); + */ + int sectorCount = 0; + DriverData result = new DriverData(); + foreach(Window w in Windows) + { + // A switch would be prettier but I dont think its supported in this C# version + if (w is DriverNameWindow) + result.Name = (string)await (w as DriverNameWindow).DecodePng(driverList); + if (w is DriverDrsWindow) + result.DRS = (bool)await (w as DriverDrsWindow).DecodePng(); + if (w is DriverGapToLeaderWindow) + result.GapToLeader = (int)await (w as DriverGapToLeaderWindow).DecodePng(); + if (w is DriverLapTimeWindow) + result.LapTime = (int)await (w as DriverLapTimeWindow).DecodePng(); + if (w is DriverPositionWindow) + result.Position = (int)await (w as DriverPositionWindow).DecodePng(); + if (w is DriverSectorWindow) + { + sectorCount++; + if (sectorCount == 1) + result.Sector1 = (int)await (w as DriverSectorWindow).DecodePng(); + if (sectorCount == 2) + result.Sector2 = (int)await (w as DriverSectorWindow).DecodePng(); + if (sectorCount == 3) + result.Sector3 = (int)await (w as DriverSectorWindow).DecodePng(); + } + if (w is DriverTyresWindow) + result.CurrentTyre = (Tyre)await (w as DriverTyresWindow).DecodePng(); + } return result; } public virtual Bitmap Draw()