From 5f8f1f070445714fa4c5d90b7f04fbee217ad385 Mon Sep 17 00:00:00 2001 From: maxluli Date: Thu, 13 Apr 2023 07:30:05 +0200 Subject: [PATCH] Fixed the ridiculus times where the ':' in laptimes was interpreted as a number --- OCR_Decode/DriverTyresWindow.cs | 2 +- OCR_Decode/OcrImage.cs | 36 ++++++++++++++++----------------- OCR_Decode/Window.cs | 19 ++++++++++++++--- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/OCR_Decode/DriverTyresWindow.cs b/OCR_Decode/DriverTyresWindow.cs index fecc544..e73ab6d 100644 --- a/OCR_Decode/DriverTyresWindow.cs +++ b/OCR_Decode/DriverTyresWindow.cs @@ -75,7 +75,7 @@ namespace OCR_Decode { laps = -1; } - type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone))); + type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone,2))); } //tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png"); diff --git a/OCR_Decode/OcrImage.cs b/OCR_Decode/OcrImage.cs index 1c0e723..59d9e92 100644 --- a/OCR_Decode/OcrImage.cs +++ b/OCR_Decode/OcrImage.cs @@ -35,30 +35,24 @@ namespace OCR_Decode { case WindowType.LapTime: result = Tresholding(result, 185); - result = Resize(result); - result = Resize(result); + result = Resize(result,2); result = Dilatation(result,1); result = Erode(result,1); break; case WindowType.Text: - result = Grayscale(result); result = InvertColors(result); result = Tresholding(result, 165); - result = Resize(result); + result = Resize(result,2); result = Dilatation(result, 1); break; case WindowType.Tyre: - //result = RemoveBG(result); result = RemoveUseless(result); - result = Resize(result); - result = Resize(result); - result = Resize(result); + result = Resize(result,4); result = Dilatation(result, 1); break; default: result = Tresholding(result, 165); - result = Resize(result); - result = Resize(result); + result = Resize(result,4); result = Erode(result, 1); break; } @@ -106,10 +100,13 @@ namespace OCR_Decode unsafe { byte* ptr = (byte*)bmpData.Scan0.ToPointer(); - for (int y = 0; y < bmp.Height; y++) + int bmpHeight = bmp.Height; + int bmpWidth = bmp.Width; + Parallel.For(0, bmpHeight, y => + //for (int y = 0; y < bmpHeight; y++) { byte* currentLine = ptr + (y * bmpData.Stride); - for (int x = 0; x < bmp.Width; x++) + for (int x = 0; x < bmpWidth; x++) { byte* pixel = currentLine + (x * bytesPerPixel); @@ -122,7 +119,7 @@ namespace OCR_Decode pixel[0] = pixel[1] = pixel[2] = (byte)value; } - } + }); } bmp.UnlockBits(bmpData); @@ -269,10 +266,13 @@ namespace OCR_Decode unsafe { byte* ptr = (byte*)bmpData.Scan0.ToPointer(); - for (int y = 0; y < bmp.Height; y++) + int bmpHeight = bmp.Height; + int bmpWidth = bmp.Width; + Parallel.For(0,bmpHeight,y=> + //for (int y = 0; y < bmp.Height; y++) { byte* currentLine = ptr + (y * bmpData.Stride); - for (int x = 0; x < bmp.Width; x++) + for (int x = 0; x < bmpWidth; x++) { byte* pixel = currentLine + (x * bytesPerPixel); @@ -290,7 +290,7 @@ namespace OCR_Decode totR += pixel[2]; } } - } + }); } bmp.UnlockBits(bmpData); @@ -329,10 +329,10 @@ namespace OCR_Decode Bitmap output = filter.Apply(input); return output; } - public static Bitmap Resize(Bitmap sourceBitmap) + public static Bitmap Resize(Bitmap sourceBitmap,int amount) { // Create a new Bitmap object to hold the resized image - var resultBitmap = new Bitmap(sourceBitmap.Width * 2, sourceBitmap.Height * 2); + var resultBitmap = new Bitmap(sourceBitmap.Width * amount, sourceBitmap.Height * amount); // Create a Graphics object to draw the resized image using (var graphics = Graphics.FromImage(resultBitmap)) diff --git a/OCR_Decode/Window.cs b/OCR_Decode/Window.cs index a7ce922..221741b 100644 --- a/OCR_Decode/Window.cs +++ b/OCR_Decode/Window.cs @@ -103,8 +103,6 @@ namespace OCR_Decode } while (iter.Next(PageIteratorLevel.Word)); } - enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(rawResult, "[^0-9A-Za-z]", "") + ".png"); - List rawNumbers; //In the gaps we can find '+' but we dont care about it its redondant @@ -127,6 +125,19 @@ namespace OCR_Decode { //ss:ms result = (Convert.ToInt32(rawNumbers[0]) * 1000) + Convert.ToInt32(rawNumbers[1]); + //1239 + //931 + + if (result > 999999) + { + //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); + } } else { @@ -149,6 +160,7 @@ namespace OCR_Decode } } } + enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(rawResult, "[^0-9A-Za-z]", "") + ".png"); page.Dispose(); return result; } @@ -170,7 +182,7 @@ namespace OCR_Decode result += iter.GetText(PageIteratorLevel.Word); } while (iter.Next(PageIteratorLevel.Word)); } - + /* if (allowedChars.Contains("S")) { enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + result +".png"); @@ -179,6 +191,7 @@ namespace OCR_Decode { enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + result +".png"); } + */ page.Dispose(); return result;