From fde3bcaae6887661eb2e2082f052ee9ee0b917fc Mon Sep 17 00:00:00 2001 From: maxluli Date: Tue, 11 Apr 2023 10:44:29 +0200 Subject: [PATCH] Increased OCR performances --- OCR_Decode/DriverData.cs | 90 ++++++++++++++++++ OCR_Decode/DriverDrsWindow.cs | 41 ++++---- OCR_Decode/DriverGapToLeaderWindow.cs | 9 +- OCR_Decode/DriverLapTimeWindow.cs | 8 +- OCR_Decode/DriverNameWindow.cs | 18 ++-- OCR_Decode/DriverPositionWindow.cs | 27 +++--- OCR_Decode/DriverSector1Window.cs | 9 +- OCR_Decode/DriverSector2Window.cs | 9 +- OCR_Decode/DriverSector3Window.cs | 9 +- OCR_Decode/DriverTyresWindow.cs | 130 +++++++++++--------------- OCR_Decode/OCR_Decode.csproj | 1 + OCR_Decode/Reader.cs | 66 ++----------- OCR_Decode/Window.cs | 15 +-- OCR_Decode/Zone.cs | 32 ++++--- 14 files changed, 270 insertions(+), 194 deletions(-) create mode 100644 OCR_Decode/DriverData.cs diff --git a/OCR_Decode/DriverData.cs b/OCR_Decode/DriverData.cs new file mode 100644 index 0000000..f70cfcd --- /dev/null +++ b/OCR_Decode/DriverData.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OCR_Decode +{ + public class DriverData + { + public bool DRS; //True = Drs is opened + public int GapToLeader; //In ms + public int LapTime; //In ms + public string Name; //Ex: LECLERC + public int Position; //Ex: 1 + public int Sector1; //in ms + public int Sector2; //in ms + public int Sector3; //in ms + public Tyre CurrentTyre;//Ex Soft 11 laps + + public DriverData(bool dRS, int gapToLeader, int lapTime, string name, int position, int sector1, int sector2, int sector3, Tyre tyre) + { + DRS = dRS; + GapToLeader = gapToLeader; + LapTime = lapTime; + Name = name; + Position = position; + Sector1 = sector1; + Sector2 = sector2; + Sector3 = sector3; + CurrentTyre = tyre; + } + public DriverData() + { + DRS = false; + GapToLeader = -1; + LapTime = -1; + Name = "Unknown"; + Position = -1; + Sector1 = -1; + Sector2 = -1; + Sector3 = -1; + CurrentTyre = new Tyre(Tyre.Type.Undefined,-1); + } + public override string ToString() + { + string result = ""; + + //Position + result += "Position : " + Position +Environment.NewLine; + //Gap + result += "Gap to leader : " + Reader.ConvertMsToTime(GapToLeader) + Environment.NewLine; + //LapTime + result += "Lap time : " + Reader.ConvertMsToTime(LapTime) + Environment.NewLine; + //DRS + result += "DRS : " + DRS + Environment.NewLine; + //Tyres + result += "Uses " + CurrentTyre.Coumpound + " tyre " + CurrentTyre.NumberOfLaps + " laps old" + Environment.NewLine; + //Name + result += "Driver name : " + Name +Environment.NewLine; + //Sector 1 + result += "Sector 1 : " + Reader.ConvertMsToTime(Sector1) + Environment.NewLine; + //Sector 1 + result += "Sector 2 : " + Reader.ConvertMsToTime(Sector2) + Environment.NewLine; + //Sector 1 + result += "Sector 3 : " + Reader.ConvertMsToTime(Sector3) + Environment.NewLine; + + return result; + } + } + public struct Tyre + { + public enum Type + { + Soft, + Medium, + Hard, + Inter, + Wet, + Undefined + } + public Type Coumpound; + public int NumberOfLaps; + public Tyre(Type type, int laps) + { + Coumpound = type; + NumberOfLaps = laps; + } + } +} diff --git a/OCR_Decode/DriverDrsWindow.cs b/OCR_Decode/DriverDrsWindow.cs index 7ee6fe3..f4248c4 100644 --- a/OCR_Decode/DriverDrsWindow.cs +++ b/OCR_Decode/DriverDrsWindow.cs @@ -28,7 +28,7 @@ namespace OCR_Decode if (greenValue > EmptyDrsGreenValue + EmptyDrsGreenValue / 100 * 30) result = true; - WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "DRS" + result + ".png"); + //WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "DRS" + result + ".png"); return result; } @@ -68,28 +68,31 @@ namespace OCR_Decode } public Rectangle GetBox() { - - var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage)); - Engine.SetVariable("tessedit_char_whitelist", ""); - Page page = Engine.Process(tessImage); - - using (var iter = page.GetIterator()) + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) { - iter.Begin(); - do + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage)); + Engine.SetVariable("tessedit_char_whitelist", ""); + Page page = Engine.Process(tessImage); + + using (var iter = page.GetIterator()) { - Rect boundingBox; - - // Get the bounding box for the current element - if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox)) + iter.Begin(); + do { - page.Dispose(); - return new Rectangle(boundingBox.X1, boundingBox.X2, boundingBox.Width, boundingBox.Height); - } - } while (iter.Next(PageIteratorLevel.Word)); + Rect boundingBox; - page.Dispose(); - return new Rectangle(0, 0, 0, 0); + // Get the bounding box for the current element + if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox)) + { + page.Dispose(); + return new Rectangle(boundingBox.X1, boundingBox.X2, boundingBox.Width, boundingBox.Height); + } + } while (iter.Next(PageIteratorLevel.Word)); + + page.Dispose(); + return new Rectangle(0, 0, 0, 0); + } } } } diff --git a/OCR_Decode/DriverGapToLeaderWindow.cs b/OCR_Decode/DriverGapToLeaderWindow.cs index 802f705..854ee07 100644 --- a/OCR_Decode/DriverGapToLeaderWindow.cs +++ b/OCR_Decode/DriverGapToLeaderWindow.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tesseract; namespace OCR_Decode { @@ -15,8 +16,12 @@ namespace OCR_Decode } public override async Task DecodePng() { - int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Gap); - return result; + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) + { + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Gap,Engine); + return result; + } } } } diff --git a/OCR_Decode/DriverLapTimeWindow.cs b/OCR_Decode/DriverLapTimeWindow.cs index d196fd3..902dc83 100644 --- a/OCR_Decode/DriverLapTimeWindow.cs +++ b/OCR_Decode/DriverLapTimeWindow.cs @@ -16,8 +16,12 @@ namespace OCR_Decode } public override async Task DecodePng() { - int result = await GetTimeFromPng(WindowImage,OcrImage.WindowType.LapTime); - return result; + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) + { + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.LapTime,Engine); + return result; + } } } } diff --git a/OCR_Decode/DriverNameWindow.cs b/OCR_Decode/DriverNameWindow.cs index 1129c2b..4839909 100644 --- a/OCR_Decode/DriverNameWindow.cs +++ b/OCR_Decode/DriverNameWindow.cs @@ -17,15 +17,19 @@ namespace OCR_Decode } public override async Task DecodePng(List DriverList) { - string result = ""; - result = await GetStringFromPng(WindowImage); - - if (!IsADriver(DriverList, result)) + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) { - //I put everything in uppercase to try to lower the chances of bad answers - result = FindClosestMatch(DriverList.ConvertAll(d => d.ToUpper()), result.ToUpper()); + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + string result = ""; + result = await GetStringFromPng(WindowImage,Engine); + + if (!IsADriver(DriverList, result)) + { + //I put everything in uppercase to try to lower the chances of bad answers + result = FindClosestMatch(DriverList.ConvertAll(d => d.ToUpper()), result.ToUpper()); + } + return result; } - return result; } private static bool IsADriver(List drivers, string potentialDriver) { diff --git a/OCR_Decode/DriverPositionWindow.cs b/OCR_Decode/DriverPositionWindow.cs index f797c9c..ec86cde 100644 --- a/OCR_Decode/DriverPositionWindow.cs +++ b/OCR_Decode/DriverPositionWindow.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tesseract; namespace OCR_Decode { @@ -15,19 +16,23 @@ namespace OCR_Decode } public override async Task DecodePng() { - string result = await GetStringFromPng(WindowImage,"0123456789"); - - int position; - try + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) { - position = Convert.ToInt32(result); + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + string result = await GetStringFromPng(WindowImage,Engine, "0123456789"); + + int position; + try + { + position = Convert.ToInt32(result); + } + catch + { + position = -1; + //WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "FailedPosition" + ".png"); + } + return position; } - catch - { - position = -1; - WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "FailedPosition"+".png"); - } - return position; } } } diff --git a/OCR_Decode/DriverSector1Window.cs b/OCR_Decode/DriverSector1Window.cs index 7b8ed2c..2c8c142 100644 --- a/OCR_Decode/DriverSector1Window.cs +++ b/OCR_Decode/DriverSector1Window.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tesseract; namespace OCR_Decode { @@ -15,8 +16,12 @@ namespace OCR_Decode } public override async Task DecodePng() { - int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector); - return result; + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) + { + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + int result = await GetTimeFromPng(WindowImage,OcrImage.WindowType.Sector,Engine); + return result; + } } } } diff --git a/OCR_Decode/DriverSector2Window.cs b/OCR_Decode/DriverSector2Window.cs index c7fc02b..575636a 100644 --- a/OCR_Decode/DriverSector2Window.cs +++ b/OCR_Decode/DriverSector2Window.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tesseract; namespace OCR_Decode { @@ -15,8 +16,12 @@ namespace OCR_Decode } public override async Task DecodePng() { - int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector); - return result; + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) + { + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector,Engine); + return result; + } } } } diff --git a/OCR_Decode/DriverSector3Window.cs b/OCR_Decode/DriverSector3Window.cs index 148bc2d..fa7ea43 100644 --- a/OCR_Decode/DriverSector3Window.cs +++ b/OCR_Decode/DriverSector3Window.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tesseract; namespace OCR_Decode { @@ -15,8 +16,12 @@ namespace OCR_Decode } public override async Task DecodePng() { - int result = await GetTimeFromPng(WindowImage,OcrImage.WindowType.Sector); - return result; + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) + { + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector,Engine); + return result; + } } } } diff --git a/OCR_Decode/DriverTyresWindow.cs b/OCR_Decode/DriverTyresWindow.cs index 37757bb..97125a0 100644 --- a/OCR_Decode/DriverTyresWindow.cs +++ b/OCR_Decode/DriverTyresWindow.cs @@ -12,11 +12,11 @@ namespace OCR_Decode { private static Random rnd = new Random(); - public static Color SOFT_TYRE_COLOR = Color.FromArgb(0xff,0x00,0x00); - public static Color MEDIUM_TYRE_COLOR = Color.FromArgb(0xf5,0xbf,0x00); - public static Color HARD_TYRE_COLOR = Color.FromArgb(0xd9,0xd8,0xd4); - public static Color INTER_TYRE_COLOR = Color.FromArgb(0x00,0xa4,0x2e); - public static Color WET_TYRE_COLOR = Color.FromArgb(0x27,0x60,0xa6); + public static Color SOFT_TYRE_COLOR = Color.FromArgb(0xff, 0x00, 0x00); + public static Color MEDIUM_TYRE_COLOR = Color.FromArgb(0xf5, 0xbf, 0x00); + public static Color HARD_TYRE_COLOR = Color.FromArgb(0xd9, 0xd8, 0xd4); + public static Color INTER_TYRE_COLOR = Color.FromArgb(0x00, 0xa4, 0x2e); + public static Color WET_TYRE_COLOR = Color.FromArgb(0x27, 0x60, 0xa6); public DriverTyresWindow(Bitmap image, Rectangle bounds) : base(image, bounds) { @@ -31,79 +31,82 @@ namespace OCR_Decode { Bitmap tyreZone = GetSmallBitmapFromBigOne(WindowImage, FindTyreZone()); - tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "ZONE" + ".png"); + //tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "ZONE" + ".png"); Tyre.Type type = Tyre.Type.Undefined; int laps = -1; - - //MHIWsmhiw so we can detect Soft Medium Hard Inters and Wet tyres - string text = await GetStringFromPng(tyreZone, "SMHIW", OcrImage.WindowType.Tyre); - if (text.Length == 1 && text != "") + using (var Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default)) { - //We found a tire letter - laps = 0; - text = text.ToUpper(); - switch (text[0]) + Engine.DefaultPageSegMode = PageSegMode.SingleLine; + //MHIWsmhiw so we can detect Soft Medium Hard Inters and Wet tyres + string text = await GetStringFromPng(tyreZone, Engine, "SMHIW", OcrImage.WindowType.Tyre); + if (text.Length == 1 && text != "") { - case 'S': - type = Tyre.Type.Soft; - break; - case 'M': - type = Tyre.Type.Medium; - break; - case 'H': - type = Tyre.Type.Hard; - break; - case 'I': - type = Tyre.Type.Inter; - break; - case 'W': - type = Tyre.Type.Wet; - break; - default: - type = Tyre.Type.Undefined; - break; + //We found a tire letter + laps = 0; + text = text.ToUpper(); + switch (text[0]) + { + case 'S': + type = Tyre.Type.Soft; + break; + case 'M': + type = Tyre.Type.Medium; + break; + case 'H': + type = Tyre.Type.Hard; + break; + case 'I': + type = Tyre.Type.Inter; + break; + case 'W': + type = Tyre.Type.Wet; + break; + default: + type = Tyre.Type.Undefined; + break; + } } - } - else - { - string number = await GetStringFromPng(tyreZone, "0123456789", OcrImage.WindowType.Tyre); - try + else { - laps = Convert.ToInt32(number); + string number = await GetStringFromPng(tyreZone, Engine, "0123456789", OcrImage.WindowType.Tyre); + try + { + laps = Convert.ToInt32(number); + } + catch + { + laps = -1; + } + type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone))); } - catch - { - laps = -1; - } - type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone))); - } - tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png"); - return new Tyre(type,laps); + //tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png"); + return new Tyre(type, laps); + } } private Rectangle FindTyreZone() { Bitmap bmp = WindowImage; int currentPosition = bmp.Width; int height = bmp.Height / 2; - Color limitColor = Color.FromArgb(0x50,0x50,0x50); - Color currentColor = Color.FromArgb(0,0,0); + Color limitColor = Color.FromArgb(0x50, 0x50, 0x50); + Color currentColor = Color.FromArgb(0, 0, 0); - Size newWindowSize = new Size(bmp.Height -Convert.ToInt32((float)bmp.Height / 100f * 25f),bmp.Height - Convert.ToInt32((float)bmp.Height / 100f * 35f)); + Size newWindowSize = new Size(bmp.Height - Convert.ToInt32((float)bmp.Height / 100f * 25f), bmp.Height - Convert.ToInt32((float)bmp.Height / 100f * 35f)); - while(currentColor.R <= limitColor.R && currentColor.G <= limitColor.G && currentColor.B <= limitColor.B && currentPosition > 0) + while (currentColor.R <= limitColor.R && currentColor.G <= limitColor.G && currentColor.B <= limitColor.B && currentPosition > 0) { currentPosition--; - currentColor = bmp.GetPixel(currentPosition,height); + currentColor = bmp.GetPixel(currentPosition, height); } //Its here to let the new window include a little bit of the right - int CorrectedX = currentPosition - (newWindowSize.Width) + Convert.ToInt32((float)newWindowSize.Width/100f*10f); + int CorrectedX = currentPosition - (newWindowSize.Width) + Convert.ToInt32((float)newWindowSize.Width / 100f * 10f); int CorrectedY = Convert.ToInt32((float)newWindowSize.Height / 100f * 35f); if (CorrectedX <= 0) - return new Rectangle(0,0,newWindowSize.Width,newWindowSize.Height); + return new Rectangle(0, 0, newWindowSize.Width, newWindowSize.Height); - return new Rectangle(CorrectedX,CorrectedY,newWindowSize.Width,newWindowSize.Height); + return new Rectangle(CorrectedX, CorrectedY, newWindowSize.Width, newWindowSize.Height); } //This method has been created with the help of chatGPT public Tyre.Type GetTyreTypeFromColor(Color inputColor) @@ -143,23 +146,4 @@ namespace OCR_Decode return type; } } - struct Tyre - { - public enum Type - { - Soft, - Medium, - Hard, - Inter, - Wet, - Undefined - } - public Type Coumpound; - public int NumberOfLaps; - public Tyre(Type type, int laps) - { - Coumpound = type; - NumberOfLaps = laps; - } - } } diff --git a/OCR_Decode/OCR_Decode.csproj b/OCR_Decode/OCR_Decode.csproj index 7eae70c..a4bc684 100644 --- a/OCR_Decode/OCR_Decode.csproj +++ b/OCR_Decode/OCR_Decode.csproj @@ -107,6 +107,7 @@ + diff --git a/OCR_Decode/Reader.cs b/OCR_Decode/Reader.cs index 095ddae..54bae7b 100644 --- a/OCR_Decode/Reader.cs +++ b/OCR_Decode/Reader.cs @@ -174,76 +174,30 @@ namespace OCR_Decode { string result = ""; ChangeImage(idImage); - List> mainResults = new List>(); + List mainResults = new List(); //Decode - for (int mainZoneId = 0; mainZoneId < MainZones.Count;mainZoneId ++) + for (int mainZoneId = 0; mainZoneId < MainZones.Count; mainZoneId++) { switch (mainZoneId) { case 0: //Main Zone - //Parallel.ForEach(MainZones[mainZoneId].Zones, z => foreach (Zone z in MainZones[mainZoneId].Zones) { mainResults.Add(await z.Decode(Drivers)); - }//); + } break; - //Next there will be the title and laps added + //Next there will be the title and laps added } - + } //Display - foreach (List objects in mainResults) + foreach (DriverData driver in mainResults) { - for (int objId = 0; objId < objects.Count; objId++) - { - switch (objId) - { - case 0: - //Position - result += "Position : " + (int)objects[objId] + " "; - break; - case 1: - //Gap - result += "Gap to leader : " + ConvertMsToTime((int)objects[objId]) + " "; - break; - case 2: - //LapTime - result += "Lap time : " + ConvertMsToTime((int)objects[objId]) + " "; - break; - case 3: - //DRS - result += "DRS : " + (bool)objects[objId] + ""; - break; - case 4: - //Tyres - Tyre tyre = (Tyre)objects[objId]; - result += "Uses " + tyre.Coumpound + " tyre for " + tyre.NumberOfLaps + " laps"; - break; - case 5: - //Name - result += "Driver name : " + (string)objects[objId] + " "; - break; - case 6: - //Sector 1 - result += "Sector 1 : " + ConvertMsToTime((int)objects[objId]) + " "; - break; - case 7: - //Sector 1 - result += "Sector 2 : " + ConvertMsToTime((int)objects[objId]) + " "; - break; - case 8: - //Sector 1 - result += "Sector 3 : " + ConvertMsToTime((int)objects[objId]) + " "; - break; - default: - result += "Unknown source : " + objects[objId].ToString(); - break; - } - result += Environment.NewLine; - } + result += driver.ToString(); + result += Environment.NewLine; } return result; @@ -252,7 +206,7 @@ namespace OCR_Decode { //Convert.ToInt32 would round upand I dont want that int minuts = (int)((float)amountOfMs / (1000f * 60f)); - int seconds = (int)((amountOfMs - (minuts*60f*1000f))/1000); + int seconds = (int)((amountOfMs - (minuts * 60f * 1000f)) / 1000); int ms = amountOfMs - ((minuts * 60 * 1000) + (seconds * 1000)); return minuts + ":" + seconds.ToString("00") + ":" + ms.ToString("000"); @@ -279,7 +233,7 @@ namespace OCR_Decode { //string driverFolder = DEBUG_DUMP_FOLDER + "driver" + count + "\\"; //if (!Directory.Exists(driverFolder)) - //Directory.CreateDirectory(driverFolder); + //Directory.CreateDirectory(driverFolder); //zz.ZoneImage.Save(driverFolder + "FullImage.png"); diff --git a/OCR_Decode/Window.cs b/OCR_Decode/Window.cs index 585e92f..08def06 100644 --- a/OCR_Decode/Window.cs +++ b/OCR_Decode/Window.cs @@ -16,7 +16,7 @@ namespace OCR_Decode private Rectangle _bounds; private Bitmap _image; private string _name; - protected static TesseractEngine Engine = null; + //protected static TesseractEngine Engine = null; public Rectangle Bounds { get => _bounds; private set => _bounds = value; } public Bitmap Image { get => _image; set => _image = value; } public string Name { get => _name; protected set => _name = value; } @@ -37,12 +37,13 @@ namespace OCR_Decode { Image = image; Bounds = bounds; - + /* if (Engine == null) { Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default); Engine.DefaultPageSegMode = PageSegMode.SingleLine; } + */ } public virtual async Task DecodePng() { @@ -60,7 +61,7 @@ namespace OCR_Decode return stream.ToArray(); } } - public static async Task GetTimeFromPng(Bitmap wImage,OcrImage.WindowType type) + public static async Task GetTimeFromPng(Bitmap wImage,OcrImage.WindowType type, TesseractEngine Engine) { string rawResult = ""; int result = 0; @@ -107,7 +108,7 @@ namespace OCR_Decode } while (iter.Next(PageIteratorLevel.Word)); } - enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(rawResult, "[^0-9A-Za-z]", "") + ".png"); + //enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(rawResult, "[^0-9A-Za-z]", "") + ".png"); List rawNumbers; @@ -156,7 +157,7 @@ namespace OCR_Decode page.Dispose(); return result; } - public static async Task GetStringFromPng(Bitmap image,string allowedChars = "",OcrImage.WindowType windowType = OcrImage.WindowType.Text) + public static async Task GetStringFromPng(Bitmap image, TesseractEngine Engine, string allowedChars = "",OcrImage.WindowType windowType = OcrImage.WindowType.Text) { string result = ""; @@ -174,6 +175,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"); @@ -182,8 +184,7 @@ namespace OCR_Decode { enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + result +".png"); } - - + */ page.Dispose(); return result; } diff --git a/OCR_Decode/Zone.cs b/OCR_Decode/Zone.cs index 3134e1f..b0263ce 100644 --- a/OCR_Decode/Zone.cs +++ b/OCR_Decode/Zone.cs @@ -62,20 +62,30 @@ namespace OCR_Decode { Windows.Add(window); } - public virtual async Task> Decode(List drivers) + public virtual async Task Decode(List drivers) { - List result = new List(); - foreach (Window w in Windows) + DriverData result = new DriverData(); + Parallel.ForEach(Windows,async w => { if (w is DriverNameWindow) - { - result.Add(await (w as DriverNameWindow).DecodePng(drivers)); - } - else - { - result.Add(await w.DecodePng()); - } - } + result.Name = (string)await (w as DriverNameWindow).DecodePng(drivers); + 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 DriverSector1Window) + result.Sector1 = (int)await (w as DriverSector1Window).DecodePng(); + if (w is DriverSector2Window) + result.Sector2 = (int)await (w as DriverSector2Window).DecodePng(); + if (w is DriverSector3Window) + result.Sector3 = (int)await (w as DriverSector3Window).DecodePng(); + if (w is DriverTyresWindow) + result.CurrentTyre = (Tyre)await (w as DriverTyresWindow).DecodePng(); + }); return result; } }