Trying to implement tyres but not working for now

This commit is contained in:
2023-04-07 15:58:12 +02:00
parent b43b61d9b0
commit 124d141181
6 changed files with 187 additions and 72 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ namespace OCR_Decode
public override object DecodePng(List<string> DriverList) public override object DecodePng(List<string> DriverList)
{ {
string result = ""; string result = "";
result = GetStringFromPng(); result = GetStringFromPng(WindowImage);
if (!IsADriver(DriverList, result)) if (!IsADriver(DriverList, result))
{ {
+1 -1
View File
@@ -15,7 +15,7 @@ namespace OCR_Decode
} }
public override object DecodePng() public override object DecodePng()
{ {
string result = GetStringFromPng(true); string result = GetStringFromPng(WindowImage,"0123456789");
int position; int position;
try try
+89 -56
View File
@@ -11,6 +11,13 @@ namespace OCR_Decode
internal class DriverTyresWindow : Window internal class DriverTyresWindow : Window
{ {
private static Random rnd = new Random(); 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 DriverTyresWindow(Bitmap image, Rectangle bounds) : base(image, bounds) public DriverTyresWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
{ {
Name = "Tyres"; Name = "Tyres";
@@ -23,50 +30,52 @@ namespace OCR_Decode
} }
private Tyre GetTyreInfos() private Tyre GetTyreInfos()
{ {
/* Bitmap tyreZone = GetSmallBitmapFromBigOne(WindowImage, FindTyreZone());
string result = ""; //MHIWsmhiw so we can detect Soft Medium Hard Inters and Wet tyres
//If it is the first lap with the tyre just the letter is displayed
string text = GetStringFromPng(tyreZone,"0123456789SMHIWsmhiw",OcrImage.WindowType.Tyre);
Bitmap rawData = WindowImage; tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "ZONE" + ".png");
//Hards softs mediums Inter Wet -> HSMIW Tyre.Type type = Tyre.Type.Undefined;
Engine.SetVariable("tessedit_char_whitelist", "0123456789HSMIW"); int laps = -1;
Page page = Engine.Process(rawData); if (text.All(char.IsDigit))
Rectangle TyreRadius = new Rectangle(0, 0, 0, 0);
using (var iter = page.GetIterator())
{ {
iter.Begin(); //We have a lap count
do if (text != "")
{ {
Rect boundingBox; laps = Convert.ToInt32(text);
type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(tyreZone));
// Get the bounding box for the current element }
if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
{
int xOffset = ((boundingBox.Width / 100) * 20) / 2;
int yOffset = ((boundingBox.Height / 100) * 20) / 2;
using (Graphics g = Graphics.FromImage(WindowImage))
{
//TyreRadius = new Rectangle(boundingBox.X1 - xOffset, boundingBox.Y1 - yOffset, boundingBox.Width + xOffset * 2, boundingBox.Height + yOffset * 2);
TyreRadius = new Rectangle(boundingBox.X1,boundingBox.Y1,boundingBox.Width,boundingBox.Height);
g.DrawRectangle(Pens.AliceBlue,TyreRadius);
GetSmallBitmapFromBigOne(WindowImage,TyreRadius).Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + result + ".png");
}
}
result += iter.GetText(PageIteratorLevel.Word);
} while (iter.Next(PageIteratorLevel.Word));
} }
else
//rawData.Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + result + ".png"); {
//We have a tire type
page.Dispose(); laps = 0;
*/ text = text.ToUpper();
switch (text[0])
GetSmallBitmapFromBigOne(WindowImage,FindTyreZone()).Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + rnd.Next(0,1000) + ".png"); {
case 'S':
type = Tyre.Type.Soft;
return new Tyre(Tyre.Type.Undefined,0); 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;
}
}
tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png");
return new Tyre(type,laps);
} }
private Rectangle FindTyreZone() private Rectangle FindTyreZone()
{ {
@@ -76,7 +85,7 @@ namespace OCR_Decode
Color limitColor = Color.FromArgb(0x50,0x50,0x50); Color limitColor = Color.FromArgb(0x50,0x50,0x50);
Color currentColor = Color.FromArgb(0,0,0); Color currentColor = Color.FromArgb(0,0,0);
Size newWindowSize = new Size(bmp.Height,bmp.Height); Size newWindowSize = new Size(bmp.Height -Convert.ToInt32((float)bmp.Height / 100f * 40f),bmp.Height - Convert.ToInt32((float)bmp.Height / 100f * 40f));
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)
{ {
@@ -84,27 +93,51 @@ namespace OCR_Decode
currentColor = bmp.GetPixel(currentPosition,height); currentColor = bmp.GetPixel(currentPosition,height);
} }
//Its here to let the new window include a little bit of the right side //Its here to let the new window include a little bit of the right
int offset = Convert.ToInt32((float)newWindowSize.Width / 100f * 20f); int CorrectedX = currentPosition - (newWindowSize.Width);
int CorrectedX = currentPosition - (newWindowSize.Width - offset); int CorrectedY = Convert.ToInt32((float)newWindowSize.Height / 100f * 30f);
if (CorrectedX <= 0) 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,0,newWindowSize.Width,newWindowSize.Height); return new Rectangle(CorrectedX,CorrectedY,newWindowSize.Width,newWindowSize.Height);
} }
private Bitmap GetSmallBitmapFromBigOne(Bitmap bmp, Rectangle rectangle) //This method has been created with the help of chatGPT
public Tyre.Type GetTyreTypeFromColor(Color inputColor)
{ {
Bitmap sample = new Bitmap(rectangle.Width, rectangle.Height); Tyre.Type type = Tyre.Type.Undefined;
Graphics g = Graphics.FromImage(sample); List<Color> colors = new List<Color>();
g.DrawImage(bmp, new Rectangle(0, 0, sample.Width, sample.Height), rectangle, GraphicsUnit.Pixel); colors.Add(SOFT_TYRE_COLOR);
return sample; colors.Add(MEDIUM_TYRE_COLOR);
colors.Add(HARD_TYRE_COLOR);
colors.Add(INTER_TYRE_COLOR);
colors.Add(WET_TYRE_COLOR);
Color closestColor = colors[0];
int closestDistance = int.MaxValue;
foreach (Color color in colors)
{
int distance = Math.Abs(color.R - inputColor.R) + Math.Abs(color.G - inputColor.G) + Math.Abs(color.B - inputColor.B);
if (distance < closestDistance)
{
closestColor = color;
closestDistance = distance;
}
}
//We cant use a switch as the colors cant be constants ...
if (closestColor == SOFT_TYRE_COLOR)
type = Tyre.Type.Soft;
if (closestColor == MEDIUM_TYRE_COLOR)
type = Tyre.Type.Medium;
if (closestColor == HARD_TYRE_COLOR)
type = Tyre.Type.Hard;
if (closestColor == INTER_TYRE_COLOR)
type = Tyre.Type.Inter;
if (closestColor == WET_TYRE_COLOR)
type = Tyre.Type.Wet;
return type;
} }
/*
public Tyre.Type GetColor(Bitmap bmp)
{
return Color.Violet;
}
*/
} }
struct Tyre struct Tyre
{ {
+1
View File
@@ -24,6 +24,7 @@ namespace OCR_Decode
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
Reader = new Reader(CONFIG_FILE,TEMPORARY_IMAGE_FOLDER); Reader = new Reader(CONFIG_FILE,TEMPORARY_IMAGE_FOLDER);
RefreshUi(); RefreshUi();
} }
+83
View File
@@ -21,6 +21,7 @@ namespace OCR_Decode
Text, Text,
Sector, Sector,
Gap, Gap,
Tyre,
} }
public OcrImage(Bitmap inputImage) public OcrImage(Bitmap inputImage)
{ {
@@ -46,6 +47,15 @@ namespace OCR_Decode
result = Resize(result); result = Resize(result);
result = Dilatation(result, 1); result = Dilatation(result, 1);
break; break;
case WindowType.Tyre:
//result = Tresholding(result, 200);
result = RemoveBG(result);
result = Grayscale(result);
result = InvertColors(result);
result = Resize(result);
//result = Resize(result);
//result = Dilatation(result,1);
break;
default: default:
result = Tresholding(result, 165); result = Tresholding(result, 165);
result = Resize(result); result = Resize(result);
@@ -120,6 +130,79 @@ namespace OCR_Decode
return bmp; return bmp;
} }
public static Bitmap RemoveBG(Bitmap input)
{
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50);
Bitmap bmp = input;
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < bmp.Height; y++)
{
byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++)
{
byte* pixel = currentLine + (x * bytesPerPixel);
int B = pixel[0];
int G = pixel[1];
int R = pixel[2];
if (R <= limitColor.R && G <= limitColor.G && B <= limitColor.B)
pixel[0] = pixel[1] = pixel[2] = 0;
}
}
}
bmp.UnlockBits(bmpData);
return bmp;
}
public static Color GetAvgColorFromBitmap(Bitmap bmp)
{
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
int bytesPerPixel = Bitmap.GetPixelFormatSize(bmp.PixelFormat) / 8;
int totR = 0;
int totG = 0;
int totB = 0;
int totPixels = 1;
unsafe
{
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
for (int y = 0; y < bmp.Height; y++)
{
byte* currentLine = ptr + (y * bmpData.Stride);
for (int x = 0; x < bmp.Width; x++)
{
byte* pixel = currentLine + (x * bytesPerPixel);
int B = pixel[0];
int G = pixel[1];
int R = pixel[2];
//We remove the background pixels
if (R >= limitColor.R && G >= limitColor.G && B >= limitColor.B)
{
totPixels++;
totB += pixel[0];
totG += pixel[1];
totR += pixel[2];
}
}
}
}
bmp.UnlockBits(bmpData);
return Color.FromArgb(255,totR / totPixels,totG / totPixels,totB / totPixels);
}
public static Bitmap InvertColors(Bitmap input) public static Bitmap InvertColors(Bitmap input)
{ {
Bitmap bmp = input; Bitmap bmp = input;
+12 -14
View File
@@ -153,22 +153,14 @@ namespace OCR_Decode
page.Dispose(); page.Dispose();
return result; return result;
} }
protected string GetStringFromPng(bool onlyDigit = false) public static string GetStringFromPng(Bitmap image,string allowedChars = "",OcrImage.WindowType windowType = OcrImage.WindowType.Text)
{ {
string result = ""; string result = "";
if (onlyDigit) Engine.SetVariable("tessedit_char_whitelist", allowedChars);
{
Engine.SetVariable("tessedit_char_whitelist", "0123456789");
}
else
{
Engine.SetVariable("tessedit_char_whitelist", "");
}
Bitmap rawData = image;
Bitmap rawData = WindowImage; Bitmap enhancedImage = new OcrImage(rawData).Enhance(windowType);
Bitmap enhancedImage = new OcrImage(rawData).Enhance();
Page page = Engine.Process(enhancedImage); Page page = Engine.Process(enhancedImage);
using (var iter = page.GetIterator()) using (var iter = page.GetIterator())
@@ -185,7 +177,13 @@ namespace OCR_Decode
page.Dispose(); page.Dispose();
return result; return result;
} }
//This method has been gnerated using ChatGPT protected Bitmap GetSmallBitmapFromBigOne(Bitmap bmp, Rectangle rectangle)
{
Bitmap sample = new Bitmap(rectangle.Width, rectangle.Height);
Graphics g = Graphics.FromImage(sample);
g.DrawImage(bmp, new Rectangle(0, 0, sample.Width, sample.Height), rectangle, GraphicsUnit.Pixel);
return sample;
}
protected static string FindClosestMatch(List<string> array, string target) protected static string FindClosestMatch(List<string> array, string target)
{ {
var closestMatch = ""; var closestMatch = "";
@@ -202,7 +200,7 @@ namespace OCR_Decode
} }
return closestMatch; return closestMatch;
} }
//This is a tool to be able to compare strings //This method has been generated with the help of ChatGPT
protected static int LevenshteinDistance(string s1, string s2) protected static int LevenshteinDistance(string s1, string s2)
{ {
if (string.IsNullOrEmpty(s1)) if (string.IsNullOrEmpty(s1))