diff --git a/Test_Merge/ConfigurationTool.cs b/Test_Merge/ConfigurationTool.cs index 4d51bb2..d3db62f 100644 --- a/Test_Merge/ConfigurationTool.cs +++ b/Test_Merge/ConfigurationTool.cs @@ -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++; } } diff --git a/Test_Merge/Form1.cs b/Test_Merge/Form1.cs index e4aea63..d378484 100644 --- a/Test_Merge/Form1.cs +++ b/Test_Merge/Form1.cs @@ -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) diff --git a/Test_Merge/Reader.cs b/Test_Merge/Reader.cs index e1f045c..11d7882 100644 --- a/Test_Merge/Reader.cs +++ b/Test_Merge/Reader.cs @@ -22,15 +22,15 @@ namespace Test_Merge public List Drivers; public List 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); } /// /// Method that reads the JSON config file and create all the Zones and Windows /// /// The image #id on wich you want to create the zones on - public static List Load(Bitmap image,string configFilePath,ref List driverListToFill,bool LoadOCR) + public static List Load(Bitmap image, string configFilePath, ref List driverListToFill, bool LoadOCR) { List mainZones = new List(); 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(); + driverListToFill = new List(); + + 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(); + 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 zonesToAdd = new List(); - List zonesImages = new List(); - - 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; - } + } /// /// Method that calls all the zones and windows to get the content they can find on the image to display them /// /// The id of the image we are working with /// a string representation of all the returns - public async Task Decode(List mainZones,List drivers) + public async Task Decode(List mainZones, List drivers) { string result = ""; List mainResults = new List(); @@ -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 /// /// the #id of the image we are working with /// the drawed bitmap - public Bitmap Draw(Bitmap image,List mainZones) + public Bitmap Draw(Bitmap image, List mainZones) { Graphics g = Graphics.FromImage(image); diff --git a/Test_Merge/Settings.cs b/Test_Merge/Settings.cs index c33f930..359a31d 100644 --- a/Test_Merge/Settings.cs +++ b/Test_Merge/Settings.cs @@ -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 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() { @@ -49,7 +49,7 @@ namespace Test_Merge } private void RefreshUI() { - + lsbDrivers.DataSource = null; lsbDrivers.DataSource = DriverList; @@ -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) diff --git a/Test_Merge/Window.cs b/Test_Merge/Window.cs index 6a679f9..ed642d8 100644 --- a/Test_Merge/Window.cs +++ b/Test_Merge/Window.cs @@ -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; - } } } diff --git a/Test_Merge/Zone.cs b/Test_Merge/Zone.cs index 9af124b..39df6a5 100644 --- a/Test_Merge/Zone.cs +++ b/Test_Merge/Zone.cs @@ -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; - } /// /// Checks if the given Rectangle fits in the current zone ///