Fixed the calibration and added the JSON serialising

This commit is contained in:
2023-05-05 15:00:39 +02:00
parent 2c2239427c
commit 16be9bf7ef
13 changed files with 205 additions and 72 deletions

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;
using System.IO;
namespace Test_Merge
{
@@ -13,12 +14,63 @@ namespace Test_Merge
public Zone MainZone;
public const int NUMBER_OF_DRIVERS = 20;
public const int NUMBER_OF_ZONES = 9;
public const string CONFIGS_FOLDER_NAME = "./Presets/";
public ConfigurationTool(Bitmap fullImage,Rectangle mainZoneDimensions)
public ConfigurationTool(Bitmap fullImage, Rectangle mainZoneDimensions)
{
MainZone = new Zone(fullImage,mainZoneDimensions);
MainZone = new Zone(fullImage, mainZoneDimensions);
AutoCalibrate();
}
public void ResetMainZone()
{
MainZone.ResetZones();
}
public void ResetWindows()
{
MainZone.ResetWindows();
}
public void SaveToJson(List<string> drivers,string configName)
{
string JSON = "";
JSON += "{" + Environment.NewLine;
JSON += MainZone.ToJSON() + "," + Environment.NewLine;
JSON += "\"Drivers\":[" + Environment.NewLine;
for (int i = 0; i < drivers.Count;i++)
{
JSON += "\"" + drivers[i] + "\"";
if (i < drivers.Count - 1)
JSON += ",";
JSON += Environment.NewLine;
}
JSON += "]" + Environment.NewLine;
JSON += "}";
if(!Directory.Exists(CONFIGS_FOLDER_NAME))
Directory.CreateDirectory(CONFIGS_FOLDER_NAME);
string path = CONFIGS_FOLDER_NAME + configName;
if(File.Exists(path + ".json"))
{
//We need to create a new name
int count = 2;
while(File.Exists(path + "_" + count + ".json"))
{
count++;
}
path += "_" + count + ".json";
}
else
{
path += ".json";
}
File.WriteAllText(path,JSON);
}
public void AddWindows(List<Rectangle> rectangles)
{
foreach (Zone driverZone in MainZone.Zones)
@@ -31,39 +83,39 @@ namespace Test_Merge
{
case 1:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverPositionWindow(driverZone.ZoneImage, rectangles[i]));
driverZone.AddWindow(new DriverPositionWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 2:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverGapToLeaderWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the Gap to leader
driverZone.AddWindow(new DriverGapToLeaderWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 3:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverLapTimeWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the driver's Lap Time
driverZone.AddWindow(new DriverLapTimeWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 4:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverDrsWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the driver's DRS status
driverZone.AddWindow(new DriverDrsWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 5:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverTyresWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the driver's Tyre's informations
driverZone.AddWindow(new DriverTyresWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 6:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverNameWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the driver's Name
driverZone.AddWindow(new DriverNameWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 7:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverSectorWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the driver's First Sector
driverZone.AddWindow(new DriverSectorWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 8:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverSectorWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the driver's Second Sector
driverZone.AddWindow(new DriverSectorWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
case 9:
//First zone should be the driver's Position
driverZone.AddWindow(new DriverSectorWindow(driverZone.ZoneImage, rectangles[i]));
//First zone should be the driver's Position Sector
driverZone.AddWindow(new DriverSectorWindow(driverZone.ZoneImage, rectangles[i - 1], false));
break;
}
}

View File

@@ -13,7 +13,7 @@ namespace Test_Merge
{
private static int EmptyDrsGreenValue = -1;
private static Random rnd = new Random();
public DriverDrsWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
public DriverDrsWindow(Bitmap image, Rectangle bounds,bool generateEngine = true) : base(image, bounds,generateEngine)
{
Name = "DRS";
}

View File

@@ -9,7 +9,7 @@ namespace Test_Merge
{
internal class DriverGapToLeaderWindow:Window
{
public DriverGapToLeaderWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
public DriverGapToLeaderWindow(Bitmap image, Rectangle bounds, bool generateEngine = true) : base(image, bounds,generateEngine)
{
Name = "Gap to leader";
}

View File

@@ -9,7 +9,7 @@ namespace Test_Merge
{
internal class DriverLapTimeWindow:Window
{
public DriverLapTimeWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
public DriverLapTimeWindow(Bitmap image, Rectangle bounds, bool generateEngine = true) : base(image, bounds,generateEngine)
{
Name = "Lap time";
}

View File

@@ -10,7 +10,7 @@ namespace Test_Merge
public class DriverNameWindow : Window
{
public static Random rnd = new Random();
public DriverNameWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
public DriverNameWindow(Bitmap image, Rectangle bounds, bool generateEngine = true) : base(image, bounds,generateEngine)
{
Name = "Name";
}

View File

@@ -9,7 +9,7 @@ namespace Test_Merge
{
public class DriverPositionWindow:Window
{
public DriverPositionWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
public DriverPositionWindow(Bitmap image, Rectangle bounds, bool generateEngine = true) : base(image, bounds,generateEngine)
{
Name = "Position";
}

View File

@@ -9,7 +9,7 @@ namespace Test_Merge
{
internal class DriverSectorWindow:Window
{
public DriverSectorWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
public DriverSectorWindow(Bitmap image, Rectangle bounds, bool generateEngine = true) : base(image, bounds,generateEngine)
{
Name = "Sector 1";
}

View File

@@ -20,7 +20,7 @@ namespace Test_Merge
public static Color WET_TYRE_COLOR = Color.FromArgb(0x27, 0x60, 0xa6);
public static Color EMPTY_COLOR = Color.FromArgb(0x20, 0x20, 0x20);
public DriverTyresWindow(Bitmap image, Rectangle bounds) : base(image, bounds)
public DriverTyresWindow(Bitmap image, Rectangle bounds, bool generateEngine = true) : base(image, bounds,generateEngine)
{
Name = "Tyres";
}

View File

@@ -272,5 +272,11 @@ namespace Test_Merge
Ready = false;
Driver.Dispose();
}
public void ResetDriver()
{
Ready = false;
Driver.Dispose();
Driver = null;
}
}
}

View File

@@ -49,13 +49,14 @@
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.label8 = new System.Windows.Forms.Label();
this.lsbPresets = new System.Windows.Forms.ListBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.tbxPresetName = new System.Windows.Forms.TextBox();
this.btnSavePreset = new System.Windows.Forms.Button();
this.lblWindowsRemaining = new System.Windows.Forms.Label();
this.lblZonePointsRemaning = new System.Windows.Forms.Label();
this.lblWindowPointsRemaining = new System.Windows.Forms.Label();
this.btnCreateWindow = new System.Windows.Forms.Button();
this.btnCreatZone = new System.Windows.Forms.Button();
this.btnResetDriver = new System.Windows.Forms.Button();
this.gpbxInfos.SuspendLayout();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
@@ -203,12 +204,13 @@
//
// groupBox2
//
this.groupBox2.Controls.Add(this.btnResetDriver);
this.groupBox2.Controls.Add(this.btnRefresh);
this.groupBox2.Controls.Add(this.pbxMain);
this.groupBox2.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F);
this.groupBox2.Location = new System.Drawing.Point(283, 55);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(906, 608);
this.groupBox2.Size = new System.Drawing.Size(968, 608);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Preview";
@@ -225,13 +227,14 @@
//
// pbxMain
//
this.pbxMain.Location = new System.Drawing.Point(6, 23);
this.pbxMain.Location = new System.Drawing.Point(6, 62);
this.pbxMain.Name = "pbxMain";
this.pbxMain.Size = new System.Drawing.Size(900, 579);
this.pbxMain.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pbxMain.Size = new System.Drawing.Size(950, 535);
this.pbxMain.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pbxMain.TabIndex = 0;
this.pbxMain.TabStop = false;
this.pbxMain.Click += new System.EventHandler(this.pbxMain_Click);
this.pbxMain.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pbxMain_MouseClick);
//
// groupBox3
//
@@ -239,7 +242,7 @@
this.groupBox3.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F);
this.groupBox3.Location = new System.Drawing.Point(18, 669);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(1390, 83);
this.groupBox3.Size = new System.Drawing.Size(1452, 83);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "DriverZonePreview";
@@ -248,17 +251,18 @@
//
this.pbxDriverZone.Location = new System.Drawing.Point(10, 27);
this.pbxDriverZone.Name = "pbxDriverZone";
this.pbxDriverZone.Size = new System.Drawing.Size(1374, 50);
this.pbxDriverZone.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pbxDriverZone.Size = new System.Drawing.Size(1436, 50);
this.pbxDriverZone.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pbxDriverZone.TabIndex = 0;
this.pbxDriverZone.TabStop = false;
this.pbxDriverZone.Click += new System.EventHandler(this.pbxDriverZone_Click);
this.pbxDriverZone.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pbxDriverZone_MouseClick);
//
// groupBox4
//
this.groupBox4.Controls.Add(this.label8);
this.groupBox4.Controls.Add(this.lsbPresets);
this.groupBox4.Controls.Add(this.textBox5);
this.groupBox4.Controls.Add(this.tbxPresetName);
this.groupBox4.Controls.Add(this.btnSavePreset);
this.groupBox4.Controls.Add(this.lblWindowsRemaining);
this.groupBox4.Controls.Add(this.lblZonePointsRemaning);
@@ -266,7 +270,7 @@
this.groupBox4.Controls.Add(this.btnCreateWindow);
this.groupBox4.Controls.Add(this.btnCreatZone);
this.groupBox4.Font = new System.Drawing.Font("Microsoft YaHei UI", 10F);
this.groupBox4.Location = new System.Drawing.Point(1195, 55);
this.groupBox4.Location = new System.Drawing.Point(1257, 55);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(213, 608);
this.groupBox4.TabIndex = 6;
@@ -291,13 +295,13 @@
this.lsbPresets.Size = new System.Drawing.Size(197, 257);
this.lsbPresets.TabIndex = 8;
//
// textBox5
// tbxPresetName
//
this.textBox5.Font = new System.Drawing.Font("Microsoft YaHei UI", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBox5.Location = new System.Drawing.Point(10, 216);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(197, 29);
this.textBox5.TabIndex = 7;
this.tbxPresetName.Font = new System.Drawing.Font("Microsoft YaHei UI", 10.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.tbxPresetName.Location = new System.Drawing.Point(10, 216);
this.tbxPresetName.Name = "tbxPresetName";
this.tbxPresetName.Size = new System.Drawing.Size(197, 29);
this.tbxPresetName.TabIndex = 7;
//
// btnSavePreset
//
@@ -307,6 +311,7 @@
this.btnSavePreset.TabIndex = 7;
this.btnSavePreset.Text = "Save as new preset";
this.btnSavePreset.UseVisualStyleBackColor = true;
this.btnSavePreset.Click += new System.EventHandler(this.btnSavePreset_Click);
//
// lblWindowsRemaining
//
@@ -355,11 +360,21 @@
this.btnCreatZone.UseVisualStyleBackColor = true;
this.btnCreatZone.Click += new System.EventHandler(this.btnCreatZone_Click);
//
// btnResetDriver
//
this.btnResetDriver.Location = new System.Drawing.Point(113, 22);
this.btnResetDriver.Name = "btnResetDriver";
this.btnResetDriver.Size = new System.Drawing.Size(101, 33);
this.btnResetDriver.TabIndex = 2;
this.btnResetDriver.Text = "Reset";
this.btnResetDriver.UseVisualStyleBackColor = true;
this.btnResetDriver.Click += new System.EventHandler(this.btnResetDriver_Click);
//
// Settings
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 19F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1420, 760);
this.ClientSize = new System.Drawing.Size(1478, 760);
this.Controls.Add(this.groupBox4);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
@@ -410,12 +425,13 @@
private System.Windows.Forms.Button btnCreateWindow;
private System.Windows.Forms.Button btnCreatZone;
private System.Windows.Forms.ListBox lsbPresets;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox tbxPresetName;
private System.Windows.Forms.Button btnSavePreset;
private System.Windows.Forms.Label lblWindowsRemaining;
private System.Windows.Forms.Label lblZonePointsRemaning;
private System.Windows.Forms.Label lblWindowPointsRemaining;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Button btnRefresh;
private System.Windows.Forms.Button btnResetDriver;
}
}

View File

@@ -7,6 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Test_Merge
{
@@ -49,6 +50,11 @@ namespace Test_Merge
lsbDrivers.DataSource = null;
lsbDrivers.DataSource = DriverList;
if (Directory.Exists(ConfigurationTool.CONFIGS_FOLDER_NAME))
{
lsbPresets.DataSource = null;
lsbPresets.DataSource = Directory.GetFiles(ConfigurationTool.CONFIGS_FOLDER_NAME);
}
if (CreatingZone)
{
if (ZoneP1 == new Point(-1, -1))
@@ -85,7 +91,8 @@ namespace Test_Merge
if (Config != null)
{
pbxMain.Image = Config.MainZone.Draw();
pbxDriverZone.Image = Config.MainZone.Zones[0].ZoneImage;
if(Config.MainZone.Zones.Count > 0)
pbxDriverZone.Image = Config.MainZone.Zones[0].Draw();
}
}
private void CreateNewZone(Point p1, Point p2)
@@ -129,6 +136,7 @@ namespace Test_Merge
{
string newDriver = tbxDriverName.Text;
DriverList.Add(newDriver);
tbxDriverName.Text = "";
RefreshUI();
}
@@ -150,6 +158,10 @@ namespace Test_Merge
else
{
CreatingZone = true;
if (Config != null)
Config.ResetMainZone();
if (CreatingWindow)
SwitchWindowCreation();
@@ -176,6 +188,9 @@ namespace Test_Merge
{
CreatingWindow = true;
if (Config != null)
Config.ResetWindows();
if (CreatingZone)
SwitchZoneCreation();
@@ -194,19 +209,17 @@ namespace Test_Merge
{
SwitchWindowCreation();
}
private void pbxMain_Click(object sender, EventArgs e)
private void pbxMain_MouseClick(object sender, MouseEventArgs e)
{
if (CreatingZone && pbxMain.Image != null)
{
Point coordinates = pbxMain.PointToClient(new Point(MousePosition.X, MousePosition.Y));
//Point coordinates = pbxMain.PointToClient(new Point(MousePosition.X, MousePosition.Y));
Point coordinates = e.Location;
float xOffset = (float)pbxMain.Image.Width / (float)pbxMain.Width;
float yOffset = (float)pbxMain.Image.Height / (float)pbxMain.Height;
Point newPoint = new Point(Convert.ToInt32((float)coordinates.X * xOffset), Convert.ToInt32((float)coordinates.Y * yOffset));
//Point newPoint = new Point(coordinates.X - pbxMain.Left, coordinates.Y - pbxMain.Top);
//newPoint = new Point(Convert.ToInt32((float)newPoint.X * xOffset),Convert.ToInt32((float)newPoint.Y * yOffset));
MessageBox.Show("Coordinates" + Environment.NewLine + "Old : " + coordinates.ToString() + Environment.NewLine + "New : " + newPoint.ToString());
//MessageBox.Show("Coordinates" + Environment.NewLine + "Old : " + coordinates.ToString() + Environment.NewLine + "New : " + newPoint.ToString());
if (ZoneP1 == new Point(-1, -1))
{
@@ -215,31 +228,21 @@ namespace Test_Merge
else
{
ZoneP2 = newPoint;
/*
//Correction
if(ZoneP1.Y > ZoneP2.Y)
{
ZoneP1 = new Point(ZoneP1.X,ZoneP1.Y + pbxMain.Top);
}
else
{
ZoneP2 = new Point(ZoneP2.X, ZoneP2.Y - pbxMain.Top);
}
*/
CreateNewZone(ZoneP1, ZoneP2);
SwitchZoneCreation();
}
RefreshUI();
}
}
private void pbxDriverZone_Click(object sender, EventArgs e)
private void pbxMain_Click(object sender, EventArgs e)
{
//Not the right one to use visibly
}
private void pbxDriverZone_MouseClick(object sender, MouseEventArgs e)
{
if (CreatingWindow && pbxDriverZone.Image != null)
{
Point coordinates = pbxDriverZone.PointToClient(new Point(MousePosition.X, MousePosition.Y));
Point coordinates = e.Location;
float xOffset = (float)pbxDriverZone.Image.Width / (float)pbxDriverZone.Width;
float yOffset = (float)pbxDriverZone.Image.Height / (float)pbxDriverZone.Height;
@@ -262,12 +265,19 @@ namespace Test_Merge
}
else
{
WindowP1 = new Point(WindowP1.X, 0);
WindowP2 = new Point(WindowP2.X, pbxDriverZone.Image.Height);
CreateWindows(WindowsToAdd);
SwitchWindowCreation();
}
}
RefreshUI();
}
}
private void pbxDriverZone_Click(object sender, EventArgs e)
{
//Not the right one to use visibly
}
private Rectangle CreateAbsoluteRectangle(Point p1, Point p2)
{
Point newP1 = new Point();
@@ -357,5 +367,23 @@ namespace Test_Merge
Emulator.Stop();
}
}
private void btnResetDriver_Click(object sender, EventArgs e)
{
if (Emulator != null)
{
Emulator.ResetDriver();
}
}
private void btnSavePreset_Click(object sender, EventArgs e)
{
string presetName = tbxPresetName.Text;
if (Config != null)
{
Config.SaveToJson(DriverList,presetName);
}
RefreshUI();
}
}
}

View File

@@ -35,12 +35,15 @@ namespace Test_Merge
return sample;
}
}
public Window(Bitmap image, Rectangle bounds)
public Window(Bitmap image, Rectangle bounds, bool generateEngine = true)
{
Image = image;
Bounds = bounds;
Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
Engine.DefaultPageSegMode = PageSegMode.SingleLine;
if (generateEngine)
{
Engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
Engine.DefaultPageSegMode = PageSegMode.SingleLine;
}
}
/// <summary>
/// Method that will have to be used by the childrens to let the model make them decode the images they have

View File

@@ -111,8 +111,24 @@ namespace Test_Merge
}
public virtual Bitmap Draw()
{
Image img = Image;
Bitmap img;
//If its the main zone we want to see everything
if (Zones.Count > 0)
{
img = Image;
}
else
{
img = ZoneImage;
}
Graphics g = Graphics.FromImage(img);
//If its the main zone we need to visualize the Zone bounds displayed
if (Zones.Count > 0)
g.DrawRectangle(new Pen(Brushes.Violet,5),Bounds);
foreach (Zone z in Zones)
{
Rectangle newBounds = new Rectangle(z.Bounds.X,z.Bounds.Y + Bounds.Y,z.Bounds.Width,z.Bounds.Height);
@@ -122,7 +138,19 @@ namespace Test_Merge
{
g.DrawRectangle(Pens.Blue, w.Bounds);
}
return (Bitmap)img;
return img;
}
public void ResetZones()
{
Zones.Clear();
}
public void ResetWindows()
{
foreach (Zone z in Zones)
{
z.ResetWindows();
}
Windows.Clear();
}
public virtual string ToJSON()
{