Added post processing, now numbers detection is almost perfect

This commit is contained in:
2023-04-05 14:18:38 +02:00
parent 66ee1301f7
commit 341b9eb0a1
4 changed files with 303 additions and 13 deletions
+16 -13
View File
@@ -7,6 +7,7 @@ using System.Drawing;
using System.IO;
using Tesseract;
using System.Text.RegularExpressions;
using System.Drawing.Drawing2D;
namespace OCR_Decode
{
@@ -89,17 +90,22 @@ namespace OCR_Decode
{
//returns milliseconds
string rawResult = "";
int treshold = 165;
int treshold = 100;
int result = 0;
Bitmap rawData = wImage;
rawData = Window.ConvertToBlackAndWhite(rawData, treshold);
//Bitmap rawData = wImage;
OcrImage rawData = new OcrImage(wImage);
Bitmap enhancedImage = rawData.Enhance();
TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
var tessImage = Pix.LoadFromMemory(ImageToByte(wImage));
engine.DefaultPageSegMode = PageSegMode.SingleLine;
engine.SetVariable("tessedit_char_whitelist", "0123456789.:+-LEADRPleadrp");
var tessImage = Pix.LoadFromMemory(ImageToByte(enhancedImage));
Page page = engine.Process(tessImage);
Graphics g = Graphics.FromImage(rawData);
Graphics g = Graphics.FromImage(enhancedImage);
// Get the iterator for the page layout
using (var iter = page.GetIterator())
{
@@ -111,7 +117,7 @@ namespace OCR_Decode
// 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));
//g.DrawRectangle(Pens.Red, new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height));
}
// Get the text for the current element
try
@@ -125,18 +131,17 @@ namespace OCR_Decode
} while (iter.Next(PageIteratorLevel.Word));
}
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(rawResult, "[^0-9A-Za-z]", "") + ".png");
List<string> rawNumbers;
//Removes all non digit chars except for the important ones
string cleanedResult = Regex.Replace(rawResult, "[^0-9.:]", "");
//Splits into minuts seconds miliseconds
rawNumbers = cleanedResult.Split('.', ':').ToList<string>();
//removes any empty cells (tho this usually sign of a really bad OCR implementation tbh will have to be fixed higher in the chian)
for (int i = 0; i < rawNumbers.Count;i++)
{
if (rawNumbers[i] == "")
rawNumbers.RemoveAt(i);
}
rawNumbers.RemoveAll(x => ((string)x) == "");
if (rawNumbers.Count == 3)
{
@@ -154,8 +159,6 @@ namespace OCR_Decode
{
if (rawNumbers.Count == 1)
{
//ss
//if (rawNumbers[0] == "LEADER")
try
{
result = Convert.ToInt32(rawNumbers[0]);