Now the JSON is fully read and the zones/windows are fully reconstructed
This commit is contained in:
Generated
+1
@@ -41,6 +41,7 @@
|
|||||||
this.pbxImage.Location = new System.Drawing.Point(12, 12);
|
this.pbxImage.Location = new System.Drawing.Point(12, 12);
|
||||||
this.pbxImage.Name = "pbxImage";
|
this.pbxImage.Name = "pbxImage";
|
||||||
this.pbxImage.Size = new System.Drawing.Size(1000, 600);
|
this.pbxImage.Size = new System.Drawing.Size(1000, 600);
|
||||||
|
this.pbxImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
|
||||||
this.pbxImage.TabIndex = 0;
|
this.pbxImage.TabIndex = 0;
|
||||||
this.pbxImage.TabStop = false;
|
this.pbxImage.TabStop = false;
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ namespace OCR_Decode
|
|||||||
private void Form1_Load(object sender, EventArgs e)
|
private void Form1_Load(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Reader = new Reader(CONFIG_FILE,TEMPORARY_IMAGE_FOLDER);
|
Reader = new Reader(CONFIG_FILE,TEMPORARY_IMAGE_FOLDER);
|
||||||
|
pbxImage.Image = Reader.Draw(9);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+83
-32
@@ -5,7 +5,6 @@ using System.Text;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
//using Newtonsoft.Json;
|
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
@@ -16,12 +15,13 @@ namespace OCR_Decode
|
|||||||
private string _configFile;
|
private string _configFile;
|
||||||
private string _imagesFolder;
|
private string _imagesFolder;
|
||||||
private List<string> _drivers;
|
private List<string> _drivers;
|
||||||
private List<Zone> MainZones;
|
private List<Zone> _mainZones;
|
||||||
private Bitmap FullImage;
|
private Bitmap FullImage;
|
||||||
private int ImageNumber;
|
private int ImageNumber;
|
||||||
public string ConfigFile { get => _configFile; private set => _configFile = value; }
|
public string ConfigFile { get => _configFile; private set => _configFile = value; }
|
||||||
public string ImagesFolder { get => _imagesFolder; private set => _imagesFolder = value; }
|
public string ImagesFolder { get => _imagesFolder; private set => _imagesFolder = value; }
|
||||||
public List<string> Drivers { get => _drivers; private set => _drivers = value; }
|
public List<string> Drivers { get => _drivers; private set => _drivers = value; }
|
||||||
|
public List<Zone> MainZones { get => _mainZones; set => _mainZones = value; }
|
||||||
|
|
||||||
const string DEFAULT_IMAGE_NAME = "screen_";
|
const string DEFAULT_IMAGE_NAME = "screen_";
|
||||||
const int NUMBER_OF_DRIVERS = 20;
|
const int NUMBER_OF_DRIVERS = 20;
|
||||||
@@ -35,6 +35,7 @@ namespace OCR_Decode
|
|||||||
}
|
}
|
||||||
private void Load()
|
private void Load()
|
||||||
{
|
{
|
||||||
|
MainZones = new List<Zone>();
|
||||||
FullImage = (Bitmap)Image.FromFile(ImagesFolder + DEFAULT_IMAGE_NAME + ImageNumber + ".png");
|
FullImage = (Bitmap)Image.FromFile(ImagesFolder + DEFAULT_IMAGE_NAME + ImageNumber + ".png");
|
||||||
Zone MainZone;
|
Zone MainZone;
|
||||||
try
|
try
|
||||||
@@ -44,6 +45,14 @@ namespace OCR_Decode
|
|||||||
var jsonText = streamReader.ReadToEnd();
|
var jsonText = streamReader.ReadToEnd();
|
||||||
var jsonDocument = JsonDocument.Parse(jsonText);
|
var jsonDocument = JsonDocument.Parse(jsonText);
|
||||||
|
|
||||||
|
var driversNames = jsonDocument.RootElement.GetProperty("Drivers");
|
||||||
|
Drivers = new List<string>();
|
||||||
|
|
||||||
|
foreach (var nameElement in driversNames.EnumerateArray())
|
||||||
|
{
|
||||||
|
Drivers.Add(nameElement.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
var mainProperty = jsonDocument.RootElement.GetProperty("Main");
|
var mainProperty = jsonDocument.RootElement.GetProperty("Main");
|
||||||
Point MainPosition = new Point(mainProperty.GetProperty("x").GetInt32(), mainProperty.GetProperty("y").GetInt32());
|
Point MainPosition = new Point(mainProperty.GetProperty("x").GetInt32(), mainProperty.GetProperty("y").GetInt32());
|
||||||
Size MainSize = new Size(mainProperty.GetProperty("width").GetInt32(), mainProperty.GetProperty("height").GetInt32());
|
Size MainSize = new Size(mainProperty.GetProperty("width").GetInt32(), mainProperty.GetProperty("height").GetInt32());
|
||||||
@@ -58,9 +67,6 @@ namespace OCR_Decode
|
|||||||
|
|
||||||
var windows = driverZone.GetProperty("Windows");
|
var windows = driverZone.GetProperty("Windows");
|
||||||
|
|
||||||
// Add the position Window
|
|
||||||
// Add the Gap to leader window
|
|
||||||
//Etc...
|
|
||||||
//var driverPosition = windows.GetProperty("Position");
|
//var driverPosition = windows.GetProperty("Position");
|
||||||
var driverPosition = windows[0].GetProperty("Position");
|
var driverPosition = windows[0].GetProperty("Position");
|
||||||
Size driverPositionArea = new Size(driverPosition.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
Size driverPositionArea = new Size(driverPosition.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||||
@@ -99,40 +105,28 @@ namespace OCR_Decode
|
|||||||
Size driverSector3Area = new Size(driverSector3.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
Size driverSector3Area = new Size(driverSector3.GetProperty("width").GetInt32(), FirstZoneSize.Height);
|
||||||
Point driverSector3Position = new Point(driverSector3.GetProperty("x").GetInt32(), driverSector3.GetProperty("y").GetInt32());
|
Point driverSector3Position = new Point(driverSector3.GetProperty("x").GetInt32(), driverSector3.GetProperty("y").GetInt32());
|
||||||
|
|
||||||
for (int i = 1; i <= NUMBER_OF_DRIVERS; i++)
|
float offset = (((float)MainZone.ZoneImage.Height - (float)(Drivers.Count * FirstZoneSize.Height)) / (float)Drivers.Count);
|
||||||
|
|
||||||
|
for (int i = 0; i < NUMBER_OF_DRIVERS; i++)
|
||||||
{
|
{
|
||||||
Point tmpPos = new Point(FirstZonePosition.X, i * FirstZonePosition.Y);
|
Point tmpPos = new Point(0,FirstZonePosition.Y + i * FirstZoneSize.Height - Convert.ToInt32(i*offset) /*- (i* (FirstZoneSize.Height / 32))*/);
|
||||||
Zone newDriverZone = new Zone(MainZone.ZoneImage, new Rectangle(tmpPos, FirstZoneSize));
|
Zone newDriverZone = new Zone(MainZone.ZoneImage, new Rectangle(tmpPos, FirstZoneSize));
|
||||||
|
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverPositionPosition, driverPositionArea)));
|
newDriverZone.AddWindow(new DriverPositionWindow(newDriverZone.ZoneImage, new Rectangle(driverPositionPosition, driverPositionArea)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverGapToLeaderPosition, driverGapToLeaderArea)));
|
newDriverZone.AddWindow(new DriverGapToLeaderWindow(newDriverZone.ZoneImage, new Rectangle(driverGapToLeaderPosition, driverGapToLeaderArea)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverLapTimePosition, driverLapTimeArea)));
|
newDriverZone.AddWindow(new DriverLapTimeWindow(newDriverZone.ZoneImage, new Rectangle(driverLapTimePosition, driverLapTimeArea)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverDrsPosition, driverDrsArea)));
|
newDriverZone.AddWindow(new DriverDrsWindow(newDriverZone.ZoneImage, new Rectangle(driverDrsPosition, driverDrsArea)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverTyresPosition, driverTyresArea)));
|
newDriverZone.AddWindow(new DriverTyresWindow(newDriverZone.ZoneImage, new Rectangle(driverTyresPosition, driverTyresArea)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverNamePosition, driverNameArea)));
|
newDriverZone.AddWindow(new DriverNameWindow(newDriverZone.ZoneImage, new Rectangle(driverNamePosition, driverNameArea)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverSector1Position, driverSector1Area)));
|
newDriverZone.AddWindow(new DriverSector1Window(newDriverZone.ZoneImage, new Rectangle(driverSector1Position, driverSector1Area)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverSector2Position, driverSector2Area)));
|
newDriverZone.AddWindow(new DriverSector2Window(newDriverZone.ZoneImage, new Rectangle(driverSector2Position, driverSector2Area)));
|
||||||
newDriverZone.AddWindow(new Window(newDriverZone.ZoneImage, new Rectangle(driverSector3Position, driverSector3Area)));
|
newDriverZone.AddWindow(new DriverSector3Window(newDriverZone.ZoneImage, new Rectangle(driverSector3Position, driverSector3Area)));
|
||||||
|
|
||||||
MainZone.AddZone(newDriverZone);
|
MainZone.AddZone(newDriverZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
var driversNames = jsonDocument.RootElement.GetProperty("Drivers");
|
//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");
|
||||||
Drivers = new List<string>();
|
MainZones.Add(MainZone);
|
||||||
|
|
||||||
foreach (var nameElement in driversNames.EnumerateArray())
|
|
||||||
{
|
|
||||||
Drivers.Add(nameElement.GetString());
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
/*
|
|
||||||
foreach (var zoneElement in zones.EnumerateArray())
|
|
||||||
{
|
|
||||||
That would be used if there were multiple DriverZones, wich is not the case because we only need one
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
@@ -144,6 +138,63 @@ namespace OCR_Decode
|
|||||||
MessageBox.Show("Invalid JSON format: " + ex.Message);
|
MessageBox.Show("Invalid JSON format: " + ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public string Decode(int idImage)
|
||||||
|
{
|
||||||
|
string result = "";
|
||||||
|
Bitmap imageToDecode;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
imageToDecode = (Bitmap)Image.FromFile(ImagesFolder + DEFAULT_IMAGE_NAME + idImage + ".png");
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
MessageBox.Show("Image could not be found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public Bitmap Draw(int idImage)
|
||||||
|
{
|
||||||
|
string debugDumpFolder = @"C:\Users\Moi\Desktop\imgDump\Decode\";
|
||||||
|
Bitmap result;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = (Bitmap)Image.FromFile(ImagesFolder + DEFAULT_IMAGE_NAME + idImage + ".png");
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
MessageBox.Show("Image could not be found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Graphics g = Graphics.FromImage(result);
|
||||||
|
|
||||||
|
foreach (Zone z in MainZones)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
foreach (Zone zz in z.Zones)
|
||||||
|
{
|
||||||
|
string driverFolder = debugDumpFolder + "driver" + count + "\\";
|
||||||
|
if (!Directory.Exists(driverFolder))
|
||||||
|
Directory.CreateDirectory(driverFolder);
|
||||||
|
|
||||||
|
zz.ZoneImage.Save(driverFolder + "FullImage.png");
|
||||||
|
|
||||||
|
g.DrawRectangle(Pens.Red,z.Bounds);
|
||||||
|
foreach (Window w in zz.Windows)
|
||||||
|
{
|
||||||
|
w.WindowImage.Save(driverFolder + w.Name +".png");
|
||||||
|
g.DrawRectangle(Pens.Blue,new Rectangle(z.Bounds.X + zz.Bounds.X,z.Bounds.Y + zz.Bounds.Y,zz.Bounds.Width,zz.Bounds.Height));
|
||||||
|
}
|
||||||
|
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user