Compare commits
5 Commits
b43b61d9b0
...
0.1
| Author | SHA1 | Date | |
|---|---|---|---|
| f6fdc8b150 | |||
| ea3ca8cd28 | |||
| 08a2176385 | |||
| c515e5ff62 | |||
| 124d141181 |
@@ -18,7 +18,7 @@ namespace OCR_Decode
|
||||
public override object DecodePng(List<string> DriverList)
|
||||
{
|
||||
string result = "";
|
||||
result = GetStringFromPng();
|
||||
result = GetStringFromPng(WindowImage);
|
||||
|
||||
if (!IsADriver(DriverList, result))
|
||||
{
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace OCR_Decode
|
||||
}
|
||||
public override object DecodePng()
|
||||
{
|
||||
string result = GetStringFromPng(true);
|
||||
string result = GetStringFromPng(WindowImage,"0123456789");
|
||||
|
||||
int position;
|
||||
try
|
||||
|
||||
@@ -11,6 +11,13 @@ namespace OCR_Decode
|
||||
internal class DriverTyresWindow : Window
|
||||
{
|
||||
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)
|
||||
{
|
||||
Name = "Tyres";
|
||||
@@ -23,50 +30,57 @@ namespace OCR_Decode
|
||||
}
|
||||
private Tyre GetTyreInfos()
|
||||
{
|
||||
/*
|
||||
string result = "";
|
||||
Bitmap tyreZone = GetSmallBitmapFromBigOne(WindowImage, FindTyreZone());
|
||||
|
||||
Bitmap rawData = WindowImage;
|
||||
//Hards softs mediums Inter Wet -> HSMIW
|
||||
Engine.SetVariable("tessedit_char_whitelist", "0123456789HSMIW");
|
||||
Page page = Engine.Process(rawData);
|
||||
Rectangle TyreRadius = new Rectangle(0, 0, 0, 0);
|
||||
tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "ZONE" + ".png");
|
||||
Tyre.Type type = Tyre.Type.Undefined;
|
||||
int laps = -1;
|
||||
|
||||
using (var iter = page.GetIterator())
|
||||
//MHIWsmhiw so we can detect Soft Medium Hard Inters and Wet tyres
|
||||
string text = GetStringFromPng(tyreZone, "SMHIW", OcrImage.WindowType.Tyre);
|
||||
if (text.Length == 1 && text != "")
|
||||
{
|
||||
iter.Begin();
|
||||
do
|
||||
//We found a tire letter
|
||||
laps = 0;
|
||||
text = text.ToUpper();
|
||||
switch (text[0])
|
||||
{
|
||||
Rect boundingBox;
|
||||
|
||||
// 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));
|
||||
case 'S':
|
||||
type = Tyre.Type.Soft;
|
||||
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;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string number = GetStringFromPng(tyreZone, "0123456789", OcrImage.WindowType.Tyre);
|
||||
try
|
||||
{
|
||||
laps = Convert.ToInt32(number);
|
||||
}
|
||||
catch
|
||||
{
|
||||
laps = -1;
|
||||
}
|
||||
type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(OcrImage.Resize(tyreZone)));
|
||||
}
|
||||
|
||||
//rawData.Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + result + ".png");
|
||||
|
||||
page.Dispose();
|
||||
*/
|
||||
|
||||
GetSmallBitmapFromBigOne(WindowImage,FindTyreZone()).Save(Reader.DEBUG_DUMP_FOLDER + "Tyres" + rnd.Next(0,1000) + ".png");
|
||||
|
||||
|
||||
return new Tyre(Tyre.Type.Undefined,0);
|
||||
tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png");
|
||||
return new Tyre(type,laps);
|
||||
}
|
||||
private Rectangle FindTyreZone()
|
||||
{
|
||||
@@ -76,7 +90,7 @@ namespace OCR_Decode
|
||||
Color limitColor = Color.FromArgb(0x50,0x50,0x50);
|
||||
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 * 25f),bmp.Height - Convert.ToInt32((float)bmp.Height / 100f * 35f));
|
||||
|
||||
while(currentColor.R <= limitColor.R && currentColor.G <= limitColor.G && currentColor.B <= limitColor.B && currentPosition > 0)
|
||||
{
|
||||
@@ -84,27 +98,51 @@ namespace OCR_Decode
|
||||
currentColor = bmp.GetPixel(currentPosition,height);
|
||||
}
|
||||
|
||||
//Its here to let the new window include a little bit of the right side
|
||||
int offset = Convert.ToInt32((float)newWindowSize.Width / 100f * 20f);
|
||||
int CorrectedX = currentPosition - (newWindowSize.Width - offset);
|
||||
//Its here to let the new window include a little bit of the right
|
||||
int CorrectedX = currentPosition - (newWindowSize.Width) + Convert.ToInt32((float)newWindowSize.Width/100f*10f);
|
||||
int CorrectedY = Convert.ToInt32((float)newWindowSize.Height / 100f * 35f);
|
||||
if (CorrectedX <= 0)
|
||||
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);
|
||||
Graphics g = Graphics.FromImage(sample);
|
||||
g.DrawImage(bmp, new Rectangle(0, 0, sample.Width, sample.Height), rectangle, GraphicsUnit.Pixel);
|
||||
return sample;
|
||||
Tyre.Type type = Tyre.Type.Undefined;
|
||||
List<Color> colors = new List<Color>();
|
||||
colors.Add(SOFT_TYRE_COLOR);
|
||||
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
|
||||
{
|
||||
|
||||
@@ -24,6 +24,7 @@ namespace OCR_Decode
|
||||
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
Reader = new Reader(CONFIG_FILE,TEMPORARY_IMAGE_FOLDER);
|
||||
RefreshUi();
|
||||
}
|
||||
|
||||
+177
-1
@@ -21,6 +21,7 @@ namespace OCR_Decode
|
||||
Text,
|
||||
Sector,
|
||||
Gap,
|
||||
Tyre,
|
||||
}
|
||||
public OcrImage(Bitmap inputImage)
|
||||
{
|
||||
@@ -46,6 +47,14 @@ namespace OCR_Decode
|
||||
result = Resize(result);
|
||||
result = Dilatation(result, 1);
|
||||
break;
|
||||
case WindowType.Tyre:
|
||||
//result = RemoveBG(result);
|
||||
result = RemoveUseless(result);
|
||||
result = Resize(result);
|
||||
result = Resize(result);
|
||||
result = Resize(result);
|
||||
result = Dilatation(result, 1);
|
||||
break;
|
||||
default:
|
||||
result = Tresholding(result, 165);
|
||||
result = Resize(result);
|
||||
@@ -53,7 +62,6 @@ namespace OCR_Decode
|
||||
result = Erode(result, 1);
|
||||
break;
|
||||
}
|
||||
|
||||
//result = Dilatation(result, 1);
|
||||
return result;
|
||||
}
|
||||
@@ -120,6 +128,174 @@ namespace OCR_Decode
|
||||
|
||||
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 unsafe static Bitmap RemoveUseless(Bitmap bmp)
|
||||
{
|
||||
bmp = RemoveBG(bmp);
|
||||
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;
|
||||
|
||||
|
||||
byte* ptr = (byte*)bmpData.Scan0.ToPointer();
|
||||
for (int y = 0; y < bmp.Height; y++)
|
||||
{
|
||||
byte* currentLine = ptr + (y * bmpData.Stride);
|
||||
|
||||
List<int> pixelsToRemove = new List<int>();
|
||||
|
||||
bool fromBorder = true;
|
||||
|
||||
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 (fromBorder && B < 0x10 && G < 0x10 && R < 0x10)
|
||||
{
|
||||
pixelsToRemove.Add(x);
|
||||
}
|
||||
else
|
||||
{
|
||||
fromBorder = false;
|
||||
}
|
||||
}
|
||||
fromBorder = true;
|
||||
for (int x = bmp.Width - 1; x > 0; x--)
|
||||
{
|
||||
byte* pixel = currentLine + (x * bytesPerPixel);
|
||||
|
||||
int B = pixel[0];
|
||||
int G = pixel[1];
|
||||
int R = pixel[2];
|
||||
|
||||
if (fromBorder && B < 0x10 && G < 0x10 && R < 0x10)
|
||||
{
|
||||
pixelsToRemove.Add(x);
|
||||
}
|
||||
else
|
||||
{
|
||||
fromBorder = false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (int pxPos in pixelsToRemove)
|
||||
{
|
||||
byte* pixel = currentLine + (pxPos * bytesPerPixel);
|
||||
|
||||
pixel[0] = 0xFF;
|
||||
pixel[1] = 0xFF;
|
||||
pixel[2] = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//NOW REMOVING THE COLOR
|
||||
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50);
|
||||
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)
|
||||
{
|
||||
pixel[0] = 0xFF;
|
||||
pixel[1] = 0xFF;
|
||||
pixel[2] = 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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, Convert.ToInt32((float)totR / (float)totPixels), Convert.ToInt32((float)totG / (float)totPixels), Convert.ToInt32((float)totB / (float)totPixels));
|
||||
}
|
||||
public static Bitmap InvertColors(Bitmap input)
|
||||
{
|
||||
Bitmap bmp = input;
|
||||
|
||||
@@ -217,7 +217,7 @@ namespace OCR_Decode
|
||||
case 4:
|
||||
//Tyres
|
||||
Tyre tyre = (Tyre)objects[objId];
|
||||
result += "Tyre : " + tyre.Coumpound + " laps with the tyre : " + tyre.NumberOfLaps + " ";
|
||||
result += "Uses " + tyre.Coumpound + " tyre for " + tyre.NumberOfLaps + " laps";
|
||||
break;
|
||||
case 5:
|
||||
//Name
|
||||
|
||||
+21
-16
@@ -153,22 +153,14 @@ namespace OCR_Decode
|
||||
page.Dispose();
|
||||
return result;
|
||||
}
|
||||
protected string GetStringFromPng(bool onlyDigit = false)
|
||||
public static string GetStringFromPng(Bitmap image,string allowedChars = "",OcrImage.WindowType windowType = OcrImage.WindowType.Text)
|
||||
{
|
||||
string result = "";
|
||||
|
||||
if (onlyDigit)
|
||||
{
|
||||
Engine.SetVariable("tessedit_char_whitelist", "0123456789");
|
||||
}
|
||||
else
|
||||
{
|
||||
Engine.SetVariable("tessedit_char_whitelist", "");
|
||||
}
|
||||
|
||||
Engine.SetVariable("tessedit_char_whitelist", allowedChars);
|
||||
|
||||
Bitmap rawData = WindowImage;
|
||||
Bitmap enhancedImage = new OcrImage(rawData).Enhance();
|
||||
Bitmap rawData = image;
|
||||
Bitmap enhancedImage = new OcrImage(rawData).Enhance(windowType);
|
||||
|
||||
Page page = Engine.Process(enhancedImage);
|
||||
using (var iter = page.GetIterator())
|
||||
@@ -179,13 +171,26 @@ namespace OCR_Decode
|
||||
result += iter.GetText(PageIteratorLevel.Word);
|
||||
} while (iter.Next(PageIteratorLevel.Word));
|
||||
}
|
||||
|
||||
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + Regex.Replace(result, "[^0-9A-Za-z]", "") + ".png");
|
||||
if (allowedChars.Contains("S"))
|
||||
{
|
||||
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + result +".png");
|
||||
}
|
||||
else
|
||||
{
|
||||
enhancedImage.Save(Reader.DEBUG_DUMP_FOLDER + result +".png");
|
||||
}
|
||||
|
||||
|
||||
page.Dispose();
|
||||
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)
|
||||
{
|
||||
var closestMatch = "";
|
||||
@@ -202,7 +207,7 @@ namespace OCR_Decode
|
||||
}
|
||||
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)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s1))
|
||||
|
||||
Reference in New Issue
Block a user