From 863ce1922f1de568361a1b7872eb4c6929d34481 Mon Sep 17 00:00:00 2001 From: maxluli Date: Fri, 12 May 2023 08:57:58 +0200 Subject: [PATCH] Optimised and verified the position detection using OCR --- Test_Merge/DriverPositionWindow.cs | 2 +- Test_Merge/OcrImage.cs | 16 +++++++---- Test_Merge/Window.cs | 45 +++++++++++++++++++++++++++--- Test_Merge/Zone.cs | 4 +-- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/Test_Merge/DriverPositionWindow.cs b/Test_Merge/DriverPositionWindow.cs index fb2379f..2b2c49a 100644 --- a/Test_Merge/DriverPositionWindow.cs +++ b/Test_Merge/DriverPositionWindow.cs @@ -24,7 +24,7 @@ namespace Test_Merge /// /// The position of the pilot in int public override async Task DecodePng() - { + { string ocrResult = await GetStringFromPng(WindowImage, Engine, "0123456789"); int position; diff --git a/Test_Merge/OcrImage.cs b/Test_Merge/OcrImage.cs index 5f31f5d..a44235a 100644 --- a/Test_Merge/OcrImage.cs +++ b/Test_Merge/OcrImage.cs @@ -41,22 +41,28 @@ namespace Test_Merge /// /// The type of the window. Depending on it different enhancing features will be applied /// The enhanced Bitmap - public Bitmap Enhance(WindowType type = WindowType.Text) + public Bitmap Enhance(int id,WindowType type = WindowType.Text) { Bitmap outputBitmap = (Bitmap)InputBitmap.Clone(); switch (type) { case WindowType.LapTime: + outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\raw_" + id + ".png"); + outputBitmap = Tresholding(outputBitmap, 185); - //outputBitmap = Resize(outputBitmap, 2); + outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\treshold_" + id + ".png"); + outputBitmap = Dilatation(outputBitmap, 1); + outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\dilatation_" + id + ".png"); + outputBitmap = Erode(outputBitmap, 1); + outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\Final_erode_" + id + ".png"); break; case WindowType.Text: - outputBitmap = InvertColors(outputBitmap); + outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\raw_" + id + ".png"); + outputBitmap = Tresholding(outputBitmap, 165); - //outputBitmap = Resize(outputBitmap, 2); - outputBitmap = Dilatation(outputBitmap, 1); + outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\Final_treshold_" + id + ".png"); break; case WindowType.Tyre: outputBitmap = RemoveUseless(outputBitmap); diff --git a/Test_Merge/Window.cs b/Test_Merge/Window.cs index f04af20..1e7684a 100644 --- a/Test_Merge/Window.cs +++ b/Test_Merge/Window.cs @@ -19,6 +19,16 @@ namespace Test_Merge { public class Window { + + public const string STRING_DEBUG_FOLDER = "./GetString"; + public const string LAPTIME_DEBUG_FOLDER = "./LapTime"; + public const string GAPTOLEADER_DEBUG_FOLDER = "./Gap"; + public const string SECTOR1_DEBUG_FOLDER = "./Sector1"; + public const string SECTOR2_DEBUG_FOLDER = "./Sector2"; + public const string SECTOR3_DEBUG_FOLDER = "./Sector3"; + public const string DRS_DEBUG_FOLDER = "./DRS"; + public const string TYRE_DEBUG_FOLDER = "./Tyre"; + private Rectangle _bounds; private Bitmap _image; private string _name; @@ -28,6 +38,8 @@ namespace Test_Merge public string Name { get => _name; protected set => _name = value; } //This will have to be changed if you want to make it run on your machine public static DirectoryInfo TESS_DATA_FOLDER = new DirectoryInfo(@"C:\Users\Moi\Pictures\SeleniumScreens\TessData"); + //Debug + public static Random rnd = new Random(); public Bitmap WindowImage { @@ -49,6 +61,24 @@ namespace Test_Merge Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default); Engine.DefaultPageSegMode = PageSegMode.SingleLine; } + + //DEBUG + if (!Directory.Exists(STRING_DEBUG_FOLDER)) + Directory.CreateDirectory(STRING_DEBUG_FOLDER); + if (!Directory.Exists(LAPTIME_DEBUG_FOLDER)) + Directory.CreateDirectory(LAPTIME_DEBUG_FOLDER); + if (!Directory.Exists(GAPTOLEADER_DEBUG_FOLDER)) + Directory.CreateDirectory(GAPTOLEADER_DEBUG_FOLDER); + if (!Directory.Exists(SECTOR1_DEBUG_FOLDER)) + Directory.CreateDirectory(SECTOR1_DEBUG_FOLDER); + if (!Directory.Exists(SECTOR2_DEBUG_FOLDER)) + Directory.CreateDirectory(SECTOR2_DEBUG_FOLDER); + if (!Directory.Exists(SECTOR3_DEBUG_FOLDER)) + Directory.CreateDirectory(SECTOR3_DEBUG_FOLDER); + if (!Directory.Exists(DRS_DEBUG_FOLDER)) + Directory.CreateDirectory(DRS_DEBUG_FOLDER); + if (!Directory.Exists(TYRE_DEBUG_FOLDER)) + Directory.CreateDirectory(TYRE_DEBUG_FOLDER); } /// /// Method that will have to be used by the childrens to let the model make them decode the images they have @@ -93,6 +123,9 @@ namespace Test_Merge string rawResult = ""; int result = 0; + //Debug + int salt = rnd.Next(0,999999); + switch (windowType) { case OcrImage.WindowType.Sector: @@ -113,7 +146,7 @@ namespace Test_Merge } - Bitmap enhancedImage = new OcrImage(image).Enhance(windowType); + Bitmap enhancedImage = new OcrImage(image).Enhance(salt,windowType); var tessImage = Pix.LoadFromMemory(ImageToByte(enhancedImage)); @@ -184,7 +217,7 @@ namespace Test_Merge { //A lap time is fucked Console.Write("Fuck"); - } + } } if (windowType == OcrImage.WindowType.Sector) { @@ -195,7 +228,8 @@ namespace Test_Merge //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) { + 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()); @@ -295,10 +329,13 @@ namespace Test_Merge { string result = ""; + //Debug + int salt = rnd.Next(0, 999999); + Engine.SetVariable("tessedit_char_whitelist", allowedChars); Bitmap rawData = image; - Bitmap enhancedImage = new OcrImage(rawData).Enhance(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 a680f17..5ff83cf 100644 --- a/Test_Merge/Zone.cs +++ b/Test_Merge/Zone.cs @@ -106,8 +106,8 @@ namespace Test_Merge //result.LapTime = (int)await (w as DriverLapTimeWindow).DecodePng(); result.LapTime = 0; if (w is DriverPositionWindow) - //result.Position = (int)await (w as DriverPositionWindow).DecodePng(); - result.Position = 0; + result.Position = (int)await (w as DriverPositionWindow).DecodePng(); + //result.Position = 0; if (w is DriverSectorWindow) { sectorCount++;