Optimised and verified the position detection using OCR

This commit is contained in:
2023-05-12 08:57:58 +02:00
parent 592025e283
commit 863ce1922f
4 changed files with 55 additions and 12 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ namespace Test_Merge
/// </summary>
/// <returns>The position of the pilot in int</returns>
public override async Task<object> DecodePng()
{
{
string ocrResult = await GetStringFromPng(WindowImage, Engine, "0123456789");
int position;
+11 -5
View File
@@ -41,22 +41,28 @@ namespace Test_Merge
/// </summary>
/// <param name="type">The type of the window. Depending on it different enhancing features will be applied</param>
/// <returns>The enhanced Bitmap</returns>
public Bitmap Enhance(WindowType type = WindowType.Text)
public Bitmap Enhance(int id,WindowType type = WindowType.Text)
{
Bitmap outputBitmap = (Bitmap)InputBitmap.Clone();
switch (type)
{
case WindowType.LapTime:
outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\raw_" + id + ".png");
outputBitmap = Tresholding(outputBitmap, 185);
//outputBitmap = Resize(outputBitmap, 2);
outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\treshold_" + id + ".png");
outputBitmap = Dilatation(outputBitmap, 1);
outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\dilatation_" + id + ".png");
outputBitmap = Erode(outputBitmap, 1);
outputBitmap.Save(Window.LAPTIME_DEBUG_FOLDER + @"\Final_erode_" + id + ".png");
break;
case WindowType.Text:
outputBitmap = InvertColors(outputBitmap);
outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\raw_" + id + ".png");
outputBitmap = Tresholding(outputBitmap, 165);
//outputBitmap = Resize(outputBitmap, 2);
outputBitmap = Dilatation(outputBitmap, 1);
outputBitmap.Save(Window.STRING_DEBUG_FOLDER + @"\Final_treshold_" + id + ".png");
break;
case WindowType.Tyre:
outputBitmap = RemoveUseless(outputBitmap);
+41 -4
View File
@@ -19,6 +19,16 @@ namespace Test_Merge
{
public class Window
{
public const string STRING_DEBUG_FOLDER = "./GetString";
public const string LAPTIME_DEBUG_FOLDER = "./LapTime";
public const string GAPTOLEADER_DEBUG_FOLDER = "./Gap";
public const string SECTOR1_DEBUG_FOLDER = "./Sector1";
public const string SECTOR2_DEBUG_FOLDER = "./Sector2";
public const string SECTOR3_DEBUG_FOLDER = "./Sector3";
public const string DRS_DEBUG_FOLDER = "./DRS";
public const string TYRE_DEBUG_FOLDER = "./Tyre";
private Rectangle _bounds;
private Bitmap _image;
private string _name;
@@ -28,6 +38,8 @@ namespace Test_Merge
public string Name { get => _name; protected set => _name = value; }
//This will have to be changed if you want to make it run on your machine
public static DirectoryInfo TESS_DATA_FOLDER = new DirectoryInfo(@"C:\Users\Moi\Pictures\SeleniumScreens\TessData");
//Debug
public static Random rnd = new Random();
public Bitmap WindowImage
{
@@ -49,6 +61,24 @@ namespace Test_Merge
Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
Engine.DefaultPageSegMode = PageSegMode.SingleLine;
}
//DEBUG
if (!Directory.Exists(STRING_DEBUG_FOLDER))
Directory.CreateDirectory(STRING_DEBUG_FOLDER);
if (!Directory.Exists(LAPTIME_DEBUG_FOLDER))
Directory.CreateDirectory(LAPTIME_DEBUG_FOLDER);
if (!Directory.Exists(GAPTOLEADER_DEBUG_FOLDER))
Directory.CreateDirectory(GAPTOLEADER_DEBUG_FOLDER);
if (!Directory.Exists(SECTOR1_DEBUG_FOLDER))
Directory.CreateDirectory(SECTOR1_DEBUG_FOLDER);
if (!Directory.Exists(SECTOR2_DEBUG_FOLDER))
Directory.CreateDirectory(SECTOR2_DEBUG_FOLDER);
if (!Directory.Exists(SECTOR3_DEBUG_FOLDER))
Directory.CreateDirectory(SECTOR3_DEBUG_FOLDER);
if (!Directory.Exists(DRS_DEBUG_FOLDER))
Directory.CreateDirectory(DRS_DEBUG_FOLDER);
if (!Directory.Exists(TYRE_DEBUG_FOLDER))
Directory.CreateDirectory(TYRE_DEBUG_FOLDER);
}
/// <summary>
/// Method that will have to be used by the childrens to let the model make them decode the images they have
@@ -93,6 +123,9 @@ namespace Test_Merge
string rawResult = "";
int result = 0;
//Debug
int salt = rnd.Next(0,999999);
switch (windowType)
{
case OcrImage.WindowType.Sector:
@@ -113,7 +146,7 @@ namespace Test_Merge
}
Bitmap enhancedImage = new OcrImage(image).Enhance(windowType);
Bitmap enhancedImage = new OcrImage(image).Enhance(salt,windowType);
var tessImage = Pix.LoadFromMemory(ImageToByte(enhancedImage));
@@ -184,7 +217,7 @@ namespace Test_Merge
{
//A lap time is fucked
Console.Write("Fuck");
}
}
}
if (windowType == OcrImage.WindowType.Sector)
{
@@ -195,7 +228,8 @@ namespace Test_Merge
//For no apparent reason im going to delete the first
seconds = Convert.ToInt32(rawNumbers[0][1].ToString() + rawNumbers[0][2].ToString());
}
if (rawNumbers[0].Length == 2) {
if (rawNumbers[0].Length == 2)
{
seconds = Convert.ToInt32(rawNumbers[0][0].ToString() + rawNumbers[0][1].ToString());
}
int ms = Convert.ToInt32(rawNumbers[1][0].ToString() + rawNumbers[1][1].ToString() + rawNumbers[1][2].ToString());
@@ -295,10 +329,13 @@ namespace Test_Merge
{
string result = "";
//Debug
int salt = rnd.Next(0, 999999);
Engine.SetVariable("tessedit_char_whitelist", allowedChars);
Bitmap rawData = image;
Bitmap enhancedImage = new OcrImage(rawData).Enhance(windowType);
Bitmap enhancedImage = new OcrImage(rawData).Enhance(salt,windowType);
Page page = Engine.Process(enhancedImage);
using (var iter = page.GetIterator())
+2 -2
View File
@@ -106,8 +106,8 @@ namespace Test_Merge
//result.LapTime = (int)await (w as DriverLapTimeWindow).DecodePng();
result.LapTime = 0;
if (w is DriverPositionWindow)
//result.Position = (int)await (w as DriverPositionWindow).DecodePng();
result.Position = 0;
result.Position = (int)await (w as DriverPositionWindow).DecodePng();
//result.Position = 0;
if (w is DriverSectorWindow)
{
sectorCount++;