Compare commits

3 Commits

Author SHA1 Message Date
Maxluli 4b9592c8e1 cleaned the rest of the files 2023-04-25 15:52:44 +02:00
Maxluli f2761db4c9 Cleaned some more files 2023-04-25 15:43:13 +02:00
Maxluli 57f19a1438 Cleaned Zone.cs 2023-04-25 15:22:11 +02:00
10 changed files with 160 additions and 33 deletions
+13 -1
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverData.cs
/// Brief : File that contains strcutures to store the found data
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -42,6 +48,10 @@ namespace OCR_Decode
Sector3 = -1;
CurrentTyre = new Tyre(Tyre.Type.Undefined,-1);
}
/// <summary>
/// Method that displays all the data found in a string
/// </summary>
/// <returns>string containing all the driver datas</returns>
public override string ToString()
{
string result = "";
@@ -68,8 +78,10 @@ namespace OCR_Decode
return result;
}
}
//Structure to store tyres infos
public struct Tyre
{
//If new tyres were to be added you will have to need to change this enum
public enum Type
{
Soft,
+11 -1
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverGapToLeaderWindow.cs
/// Brief : Window that contains infos about the driver Gap to leader
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -14,6 +20,10 @@ namespace OCR_Decode
{
Name = "Gap to leader";
}
/// <summary>
/// Decodes the gap to leader using Tesseract OCR
/// </summary>
/// <returns></returns>
public override async Task<object> DecodePng()
{
int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Gap, Engine);
+11 -1
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverLapTimeWindow.cs
/// Brief : Window that contains infos about a laptime of the driver
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -14,6 +20,10 @@ namespace OCR_Decode
{
Name = "Lap time";
}
/// <summary>
/// Decodes the lap time contained in the image using OCR Tesseract
/// </summary>
/// <returns>The laptime in int (ms)</returns>
public override async Task<object> DecodePng()
{
int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.LapTime,Engine);
+20 -3
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverNameWindow.cs
/// Brief : Window that contains infos about the driver name in it
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -15,6 +21,11 @@ namespace OCR_Decode
{
Name = "Name";
}
/// <summary>
/// Decodes using OCR wich driver name is in the image
/// </summary>
/// <param name="DriverList"></param>
/// <returns>The driver name in string</returns>
public override async Task<object> DecodePng(List<string> DriverList)
{
string result = "";
@@ -27,11 +38,17 @@ namespace OCR_Decode
}
return result;
}
private static bool IsADriver(List<string> drivers, string potentialDriver)
/// <summary>
/// Verifies that the name found in the OCR is a valid name
/// </summary>
/// <param name="driverList"></param>
/// <param name="potentialDriver"></param>
/// <returns>If ye or no the driver exists</returns>
private static bool IsADriver(List<string> driverList, string potentialDriver)
{
bool result = false;
//I cant use drivers.Contains because it has missmatched cases and all
foreach (string name in drivers)
foreach (string name in driverList)
{
if (name.ToUpper() == potentialDriver.ToUpper())
result = true;
+13 -4
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverPositionWindow.cs
/// Brief : Window that contains infos about a driver position
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -14,19 +20,22 @@ namespace OCR_Decode
{
Name = "Position";
}
/// <summary>
/// Decodes the position number using Tesseract OCR
/// </summary>
/// <returns>The position of the pilot in int</returns>
public override async Task<object> DecodePng()
{
string result = await GetStringFromPng(WindowImage,Engine, "0123456789");
string ocrResult = await GetStringFromPng(WindowImage,Engine, "0123456789");
int position;
try
{
position = Convert.ToInt32(result);
position = Convert.ToInt32(ocrResult);
}
catch
{
position = -1;
//WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "FailedPosition" + ".png");
}
return position;
}
+13 -3
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverSector1Window.cs
/// Brief : Window that contains infos about the first sector of a driver
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -14,10 +20,14 @@ namespace OCR_Decode
{
Name = "Sector 1";
}
/// <summary>
/// Decodes the sector
/// </summary>
/// <returns>the sector time in int (ms)</returns>
public override async Task<object> DecodePng()
{
int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector, Engine);
return result;
int ocrResult = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector, Engine);
return ocrResult;
}
}
}
+14 -3
View File
@@ -1,4 +1,11 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverSector2Window.cs
/// Brief : Window that contains infos about the second sector of a driver
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -14,10 +21,14 @@ namespace OCR_Decode
{
Name = "Sector 2";
}
/// <summary>
/// Decodes the sector
/// </summary>
/// <returns>the sector time in int (ms)</returns>
public override async Task<object> DecodePng()
{
int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector,Engine);
return result;
int ocrResult = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector,Engine);
return ocrResult;
}
}
}
+13 -3
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverSector3Window.cs
/// Brief : Window that contains infos about the third sector of a driver
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
@@ -15,10 +21,14 @@ namespace OCR_Decode
{
Name = "Sector 3";
}
/// <summary>
/// Decodes the sector
/// </summary>
/// <returns>the sector time in int (ms)</returns>
public override async Task<object> DecodePng()
{
int result = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector,Engine);
return result;
int ocrResult = await GetTimeFromPng(WindowImage, OcrImage.WindowType.Sector,Engine);
return ocrResult;
}
}
}
+27 -11
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : DriverTyresWindow.cs
/// Brief : Window that contains infos about tyres and that can decode them
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
@@ -14,6 +20,7 @@ namespace OCR_Decode
private static Random rnd = new Random();
int seed = rnd.Next(0,10000);
//Those are the colors I found but you can change them if they change in the future like in 2019
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(0xa4, 0xa5, 0xa8);
@@ -25,15 +32,21 @@ namespace OCR_Decode
{
Name = "Tyres";
}
/// <summary>
/// This will decode the content of the image
/// </summary>
/// <returns>And object containing what was on the image</returns>
public override async Task<object> DecodePng()
{
//WindowImage.Save(Reader.DEBUG_DUMP_FOLDER + "testTyres" + rnd.Next(0, 1000) + ".png");
return await GetTyreInfos();
}
/// <summary>
/// Method that will decode whats on the image and return the tyre infos it could manage to recover
/// </summary>
/// <returns>A tyre object containing tyre infos</returns>
private async Task<Tyre> GetTyreInfos()
{
Bitmap tyreZone = GetSmallBitmapFromBigOne(WindowImage, FindTyreZone());
//tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "NewTyreZone_"+seed+".png");
Tyre.Type type = Tyre.Type.Undefined;
type = GetTyreTypeFromColor(OcrImage.GetAvgColorFromBitmap(tyreZone));
int laps = -1;
@@ -51,12 +64,13 @@ namespace OCR_Decode
//tyreZone.Save(Reader.DEBUG_DUMP_FOLDER + "Tyre" + type + "Laps" + laps + '#' + rnd.Next(0, 1000) + ".png");
return new Tyre(type, laps);
}
/// <summary>
/// Finds where the important part of the image is
/// </summary>
/// <returns>A rectangle containing position and dimensions of the important part of the image</returns>
private Rectangle FindTyreZone()
{
Bitmap bmp = WindowImage;
//bmp.Save(Reader.DEBUG_DUMP_FOLDER + "OldTyreZone_" + seed + ".png");
int currentPosition = bmp.Width;
int height = bmp.Height / 2;
Color limitColor = Color.FromArgb(0x50, 0x50, 0x50);
@@ -79,10 +93,17 @@ namespace OCR_Decode
return new Rectangle(CorrectedX, CorrectedY, newWindowSize.Width, newWindowSize.Height);
}
//This method has been created with the help of chatGPT
/// <summary>
/// Methods that compares a list of colors to see wich is the closest from the input color and decide wich tyre type it is
/// </summary>
/// <param name="inputColor">The color that you found</param>
/// <returns>The tyre type</returns>
public Tyre.Type GetTyreTypeFromColor(Color inputColor)
{
Tyre.Type type = Tyre.Type.Undefined;
List<Color> colors = new List<Color>();
//dont forget that if for some reason someday F1 adds a new Tyre type you will need to add it in the constants but also here in the list
//You will also need to add it below in the Tyre object's enum and add an if in the end of this method
colors.Add(SOFT_TYRE_COLOR);
colors.Add(MEDIUM_TYRE_COLOR);
colors.Add(HARD_TYRE_COLOR);
@@ -102,11 +123,6 @@ namespace OCR_Decode
}
}
//Bitmap colorBmp = new Bitmap(200, 100);
//Graphics g = Graphics.FromImage(colorBmp);
//g.FillRectangle(new SolidBrush(closestColor), new Rectangle(0, 0, colorBmp.Width, colorBmp.Height));
//colorBmp.Save(Reader.DEBUG_DUMP_FOLDER+"AVGCOLOR_"+closestColor.ToString()+"_"+seed+".png");
//We cant use a switch as the colors cant be constants ...
if (closestColor == SOFT_TYRE_COLOR)
type = Tyre.Type.Soft;
+25 -3
View File
@@ -1,4 +1,10 @@
using System;
/// Author : Maxime Rohmer
/// Date : 25/04/2023
/// File : Zone.cs
/// Brief : Parent for futur zones and the that contains all the usefull methods for any zone
/// Version : 0.1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -18,6 +24,7 @@ namespace OCR_Decode
{
get
{
//This little trickery lets you have the image that the zone sees
Bitmap sample = new Bitmap(Bounds.Width, Bounds.Height);
Graphics g = Graphics.FromImage(sample);
g.DrawImage(Image, new Rectangle(0, 0, sample.Width, sample.Height), Bounds, GraphicsUnit.Pixel);
@@ -29,6 +36,7 @@ namespace OCR_Decode
get { return _image; }
set
{
//It automatically sets the image for the contained windows and zones
_image = Image;
foreach (Window w in Windows)
{
@@ -54,21 +62,35 @@ namespace OCR_Decode
_image = image;
Bounds = bounds;
}
/// <summary>
/// Adds a zone to the list of zones
/// </summary>
/// <param name="zone">The zone you want to add</param>
public virtual void AddZone(Zone zone)
{
Zones.Add(zone);
}
/// <summary>
/// Add a window to the list of windows
/// </summary>
/// <param name="window">the window you want to add</param>
public virtual void AddWindow(Window window)
{
Windows.Add(window);
}
public virtual async Task<DriverData> Decode(List<string> drivers)
/// <summary>
/// Calls all the windows to do OCR and to give back the results so we can send them to the model
/// </summary>
/// <param name="driverList">A list of all the driver in the race to help with text recognition</param>
/// <returns>A driver data object that contains all the infos about a driver</returns>
public virtual async Task<DriverData> Decode(List<string> driverList)
{
DriverData result = new DriverData();
Parallel.ForEach(Windows,async w =>
{
// A switch would be prettier but I dont think its supported in this C# version
if (w is DriverNameWindow)
result.Name = (string)await (w as DriverNameWindow).DecodePng(drivers);
result.Name = (string)await (w as DriverNameWindow).DecodePng(driverList);
if (w is DriverDrsWindow)
result.DRS = (bool)await (w as DriverDrsWindow).DecodePng();
if (w is DriverGapToLeaderWindow)