From d722015cd10336a45136c7303afbfabc833902f9 Mon Sep 17 00:00:00 2001 From: maxluli Date: Fri, 12 May 2023 13:26:38 +0200 Subject: [PATCH] Reworked the GapToLeader metrics --- Test_Merge/OcrImage.cs | 19 +++++- Test_Merge/Window.cs | 152 +++++++++++++++++++++++++++++++++++------ Test_Merge/Zone.cs | 10 +-- 3 files changed, 150 insertions(+), 31 deletions(-) diff --git a/Test_Merge/OcrImage.cs b/Test_Merge/OcrImage.cs index 4b71b2e..f0da6ac 100644 --- a/Test_Merge/OcrImage.cs +++ b/Test_Merge/OcrImage.cs @@ -46,6 +46,18 @@ namespace Test_Merge Bitmap outputBitmap = (Bitmap)InputBitmap.Clone(); switch (type) { + case WindowType.Gap: + outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\raw_" + id + ".png"); + + outputBitmap = Tresholding(outputBitmap, 165); + outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\treshold_" + id + ".png"); + + outputBitmap = Resize(outputBitmap, 2); + outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\resize_" + id + ".png"); + + outputBitmap = Dilatation(outputBitmap, 1); + outputBitmap.Save(Window.GAPTOLEADER_DEBUG_FOLDER + @"\Final_dilatation_" + id + ".png"); + break; case WindowType.Sector: outputBitmap.Save(Window.SECTOR1_DEBUG_FOLDER + @"\raw_" + id + ".png"); @@ -71,13 +83,16 @@ namespace Test_Merge outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\Final_treshold_" + id + ".png"); break; case WindowType.Tyre: + outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\raw_" + id + ".png"); + outputBitmap = RemoveUseless(outputBitmap); - outputBitmap = Resize(outputBitmap, 4); + outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\uselessRemoved_" + id + ".png"); + outputBitmap = Dilatation(outputBitmap, 1); + outputBitmap.Save(Window.TYRE_DEBUG_FOLDER + @"\Final_dilatation_" + id + ".png"); break; default: outputBitmap = Tresholding(outputBitmap, 165); - //outputBitmap = Resize(outputBitmap, 4); outputBitmap = Resize(outputBitmap, 2); outputBitmap = Erode(outputBitmap, 1); break; diff --git a/Test_Merge/Window.cs b/Test_Merge/Window.cs index 5e36f71..5079649 100644 --- a/Test_Merge/Window.cs +++ b/Test_Merge/Window.cs @@ -124,7 +124,7 @@ namespace Test_Merge int result = 0; //Debug - int salt = rnd.Next(0,999999); + int salt = rnd.Next(0, 999999); switch (windowType) { @@ -146,7 +146,7 @@ namespace Test_Merge } - Bitmap enhancedImage = new OcrImage(image).Enhance(salt,windowType); + Bitmap enhancedImage = new OcrImage(image).Enhance(salt, windowType); var tessImage = Pix.LoadFromMemory(ImageToByte(enhancedImage)); @@ -194,8 +194,8 @@ namespace Test_Merge //The perect case try { - seconds = Convert.ToInt32(rawNumbers[0]); - miliseconds = Convert.ToInt32(rawNumbers[1]); + seconds = Convert.ToInt32(rawNumbers[0].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[1].ToString()); } catch { @@ -204,7 +204,7 @@ namespace Test_Merge } else { - if(rawNumbers.Count == 1) + if (rawNumbers.Count == 1) { //Here it is a little harder... Usually its because a '.' has been overlooked or interpreted as a number @@ -260,9 +260,9 @@ namespace Test_Merge //The normal way try { - minuts = Convert.ToInt32(rawNumbers[0]); - seconds = Convert.ToInt32(rawNumbers[1]); - miliseconds = Convert.ToInt32(rawNumbers[2]); + minuts = Convert.ToInt32(rawNumbers[0].ToString()); + seconds = Convert.ToInt32(rawNumbers[1].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[2].ToString()); } catch { @@ -290,7 +290,7 @@ namespace Test_Merge { Console.WriteLine("Lap time convertion failed"); } - + } else { @@ -321,7 +321,7 @@ namespace Test_Merge if (rawNumbers[1].Length == 5) { //It has been forgotten - minuts = Convert.ToInt32(rawNumbers[0]); + minuts = Convert.ToInt32(rawNumbers[0].ToString()); seconds = Convert.ToInt32(rawNumbers[1][0].ToString() + rawNumbers[1][1].ToString()); miliseconds = Convert.ToInt32(rawNumbers[1][2].ToString() + rawNumbers[1][3].ToString() + rawNumbers[1][4].ToString()); } @@ -343,7 +343,7 @@ namespace Test_Merge } else { - if(rawNumbers.Count == 1) + if (rawNumbers.Count == 1) { //Both the '.' and the ':' have been missinterpreted if (rawNumbers[0].Length == 6) @@ -362,7 +362,7 @@ namespace Test_Merge } else { - if(rawNumbers[0].Length == 7) + if (rawNumbers[0].Length == 7) { //The '.' or ':' have been interpreted as a number (usually the ':') try @@ -408,23 +408,133 @@ namespace Test_Merge } } - if(minuts > 9) - { - Console.Write("Euuuuuh chelou la non?"); - } - result = 0; result += minuts * 60 * 1000; result += seconds * 1000; result += miliseconds; break; case OcrImage.WindowType.Gap: - + if (rawNumbers.Count == 2) + { + // This should be the x.xxx or a missed x:xx.xxx + if (rawNumbers[0].Length > 2) + { + //Its a missed x:xx.xxx + if (rawNumbers[0].Length == 3) + { + //It forgot the ":" + try + { + minuts = Convert.ToInt32(rawNumbers[0][0].ToString()); + seconds = Convert.ToInt32(rawNumbers[0][1].ToString() + rawNumbers[0][2].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[1]); + } + catch + { + Console.WriteLine("Gap to leader convertion failed"); + } + } + else + { + //The ":" has been mistaken as a number + if (rawNumbers[0].Length == 4) + { + try + { + minuts = Convert.ToInt32(rawNumbers[0][0].ToString()); + seconds = Convert.ToInt32(rawNumbers[0][2].ToString() + rawNumbers[0][3].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[1]); + } + catch + { + Console.WriteLine("Gap to leader convertion failed"); + } + } + else + { + Console.WriteLine("Gap to leader convertion failed"); + } + } + + } + else + { + //It should be a normal x.xxx or xx.xxx + try + { + seconds = Convert.ToInt32(rawNumbers[0].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[1].ToString()); + } + catch + { + Console.WriteLine("Gap to leader convertion failed"); + } + } + } + else + { + if (rawNumbers.Count == 1) + { + //can be anything depending on the size of the string + if (rawNumbers[0].Length == 4) + { + //We just missed the '.' + try + { + seconds = Convert.ToInt32(rawNumbers[0][0].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[0][1].ToString() + rawNumbers[0][2].ToString() + rawNumbers[0][3].ToString()); + } + catch + { + Console.WriteLine("Gap to leader convertion failed"); + } + } + else + { + if (rawNumbers[0].Length == 5) + { + //We just missed the '.' + try + { + seconds = Convert.ToInt32(rawNumbers[0][0].ToString() + rawNumbers[0][1].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[0][2].ToString() + rawNumbers[0][3].ToString() + rawNumbers[0][4].ToString()); + } + catch + { + Console.WriteLine("Gap to leader convertion failed"); + } + } + //There is just too much possibilities that it would be stupid to try and tell them appart so for now im leaving that as just an error + Console.WriteLine("Gap to leader convertion failed"); + } + } + else + { + if (rawNumbers.Count == 3) + { + // This should be the x:xx.xxx + try + { + minuts = Convert.ToInt32(rawNumbers[0].ToString()); + seconds = Convert.ToInt32(rawNumbers[1].ToString()); + miliseconds = Convert.ToInt32(rawNumbers[2].ToString()); + } + catch + { + Console.WriteLine("Gap to leader convertion failed"); + } + } + } + } + result = 0; + result += minuts * 60 * 1000; + result += seconds * 1000; + result += miliseconds; break; default: try { - result = Convert.ToInt32(rawNumbers[0]); + result = Convert.ToInt32(rawNumbers[0].ToString()); } catch { @@ -432,7 +542,7 @@ namespace Test_Merge } break; } - + page.Dispose(); return result; } @@ -454,7 +564,7 @@ namespace Test_Merge Engine.SetVariable("tessedit_char_whitelist", allowedChars); Bitmap rawData = image; - Bitmap enhancedImage = new OcrImage(rawData).Enhance(salt,windowType); + Bitmap enhancedImage = new OcrImage(rawData).Enhance(salt, windowType); Page page = Engine.Process(enhancedImage); using (var iter = page.GetIterator()) diff --git a/Test_Merge/Zone.cs b/Test_Merge/Zone.cs index 464fd1b..150d2c3 100644 --- a/Test_Merge/Zone.cs +++ b/Test_Merge/Zone.cs @@ -94,16 +94,13 @@ namespace Test_Merge { // 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); - result.Name = "Unknown"; + 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(); - result.GapToLeader = 0; + result.GapToLeader = (int)await (w as DriverGapToLeaderWindow).DecodePng(); if (w is DriverLapTimeWindow) result.LapTime = (int)await (w as DriverLapTimeWindow).DecodePng(); - //result.LapTime = 0; if (w is DriverPositionWindow) result.Position = (int)await (w as DriverPositionWindow).DecodePng(); if (w is DriverSectorWindow) @@ -111,13 +108,10 @@ namespace Test_Merge sectorCount++; if (sectorCount == 1) result.Sector1 = (int)await (w as DriverSectorWindow).DecodePng(); - //result.Sector1 = 0; if (sectorCount == 2) result.Sector2 = (int)await (w as DriverSectorWindow).DecodePng(); - //result.Sector2 = 0; if (sectorCount == 3) result.Sector3 = (int)await (w as DriverSectorWindow).DecodePng(); - //result.Sector3 = 0; } if (w is DriverTyresWindow) //result.CurrentTyre = (Tyre)await (w as DriverTyresWindow).DecodePng();