Compare commits

..

2 Commits

Author SHA1 Message Date
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
5 changed files with 86 additions and 17 deletions
+11 -1
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,6 +20,10 @@ 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);
+12 -1
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,6 +21,10 @@ 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);
+11 -1
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,6 +21,10 @@ 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);
+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)