Reworked the JSON serialisation
This commit is contained in:
+156
-101
@@ -22,15 +22,15 @@ namespace Test_Merge
|
||||
public List<string> Drivers;
|
||||
public List<Zone> MainZones;
|
||||
|
||||
public Reader(string configFile, Bitmap image,bool loadOCR = true)
|
||||
public Reader(string configFile, Bitmap image, bool loadOCR = true)
|
||||
{
|
||||
MainZones = Load(image,configFile,ref Drivers,loadOCR);
|
||||
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 static List<Zone> Load(Bitmap image, string configFilePath, ref List<string> driverListToFill, bool LoadOCR)
|
||||
{
|
||||
List<Zone> mainZones = new List<Zone>();
|
||||
Bitmap fullImage = image;
|
||||
@@ -38,106 +38,155 @@ namespace Test_Merge
|
||||
|
||||
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(fullImage, 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())
|
||||
{
|
||||
driverListToFill.Add(driverElement.GetString());
|
||||
}
|
||||
|
||||
mainZones.Add(mainZone);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
@@ -147,14 +196,20 @@ 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)
|
||||
public async Task<string> Decode(List<Zone> mainZones, List<string> drivers)
|
||||
{
|
||||
string result = "";
|
||||
List<DriverData> mainResults = new List<DriverData>();
|
||||
@@ -172,7 +227,7 @@ namespace Test_Merge
|
||||
driverID++;
|
||||
if (driverID == 9)
|
||||
Console.WriteLine("AAAAA");
|
||||
z.ZoneImage.Save("PUTAIN_DE_PILOTE_N"+driverID+".png");
|
||||
z.ZoneImage.Save("PUTAIN_DE_PILOTE_N" + driverID + ".png");
|
||||
mainResults.Add(await z.Decode(drivers));
|
||||
}
|
||||
break;
|
||||
@@ -216,7 +271,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