Reworked the JSON serialisation

This commit is contained in:
2023-05-11 14:23:44 +02:00
parent 57028f1fc9
commit 025c2f8d61
6 changed files with 219 additions and 191 deletions
+54 -13
View File
@@ -12,6 +12,8 @@ using System.Text;
using System.Threading.Tasks;
using Tesseract;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace Test_Merge
{
@@ -50,25 +52,64 @@ namespace Test_Merge
{
string JSON = "";
JSON += "{" + Environment.NewLine;
JSON += MainZone.ToJSON() + "," + Environment.NewLine;
JSON += "\"Drivers\":[" + Environment.NewLine;
JsonObject jsonFileObject = new JsonObject();
for (int i = 0; i < drivers.Count; i++)
//Creating the mainZone object
JsonObject mainZoneObject = new JsonObject();
mainZoneObject.Add("x",MainZone.Bounds.X);
mainZoneObject.Add("y",MainZone.Bounds.Y);
mainZoneObject.Add("width",MainZone.Bounds.Width);
mainZoneObject.Add("height",MainZone.Bounds.Height);
JsonArray driverZonesArray = new JsonArray();
int DriverID = 0;
foreach (Zone driverZone in MainZone.Zones)
{
JSON += "\"" + drivers[i] + "\"";
if (i < drivers.Count - 1)
JSON += ",";
JSON += Environment.NewLine;
DriverID++;
JsonObject driverZoneObject = new JsonObject();
driverZoneObject.Add("name","Driver"+DriverID);
driverZoneObject.Add("x", driverZone.Bounds.X);
driverZoneObject.Add("y", driverZone.Bounds.Y);
driverZoneObject.Add("width", driverZone.Bounds.Width);
driverZoneObject.Add("height", driverZone.Bounds.Height);
JsonArray windowsArray = new JsonArray();
JsonObject windowObject = new JsonObject();
foreach (Window window in driverZone.Windows)
{
windowObject.Add(window.Name, new JsonObject {
{ "x", window.Bounds.X },
{ "y", window.Bounds.Y },
{ "width", window.Bounds.Width },
{ "height", window.Bounds.Height }
});
}
windowsArray.Add(windowObject);
driverZoneObject.Add("Windows",windowsArray);
driverZonesArray.Add(driverZoneObject);
}
JSON += "]" + Environment.NewLine;
mainZoneObject.Add("DriverZones",driverZonesArray);
JSON += "}";
JsonArray driversArray = new JsonArray();
if (!Directory.Exists(CONFIGS_FOLDER_NAME))
Directory.CreateDirectory(CONFIGS_FOLDER_NAME);
foreach (string driver in drivers)
{
driversArray.Add(driver);
}
mainZoneObject.Add("Drivers",driversArray);
jsonFileObject.Add("Main",mainZoneObject);
JSON = jsonFileObject.ToString();
//Saving the file
string path = CONFIGS_FOLDER_NAME + configName;
if (File.Exists(path + ".json"))
@@ -204,7 +245,7 @@ namespace Test_Merge
Zone driverZone = new Zone(MainZone.ZoneImage, windowRectangle, "DriverZone");
MainZone.AddZone(driverZone);
driverZone.ZoneImage.Save("Driver" + i+".png");
//driverZone.ZoneImage.Save("Driver" + i+".png");
i++;
}
}
+3 -3
View File
@@ -40,9 +40,9 @@ namespace Test_Merge
string configFile = "./Presets/Clean_2023.json";
string gpUrl = "https://f1tv.formula1.com/detail/1000006688/2023-azerbaijan-grand-prix?action=play";
Emulator = new F1TVEmulator(gpUrl);
await Emulator.Start();
Reader = new Reader(configFile,Emulator.Screenshot(),true);
//Emulator = new F1TVEmulator(gpUrl);
//await Emulator.Start();
//Reader = new Reader(configFile,Emulator.Screenshot(),true);
}
private void btnUpdate_Click(object sender, EventArgs e)
+151 -96
View File
@@ -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,107 +38,156 @@ namespace Test_Merge
try
{
using (var streamReader = new StreamReader(configFilePath))
{
var jsonText = streamReader.ReadToEnd();
var jsonDocument = JsonDocument.Parse(jsonText);
string jsonString = File.ReadAllText(configFilePath);
var driversNames = jsonDocument.RootElement.GetProperty("Drivers");
JsonDocument document = JsonDocument.Parse(jsonString);
JsonElement root = document.RootElement;
mainZones = new List<Zone>();
driverListToFill = new List<string>();
foreach (var nameElement in driversNames.EnumerateArray())
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())
{
driverListToFill.Add(nameElement.GetString());
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();
Zone driverZone = new Zone(fullImage, new Rectangle(driverX, driverY, driverWidth, driverHeight), "Driver");
JsonElement windowsElement = driverZoneElement.GetProperty("Windows");
//string[] windowNames = new string[] { "Position","GapToLeader","LapTime","DRS","Tyres","Name","Sector1","Sector2","Sector3" };
foreach (JsonElement windowElement in windowsElement.EnumerateArray())
{
//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);
}
mainZone.AddZone(driverZone);
}
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++)
JsonElement driversElement = main.GetProperty("Drivers");
foreach (JsonElement driverElement in driversElement.EnumerateArray())
{
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");
driverListToFill.Add(driverElement.GetString());
}
//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);
}
}
catch (IOException ex)
{
MessageBox.Show("Error reading JSON file: " + ex.Message);
@@ -147,6 +196,12 @@ 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>
@@ -154,7 +209,7 @@ namespace Test_Merge
/// </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);
+5 -5
View File
@@ -36,7 +36,7 @@ namespace Test_Merge
public string GrandPrixName { get => _grandPrixName; private set => _grandPrixName = value; }
public int GrandPrixYear { get => _grandPrixYear; private set => _grandPrixYear = value; }
public List<string> DriverList { get => _driverList; private set => _driverList = value; }
public string SelectedConfigFile { get => _selectedConfigFile;private set => _selectedConfigFile = value; }
public string SelectedConfigFile { get => _selectedConfigFile; private set => _selectedConfigFile = value; }
public Settings()
{
@@ -94,7 +94,7 @@ namespace Test_Merge
if (Config != null)
{
pbxMain.Image = Config.MainZone.Draw();
if(Config.MainZone.Zones.Count > 0)
if (Config.MainZone.Zones.Count > 0)
pbxDriverZone.Image = Config.MainZone.Zones[0].Draw();
}
}
@@ -384,7 +384,7 @@ namespace Test_Merge
string presetName = tbxPresetName.Text;
if (Config != null)
{
Config.SaveToJson(DriverList,presetName);
Config.SaveToJson(DriverList, presetName);
}
RefreshUI();
}
@@ -396,17 +396,17 @@ namespace Test_Merge
private void btnLoadPreset_Click(object sender, EventArgs e)
{
//MessageBox.Show(lsbPresets.SelectedIndex.ToString());
if (lsbPresets.SelectedIndex >= 0 && pbxMain.Image != null)
{
try
{
string fileName = lsbPresets.Items[lsbPresets.SelectedIndex].ToString();
Reader reader = new Reader(fileName, (Bitmap)pbxMain.Image,false);
Reader reader = new Reader(fileName, (Bitmap)pbxMain.Image, false);
//MainZones #0 is the big main zone containing driver zones
Config = new ConfigurationTool((Bitmap)pbxMain.Image, reader.MainZones[0].Bounds);
Config.MainZone = reader.MainZones[0];
DriverList = reader.Drivers;
SelectedConfigFile = fileName;
}
catch (Exception ex)
-12
View File
@@ -324,17 +324,5 @@ namespace Test_Merge
return d[string1.Length, string2.Length];
}
public virtual string ToJSON()
{
string result = "";
result += "\"" + Name + "\"" + ":{" + Environment.NewLine;
result += "\t" + "\"x\":" + Bounds.X + "," + Environment.NewLine;
result += "\t" + "\"y\":" + Bounds.Y + "," + Environment.NewLine;
result += "\t" + "\"width\":" + Bounds.Width + Environment.NewLine;
result += "}";
return result;
}
}
}
-56
View File
@@ -161,62 +161,6 @@ namespace Test_Merge
}
Windows.Clear();
}
public virtual string ToJSON()
{
string result = "";
result += "\"" + Name + "\":{" + Environment.NewLine;
result += "\t" + "\"x\":" + Bounds.X + "," + Environment.NewLine;
result += "\t" + "\"y\":" + Bounds.Y + "," + Environment.NewLine;
result += "\t" + "\"width\":" + Bounds.Width + "," + Environment.NewLine;
result += "\t" + "\"height\":" + Bounds.Height;
if (Windows.Count != 0)
{
result += "," + Environment.NewLine;
result += "\t" + "\"Windows\":[" + Environment.NewLine;
result += "\t\t{" + Environment.NewLine;
int Wcount = 0;
foreach (Window w in Windows)
{
result += "\t\t" + w.ToJSON();
Wcount++;
if (Wcount != Windows.Count)
result += ",";
}
result += "\t\t}" + Environment.NewLine;
result += "\t" + "]" + Environment.NewLine;
}
else
{
result += Environment.NewLine;
}
if (Zones.Count != 0)
{
result += "," + Environment.NewLine;
result += "\t" + "\"Zones\":[" + Environment.NewLine;
result += "\t\t{" + Environment.NewLine;
int Zcount = 0;
//foreach (Zone z in Zones)
//{
result += "\t\t" + Zones[0].ToJSON();
Zcount++;
if (Zcount != Zones.Count)
//result += ",";
//}
result += "\t\t}" + Environment.NewLine;
result += "\t" + "]" + Environment.NewLine;
}
else
{
result += Environment.NewLine;
}
result += "}";
return result;
}
/// <summary>
/// Checks if the given Rectangle fits in the current zone
/// </summary>