Added a lot to the doc and modified the pdf generation
This commit is contained in:
+225
-117
@@ -1,8 +1,8 @@
|
||||
/// Author : Maxime Rohmer
|
||||
/// Date : 08/05/2023
|
||||
/// Date : 30/05/2023
|
||||
/// File : Reader.cs
|
||||
/// Brief : Class used to Read the config file for the OCR
|
||||
/// Version : 0.1
|
||||
/// Version : Alpha 1.0
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -14,7 +14,7 @@ using System.Windows.Forms;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Test_Merge
|
||||
namespace TrackTrends
|
||||
{
|
||||
public class Reader
|
||||
{
|
||||
@@ -22,123 +22,188 @@ namespace Test_Merge
|
||||
public List<string> Drivers;
|
||||
public List<Zone> MainZones;
|
||||
|
||||
public Reader(string configFile, Bitmap image,bool loadOCR = true)
|
||||
private SqliteStorage _storage;
|
||||
private List<DriverData>[] DriverDataLogs = new List<DriverData>[NUMBER_OF_DRIVERS];
|
||||
private int[] DriverLaps = new int[NUMBER_OF_DRIVERS];
|
||||
|
||||
public SqliteStorage Storage { get => _storage; private set => _storage = value; }
|
||||
|
||||
public Reader(string configFile, Bitmap image, bool loadOCR = true)
|
||||
{
|
||||
MainZones = Load(image,configFile,ref Drivers,loadOCR);
|
||||
Storage = new SqliteStorage();
|
||||
MainZones = Load(image, configFile, ref Drivers, loadOCR);
|
||||
}
|
||||
/// <summary>
|
||||
/// Method that reads the JSON config file and create all the Zones and Windows
|
||||
/// </summary>
|
||||
/// <param name="imageNumber">The image #id on wich you want to create the zones on</param>
|
||||
public static List<Zone> Load(Bitmap image,string configFilePath,ref List<string> driverListToFill,bool LoadOCR)
|
||||
public List<Zone> Load(Bitmap image, string configFilePath, ref List<string> driverListToFill, bool LoadOCR)
|
||||
{
|
||||
// Note : You may wonder why in the H... I have all the zones and windows stored in a JSON file and not just for example the first and the last
|
||||
// Its because they are not perfectly aligned to each others and every zone has his own alignement to the main image
|
||||
List<Zone> mainZones = new List<Zone>();
|
||||
Bitmap fullImage = image;
|
||||
List<string> drivers;
|
||||
Zone mainZone;
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_DRIVERS; i++)
|
||||
{
|
||||
DriverDataLogs[i] = new List<DriverData>();
|
||||
DriverLaps[i] = 0;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
using (var streamReader = new StreamReader(configFilePath))
|
||||
string jsonString = File.ReadAllText(configFilePath);
|
||||
|
||||
JsonDocument document = JsonDocument.Parse(jsonString);
|
||||
|
||||
JsonElement root = document.RootElement;
|
||||
|
||||
mainZones = new List<Zone>();
|
||||
driverListToFill = new List<string>();
|
||||
|
||||
JsonElement main = root.GetProperty("Main");
|
||||
|
||||
int x = main.GetProperty("x").GetInt32();
|
||||
int y = main.GetProperty("y").GetInt32();
|
||||
int width = main.GetProperty("width").GetInt32();
|
||||
int height = main.GetProperty("height").GetInt32();
|
||||
|
||||
mainZone = new Zone(fullImage, new Rectangle(x, y, width, height), "Main");
|
||||
|
||||
mainZone.ResetWindows();
|
||||
mainZone.ResetZones();
|
||||
|
||||
JsonElement driverZones = main.GetProperty("DriverZones");
|
||||
|
||||
foreach (JsonElement driverZoneElement in driverZones.EnumerateArray())
|
||||
{
|
||||
var jsonText = streamReader.ReadToEnd();
|
||||
var jsonDocument = JsonDocument.Parse(jsonText);
|
||||
string name = driverZoneElement.GetProperty("name").GetString();
|
||||
int driverX = driverZoneElement.GetProperty("x").GetInt32() + mainZone.Bounds.X;
|
||||
int driverY = driverZoneElement.GetProperty("y").GetInt32() + mainZone.Bounds.Y;
|
||||
int driverWidth = driverZoneElement.GetProperty("width").GetInt32();
|
||||
int driverHeight = driverZoneElement.GetProperty("height").GetInt32();
|
||||
|
||||
var driversNames = jsonDocument.RootElement.GetProperty("Drivers");
|
||||
driverListToFill = new List<string>();
|
||||
Zone driverZone = new Zone(fullImage, new Rectangle(driverX, driverY, driverWidth, driverHeight), "Driver");
|
||||
|
||||
foreach (var nameElement in driversNames.EnumerateArray())
|
||||
JsonElement windowsElement = driverZoneElement.GetProperty("Windows");
|
||||
|
||||
//string[] windowNames = new string[] { "Position","GapToLeader","LapTime","DRS","Tyres","Name","Sector1","Sector2","Sector3" };
|
||||
|
||||
foreach (JsonElement windowElement in windowsElement.EnumerateArray())
|
||||
{
|
||||
driverListToFill.Add(nameElement.GetString());
|
||||
//Position
|
||||
JsonElement posEl = windowElement.GetProperty("Position");
|
||||
DriverPositionWindow positionWindow = new DriverPositionWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
posEl.GetProperty("x").GetInt32(),
|
||||
posEl.GetProperty("y").GetInt32(),
|
||||
posEl.GetProperty("width").GetInt32(),
|
||||
posEl.GetProperty("height").GetInt32()),
|
||||
LoadOCR);
|
||||
|
||||
//GapToLeader
|
||||
JsonElement gapEl = windowElement.GetProperty("GapToLeader");
|
||||
DriverGapToLeaderWindow gapWindow = new DriverGapToLeaderWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
gapEl.GetProperty("x").GetInt32(),
|
||||
gapEl.GetProperty("y").GetInt32(),
|
||||
gapEl.GetProperty("width").GetInt32(),
|
||||
gapEl.GetProperty("height").GetInt32()),
|
||||
LoadOCR);
|
||||
|
||||
//LapTime
|
||||
JsonElement lapEl = windowElement.GetProperty("LapTime");
|
||||
DriverLapTimeWindow lapWindow = new DriverLapTimeWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
lapEl.GetProperty("x").GetInt32(),
|
||||
lapEl.GetProperty("y").GetInt32(),
|
||||
lapEl.GetProperty("width").GetInt32(),
|
||||
lapEl.GetProperty("height").GetInt32()),
|
||||
LoadOCR);
|
||||
|
||||
//DRS
|
||||
JsonElement drsEl = windowElement.GetProperty("DRS");
|
||||
DriverDrsWindow drsWindow = new DriverDrsWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
drsEl.GetProperty("x").GetInt32(),
|
||||
drsEl.GetProperty("y").GetInt32(),
|
||||
drsEl.GetProperty("width").GetInt32(),
|
||||
drsEl.GetProperty("height").GetInt32()),
|
||||
LoadOCR);
|
||||
|
||||
//Tyre
|
||||
JsonElement tyresEl = windowElement.GetProperty("Tyres");
|
||||
DriverTyresWindow tyreWindow = new DriverTyresWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
tyresEl.GetProperty("x").GetInt32(),
|
||||
tyresEl.GetProperty("y").GetInt32(),
|
||||
tyresEl.GetProperty("width").GetInt32(),
|
||||
tyresEl.GetProperty("height").GetInt32()),
|
||||
LoadOCR);
|
||||
|
||||
//Name
|
||||
JsonElement nameEl = windowElement.GetProperty("Name");
|
||||
DriverNameWindow nameWindow = new DriverNameWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
nameEl.GetProperty("x").GetInt32(),
|
||||
nameEl.GetProperty("y").GetInt32(),
|
||||
nameEl.GetProperty("width").GetInt32(),
|
||||
nameEl.GetProperty("height").GetInt32()),
|
||||
LoadOCR);
|
||||
|
||||
//Sector1
|
||||
JsonElement sec1El = windowElement.GetProperty("Sector1");
|
||||
DriverSectorWindow sec1Window = new DriverSectorWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
sec1El.GetProperty("x").GetInt32(),
|
||||
sec1El.GetProperty("y").GetInt32(),
|
||||
sec1El.GetProperty("width").GetInt32(),
|
||||
sec1El.GetProperty("height").GetInt32()),
|
||||
1, LoadOCR);
|
||||
|
||||
//Sector2
|
||||
JsonElement sec2El = windowElement.GetProperty("Sector2");
|
||||
DriverSectorWindow sec2Window = new DriverSectorWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
sec2El.GetProperty("x").GetInt32(),
|
||||
sec2El.GetProperty("y").GetInt32(),
|
||||
sec2El.GetProperty("width").GetInt32(),
|
||||
sec2El.GetProperty("height").GetInt32()),
|
||||
2, LoadOCR);
|
||||
|
||||
//Sector3
|
||||
JsonElement sec3El = windowElement.GetProperty("Sector3");
|
||||
DriverSectorWindow sec3Window = new DriverSectorWindow(driverZone.ZoneImage,
|
||||
new Rectangle(
|
||||
sec3El.GetProperty("x").GetInt32(),
|
||||
sec3El.GetProperty("y").GetInt32(),
|
||||
sec3El.GetProperty("width").GetInt32(),
|
||||
sec3El.GetProperty("height").GetInt32()),
|
||||
3, LoadOCR);
|
||||
|
||||
driverZone.AddWindow(positionWindow);
|
||||
driverZone.AddWindow(gapWindow);
|
||||
driverZone.AddWindow(lapWindow);
|
||||
driverZone.AddWindow(drsWindow);
|
||||
driverZone.AddWindow(tyreWindow);
|
||||
driverZone.AddWindow(nameWindow);
|
||||
driverZone.AddWindow(sec1Window);
|
||||
driverZone.AddWindow(sec2Window);
|
||||
driverZone.AddWindow(sec3Window);
|
||||
}
|
||||
|
||||
var mainProperty = jsonDocument.RootElement.GetProperty("Main");
|
||||
Point MainPosition = new Point(mainProperty.GetProperty("x").GetInt32(), mainProperty.GetProperty("y").GetInt32());
|
||||
Size MainSize = new Size(mainProperty.GetProperty("width").GetInt32(), mainProperty.GetProperty("height").GetInt32());
|
||||
Rectangle MainRectangle = new Rectangle(MainPosition, MainSize);
|
||||
mainZone = new Zone(image, MainRectangle,"Main");
|
||||
|
||||
var zones = mainProperty.GetProperty("Zones");
|
||||
var driverZone = zones[0].GetProperty("DriverZone");
|
||||
|
||||
Point FirstZonePosition = new Point(driverZone.GetProperty("x").GetInt32(), driverZone.GetProperty("y").GetInt32());
|
||||
Size FirstZoneSize = new Size(driverZone.GetProperty("width").GetInt32(), driverZone.GetProperty("height").GetInt32());
|
||||
|
||||
var windows = driverZone.GetProperty("Windows");
|
||||
|
||||
var driverPosition = windows[0].GetProperty("Position");
|
||||
Size driverPositionArea = new Size(driverPosition.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverPositionPosition = new Point(driverPosition.GetProperty("x").GetInt32(), driverPosition.GetProperty("y").GetInt32());
|
||||
|
||||
var driverGapToLeader = windows[0].GetProperty("GapToLeader");
|
||||
Size driverGapToLeaderArea = new Size(driverGapToLeader.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverGapToLeaderPosition = new Point(driverGapToLeader.GetProperty("x").GetInt32(), driverGapToLeader.GetProperty("y").GetInt32());
|
||||
|
||||
var driverLapTime = windows[0].GetProperty("LapTime");
|
||||
Size driverLapTimeArea = new Size(driverLapTime.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverLapTimePosition = new Point(driverLapTime.GetProperty("x").GetInt32(), driverLapTime.GetProperty("y").GetInt32());
|
||||
|
||||
|
||||
var driverDrs = windows[0].GetProperty("DRS");
|
||||
Size driverDrsArea = new Size(driverDrs.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverDrsPosition = new Point(driverDrs.GetProperty("x").GetInt32(), driverDrs.GetProperty("y").GetInt32());
|
||||
|
||||
var driverTyres = windows[0].GetProperty("Tyres");
|
||||
Size driverTyresArea = new Size(driverTyres.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverTyresPosition = new Point(driverTyres.GetProperty("x").GetInt32(), driverTyres.GetProperty("y").GetInt32());
|
||||
|
||||
var driverName = windows[0].GetProperty("Name");
|
||||
Size driverNameArea = new Size(driverName.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverNamePosition = new Point(driverName.GetProperty("x").GetInt32(), driverName.GetProperty("y").GetInt32());
|
||||
|
||||
var driverSector1 = windows[0].GetProperty("Sector1");
|
||||
Size driverSector1Area = new Size(driverSector1.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverSector1Position = new Point(driverSector1.GetProperty("x").GetInt32(), driverSector1.GetProperty("y").GetInt32());
|
||||
|
||||
var driverSector2 = windows[0].GetProperty("Sector2");
|
||||
Size driverSector2Area = new Size(driverSector2.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverSector2Position = new Point(driverSector2.GetProperty("x").GetInt32(), driverSector2.GetProperty("y").GetInt32());
|
||||
|
||||
var driverSector3 = windows[0].GetProperty("Sector3");
|
||||
Size driverSector3Area = new Size(driverSector3.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||
Point driverSector3Position = new Point(driverSector3.GetProperty("x").GetInt32(), driverSector3.GetProperty("y").GetInt32());
|
||||
|
||||
float offset = (((float)mainZone.ZoneImage.Height - (float)(driverListToFill.Count * FirstZoneSize.Height)) / (float)driverListToFill.Count);
|
||||
Bitmap MainZoneImage = mainZone.ZoneImage;
|
||||
List<Zone> zonesToAdd = new List<Zone>();
|
||||
List<Bitmap> zonesImages = new List<Bitmap>();
|
||||
|
||||
for (int i = 0; i < NUMBER_OF_DRIVERS; i++)
|
||||
{
|
||||
Point tmpPos = new Point(0, FirstZonePosition.Y + i * FirstZoneSize.Height - Convert.ToInt32(i * offset));
|
||||
Zone newDriverZone = new Zone(MainZoneImage, new Rectangle(tmpPos, FirstZoneSize), "DriverZone");
|
||||
zonesToAdd.Add(newDriverZone);
|
||||
zonesImages.Add(newDriverZone.ZoneImage);
|
||||
|
||||
newDriverZone.ZoneImage.Save("Driver"+i+".png");
|
||||
}
|
||||
|
||||
//Parallel.For(0, NUMBER_OF_DRIVERS, i =>
|
||||
for (int i = 0; i < NUMBER_OF_DRIVERS; i++)
|
||||
{
|
||||
Zone newDriverZone = zonesToAdd[(int)i];
|
||||
Bitmap zoneImg = zonesImages[(int)i];
|
||||
|
||||
newDriverZone.AddWindow(new DriverPositionWindow(zoneImg, new Rectangle(driverPositionPosition, driverPositionArea),LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverGapToLeaderWindow(zoneImg, new Rectangle(driverGapToLeaderPosition, driverGapToLeaderArea), LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverLapTimeWindow(zoneImg, new Rectangle(driverLapTimePosition, driverLapTimeArea), LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverDrsWindow(zoneImg, new Rectangle(driverDrsPosition, driverDrsArea), LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverTyresWindow(zoneImg, new Rectangle(driverTyresPosition, driverTyresArea), LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverNameWindow(zoneImg, new Rectangle(driverNamePosition, driverNameArea), LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverSectorWindow(zoneImg, new Rectangle(driverSector1Position, driverSector1Area),1, LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverSectorWindow(zoneImg, new Rectangle(driverSector2Position, driverSector2Area),2, LoadOCR));
|
||||
newDriverZone.AddWindow(new DriverSectorWindow(zoneImg, new Rectangle(driverSector3Position, driverSector3Area),3, LoadOCR));
|
||||
|
||||
mainZone.AddZone(newDriverZone);
|
||||
}//);
|
||||
//MessageBox.Show("We have a main zone with " + MainZone.Zones.Count() + " Driver zones with " + MainZone.Zones[4].Windows.Count() + " windows each and we have " + Drivers.Count + " drivers");
|
||||
mainZones.Add(mainZone);
|
||||
mainZone.AddZone(driverZone);
|
||||
}
|
||||
|
||||
JsonElement driversElement = main.GetProperty("Drivers");
|
||||
foreach (JsonElement driverElement in driversElement.EnumerateArray())
|
||||
{
|
||||
string driverName = driverElement.GetString();
|
||||
driverListToFill.Add(driverName);
|
||||
Storage.AddDriver(driverName);
|
||||
}
|
||||
|
||||
mainZones.Add(mainZone);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
@@ -148,42 +213,85 @@ namespace Test_Merge
|
||||
{
|
||||
MessageBox.Show("Invalid JSON format: " + ex.Message);
|
||||
}
|
||||
int driverID = 0;
|
||||
foreach (Zone z in mainZones[0].Zones)
|
||||
{
|
||||
driverID++;
|
||||
z.ZoneImage.Save("LoadedDriver" + driverID + ".png");
|
||||
}
|
||||
return mainZones;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Method that calls all the zones and windows to get the content they can find on the image to display them
|
||||
/// </summary>
|
||||
/// <param name="idImage">The id of the image we are working with</param>
|
||||
/// <returns>a string representation of all the returns</returns>
|
||||
public async Task<string> Decode(List<Zone> mainZones,List<string> drivers)
|
||||
{
|
||||
string result = "";
|
||||
public List<DriverData> Decode(List<Zone> mainZones, List<string> drivers)
|
||||
{
|
||||
List<DriverData> mainResults = new List<DriverData>();
|
||||
|
||||
//Decode
|
||||
for (int mainZoneId = 0; mainZoneId < mainZones.Count; mainZoneId++)
|
||||
{
|
||||
switch (mainZoneId)
|
||||
{
|
||||
case 0:
|
||||
//object lockObject = new object();
|
||||
//Main Zone
|
||||
foreach (Zone z in mainZones[mainZoneId].Zones)
|
||||
Parallel.For(0, mainZones[mainZoneId].Zones.Count, async i =>
|
||||
//for (int i = 0; i < mainZones[mainZoneId].Zones.Count; i++)
|
||||
{
|
||||
mainResults.Add(await z.Decode(Drivers));
|
||||
}
|
||||
DriverData data = mainZones[mainZoneId].Zones[i].Decode(new List<string>(drivers));
|
||||
mainResults.Add(data);
|
||||
DriverDataLogs[i].Add(data);
|
||||
|
||||
if (data.Position != -1 && DriverDataLogs[i].Count > 1)
|
||||
{
|
||||
//Tries to fix the tyres
|
||||
if (data.CurrentTyre.NumberOfLaps > DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps + 3)
|
||||
data.CurrentTyre.NumberOfLaps = DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps + 1;
|
||||
|
||||
//Checking if its a new lap
|
||||
//If the third sector is filled but it was'nt the last time, then it means that a new Lap has been started
|
||||
//Lap detection can be f***ed if the OCR takes so much time that an entire sector can be raced without us knowing.
|
||||
if (
|
||||
DriverDataLogs[i][DriverDataLogs[i].Count - 1].Sector3 != 0
|
||||
&& DriverDataLogs[i][DriverDataLogs[i].Count - 2].Sector3 == 0
|
||||
&& DriverDataLogs[i][DriverDataLogs[i].Count - 2].Position != -1
|
||||
&& DriverDataLogs[i][DriverDataLogs[i].Count - 1].Position != -1)
|
||||
{
|
||||
DriverData stats = new DriverData();
|
||||
stats = DriverDataLogs[i][DriverDataLogs[i].Count - 1];
|
||||
DriverLaps[i]++;
|
||||
Storage.AddDriverStat(stats, DriverLaps[i]);
|
||||
}
|
||||
//Checking if its a pitstop
|
||||
//Forget this the best way to know if a tyre has been changed is if the number of laps is zero
|
||||
if (data.CurrentTyre.Coumpound != Tyre.Type.Undefined && data.CurrentTyre.NumberOfLaps == 0 && DriverDataLogs[i][DriverDataLogs[i].Count - 2].CurrentTyre.NumberOfLaps != 0)
|
||||
{
|
||||
Storage.AddPitstop(data.Name, DriverLaps[i] - 1, data.CurrentTyre.Coumpound.ToString());
|
||||
//Driver laps -1 because it would take AT LEAST one lap for this program to detect a pitstop
|
||||
}
|
||||
}
|
||||
DriverDataLogs[i].Add(data);
|
||||
});
|
||||
break;
|
||||
//Next there could be a Title Zone and TrackInfoZone
|
||||
}
|
||||
}
|
||||
|
||||
//Display
|
||||
foreach (DriverData driver in mainResults)
|
||||
//mainResults = mainResults.OrderBy(driver => driver.Position >= 0).ThenBy(driver => driver.Position).ToList();
|
||||
mainResults = mainResults.OrderBy(driver => driver.Position).ToList();
|
||||
return mainResults;
|
||||
}
|
||||
/// <summary>
|
||||
/// Changes the image in all of the zones wich then will do the same for theyre own subzones and windows
|
||||
/// </summary>
|
||||
/// <param name="Image">The new Image from the F1TV data channel</param>
|
||||
public void ChangeImage(Bitmap Image)
|
||||
{
|
||||
foreach (Zone z in MainZones)
|
||||
{
|
||||
result += driver.ToString();
|
||||
result += Environment.NewLine;
|
||||
z.Image = Image;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// Method that can be used to convert an amount of miliseconds into a more readable human form
|
||||
@@ -204,7 +312,7 @@ namespace Test_Merge
|
||||
/// </summary>
|
||||
/// <param name="idImage">the #id of the image we are working with</param>
|
||||
/// <returns>the drawed bitmap</returns>
|
||||
public Bitmap Draw(Bitmap image,List<Zone> mainZones)
|
||||
public Bitmap Draw(Bitmap image, List<Zone> mainZones)
|
||||
{
|
||||
|
||||
Graphics g = Graphics.FromImage(image);
|
||||
|
||||
Reference in New Issue
Block a user