From d9a0a5e7666ecc5c8de9f620f1d9c82ebebf9723 Mon Sep 17 00:00:00 2001 From: maxluli Date: Tue, 4 Apr 2023 17:24:55 +0200 Subject: [PATCH] Added laptime recognition (not precise enough yet tho) --- OCR_Decode/DriverLapTimeWindow.cs | 76 ++++++++++++++++++++++++++++++- OCR_Decode/DriverNameWindow.cs | 34 -------------- OCR_Decode/Form1.Designer.cs | 14 +++--- OCR_Decode/Reader.cs | 5 +- 4 files changed, 85 insertions(+), 44 deletions(-) diff --git a/OCR_Decode/DriverLapTimeWindow.cs b/OCR_Decode/DriverLapTimeWindow.cs index 01e6e68..a3d128e 100644 --- a/OCR_Decode/DriverLapTimeWindow.cs +++ b/OCR_Decode/DriverLapTimeWindow.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tesseract; namespace OCR_Decode { @@ -15,7 +16,80 @@ namespace OCR_Decode } public override object DecodePng() { - return 0; + int result = GetTimeFromPng(); + return result; + } + private int GetTimeFromPng() + { + //returns milliseconds + string rawResult = ""; + int treshold = 185; + int result = 0; + + Bitmap rawData = WindowImage; + rawData = Window.ConvertToBlackAndWhite(rawData, treshold); + + TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default); + var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage)); + + Page page = engine.Process(tessImage); + Graphics g = Graphics.FromImage(rawData); + // Get the iterator for the page layout + using (var iter = page.GetIterator()) + { + // Loop over the elements of the page layout + iter.Begin(); + do + { + Rect boundingBox; + // Get the bounding box for the current element + if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox)) + { + g.DrawRectangle(Pens.Red, new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height)); + } + // Get the text for the current element + try{ + rawResult += iter.GetText(PageIteratorLevel.Word); + } + catch + { + //nothing we just dont add it if its not a number + } + } while (iter.Next(PageIteratorLevel.Word)); + } + + string[] rawNumbers; + + rawNumbers = rawResult.Split('.',':'); + + if (rawNumbers.Length == 3) + { + //mm:ss:ms + result = (Convert.ToInt32(rawNumbers[0]) * 1000 * 60) + (Convert.ToInt32(rawNumbers[1]) * 1000) + Convert.ToInt32(rawNumbers[2]); + } + else + { + if (rawNumbers.Length == 2) + { + //ss:ms + result = (Convert.ToInt32(rawNumbers[0]) * 1000) + Convert.ToInt32(rawNumbers[1]); + } + else + { + if (rawNumbers.Length == 1) + { + //ss + result = Convert.ToInt32(rawNumbers[0]); + } + else + { + //Auuuugh + result = 1111111111; + } + } + } + + return result; } } } diff --git a/OCR_Decode/DriverNameWindow.cs b/OCR_Decode/DriverNameWindow.cs index 2bc6ea1..58d12f9 100644 --- a/OCR_Decode/DriverNameWindow.cs +++ b/OCR_Decode/DriverNameWindow.cs @@ -15,40 +15,6 @@ namespace OCR_Decode { Name = "Name"; } - /* - public override object DecodePng() - { - string result = ""; - - Bitmap rawData = WindowImage; - rawData = Window.ConvertToBlackAndWhite(rawData,150); - - TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default); - var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage)); - - Page page = engine.Process(tessImage); - Graphics g = Graphics.FromImage(rawData); - // Get the iterator for the page layout - using (var iter = page.GetIterator()) - { - // Loop over the elements of the page layout - iter.Begin(); - do - { - Rect boundingBox; - // Get the bounding box for the current element - if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox)) - { - g.DrawRectangle(Pens.Red, new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height)); - } - // Get the text for the current element - result += iter.GetText(PageIteratorLevel.Word); - } while (iter.Next(PageIteratorLevel.Word)); - } - rawData.Save(Reader.DEBUG_DUMP_FOLDER + result + "_Before" + ".png"); - return result; - } - */ public override object DecodePng(List DriverList) { string result = ""; diff --git a/OCR_Decode/Form1.Designer.cs b/OCR_Decode/Form1.Designer.cs index c21f65f..9e24a6d 100644 --- a/OCR_Decode/Form1.Designer.cs +++ b/OCR_Decode/Form1.Designer.cs @@ -40,23 +40,23 @@ // this.pbxImage.Location = new System.Drawing.Point(12, 12); this.pbxImage.Name = "pbxImage"; - this.pbxImage.Size = new System.Drawing.Size(1000, 600); + this.pbxImage.Size = new System.Drawing.Size(1231, 724); this.pbxImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom; this.pbxImage.TabIndex = 0; this.pbxImage.TabStop = false; // // tbxResult // - this.tbxResult.Location = new System.Drawing.Point(441, 618); + this.tbxResult.Location = new System.Drawing.Point(719, 742); this.tbxResult.Multiline = true; this.tbxResult.Name = "tbxResult"; this.tbxResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; - this.tbxResult.Size = new System.Drawing.Size(571, 192); + this.tbxResult.Size = new System.Drawing.Size(571, 255); this.tbxResult.TabIndex = 1; // // btnPrevious // - this.btnPrevious.Location = new System.Drawing.Point(12, 618); + this.btnPrevious.Location = new System.Drawing.Point(12, 745); this.btnPrevious.Name = "btnPrevious"; this.btnPrevious.Size = new System.Drawing.Size(228, 72); this.btnPrevious.TabIndex = 2; @@ -65,7 +65,7 @@ // // btnNext // - this.btnNext.Location = new System.Drawing.Point(12, 696); + this.btnNext.Location = new System.Drawing.Point(12, 823); this.btnNext.Name = "btnNext"; this.btnNext.Size = new System.Drawing.Size(228, 72); this.btnNext.TabIndex = 3; @@ -75,7 +75,7 @@ // lblFileName // this.lblFileName.AutoSize = true; - this.lblFileName.Location = new System.Drawing.Point(12, 771); + this.lblFileName.Location = new System.Drawing.Point(12, 898); this.lblFileName.Name = "lblFileName"; this.lblFileName.Size = new System.Drawing.Size(66, 16); this.lblFileName.TabIndex = 4; @@ -85,7 +85,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1022, 822); + this.ClientSize = new System.Drawing.Size(1255, 1009); this.Controls.Add(this.lblFileName); this.Controls.Add(this.btnNext); this.Controls.Add(this.btnPrevious); diff --git a/OCR_Decode/Reader.cs b/OCR_Decode/Reader.cs index 63f66ec..b5ea005 100644 --- a/OCR_Decode/Reader.cs +++ b/OCR_Decode/Reader.cs @@ -247,8 +247,9 @@ namespace OCR_Decode } public static string ConvertMsToTime(int amountOfMs) { - int minuts = Convert.ToInt32((float)amountOfMs / 1000f / 60f); - int seconds = Convert.ToInt32((float)minuts * 60f - (float)amountOfMs / 1000f); + //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 ms = amountOfMs - ((minuts * 60 * 1000) + (seconds * 1000)); return minuts + ":" + seconds + ":" + ms;