Added sector times and all but still not really precise and I also added some cleaning to the strings

This commit is contained in:
2023-04-04 17:53:12 +02:00
parent d9a0a5e766
commit 66ee1301f7
6 changed files with 102 additions and 77 deletions
+2 -1
View File
@@ -15,7 +15,8 @@ namespace OCR_Decode
}
public override object DecodePng()
{
return 0;
int result = GetTimeFromPng(WindowImage);
return result;
}
}
}
+1 -73
View File
@@ -16,79 +16,7 @@ namespace OCR_Decode
}
public override object DecodePng()
{
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;
}
}
}
int result = GetTimeFromPng(WindowImage);
return result;
}
}
+2 -1
View File
@@ -15,7 +15,8 @@ namespace OCR_Decode
}
public override object DecodePng()
{
return 0;
int result = GetTimeFromPng(WindowImage);
return result;
}
}
}
+2 -1
View File
@@ -15,7 +15,8 @@ namespace OCR_Decode
}
public override object DecodePng()
{
return 0;
int result = GetTimeFromPng(WindowImage);
return result;
}
}
}
+2 -1
View File
@@ -15,7 +15,8 @@ namespace OCR_Decode
}
public override object DecodePng()
{
return 0;
int result = GetTimeFromPng(WindowImage);
return result;
}
}
}
+93
View File
@@ -5,6 +5,8 @@ using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
using Tesseract;
using System.Text.RegularExpressions;
namespace OCR_Decode
{
@@ -83,5 +85,96 @@ namespace OCR_Decode
}
return result;
}
public static int GetTimeFromPng(Bitmap wImage)
{
//returns milliseconds
string rawResult = "";
int treshold = 165;
int result = 0;
Bitmap rawData = wImage;
rawData = Window.ConvertToBlackAndWhite(rawData, treshold);
TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
var tessImage = Pix.LoadFromMemory(ImageToByte(wImage));
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));
}
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);
}
if (rawNumbers.Count == 3)
{
//mm:ss:ms
result = (Convert.ToInt32(rawNumbers[0]) * 1000 * 60) + (Convert.ToInt32(rawNumbers[1]) * 1000) + Convert.ToInt32(rawNumbers[2]);
}
else
{
if (rawNumbers.Count == 2)
{
//ss:ms
result = (Convert.ToInt32(rawNumbers[0]) * 1000) + Convert.ToInt32(rawNumbers[1]);
}
else
{
if (rawNumbers.Count == 1)
{
//ss
//if (rawNumbers[0] == "LEADER")
try
{
result = Convert.ToInt32(rawNumbers[0]);
}
catch
{
//It can be because the input is empty or because its the LEADER bracket
result = 0;
}
}
else
{
//Auuuugh
result = 0;
}
}
}
return result;
}
}
}