I just dont want to loose all of this even tho it does not work

This commit is contained in:
2023-04-04 13:14:16 +02:00
parent b80f121299
commit f3bb302ac9
5 changed files with 211 additions and 22 deletions
+25
View File
@@ -4,6 +4,7 @@ using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tesseract;
namespace OCR_Decode
{
@@ -13,5 +14,29 @@ namespace OCR_Decode
{
Name = "Name";
}
public override object DecodePng()
{
string result = "";
Image rawData = WindowImage;
TesseractEngine engine = new TesseractEngine(TESS_DATA_FOLDER.FullName, "eng", EngineMode.Default);
var tessImage = Pix.LoadFromMemory(ImageToByte(WindowImage));
Page page = engine.Process(tessImage);
// Get the iterator for the page layout
using (var iter = page.GetIterator())
{
// Loop over the elements of the page layout
iter.Begin();
do
{
// Get the text for the current element
result += iter.GetText(PageIteratorLevel.Word);
} while (iter.Next(PageIteratorLevel.Word));
}
return result;
}
}
}
+13
View File
@@ -14,4 +14,17 @@ namespace OCR_Decode
Name = "Tyres";
}
}
struct Tyre
{
public enum Type
{
Soft,
Medium,
Hard,
Inter,
Wet
}
public int numberOfLaps;
public Type type;
}
}
+104 -15
View File
@@ -17,7 +17,6 @@ namespace OCR_Decode
private List<string> _drivers;
private List<Zone> _mainZones;
private Bitmap FullImage;
private int ImageNumber;
public string ConfigFile { get => _configFile; private set => _configFile = value; }
public string ImagesFolder { get => _imagesFolder; private set => _imagesFolder = value; }
public List<string> Drivers { get => _drivers; private set => _drivers = value; }
@@ -30,13 +29,22 @@ namespace OCR_Decode
{
ConfigFile = configFile;
ImagesFolder = imageFolder;
ImageNumber = 1;
Load();
Load(9);
}
private void Load()
private void Load(int imageNumber)
{
MainZones = new List<Zone>();
FullImage = (Bitmap)Image.FromFile(ImagesFolder + DEFAULT_IMAGE_NAME + ImageNumber + ".png");
try
{
FullImage = (Bitmap)Image.FromFile(ImagesFolder + DEFAULT_IMAGE_NAME + imageNumber + ".png");
}
catch
{
MessageBox.Show("Trouble reaching the image");
//Maybe a bit to harsh, Ill see what I can do to soft this a bit
Application.Exit();
}
Zone MainZone;
try
{
@@ -109,7 +117,7 @@ namespace OCR_Decode
for (int i = 0; i < NUMBER_OF_DRIVERS; i++)
{
Point tmpPos = new Point(0,FirstZonePosition.Y + i * FirstZoneSize.Height - Convert.ToInt32(i*offset) /*- (i* (FirstZoneSize.Height / 32))*/);
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));
newDriverZone.AddWindow(new DriverPositionWindow(newDriverZone.ZoneImage, new Rectangle(driverPositionPosition, driverPositionArea)));
@@ -138,23 +146,104 @@ namespace OCR_Decode
MessageBox.Show("Invalid JSON format: " + ex.Message);
}
}
public string Decode(int idImage)
public void ChangeImage(int imageNumber)
{
string result = "";
Bitmap imageToDecode;
Bitmap img = null;
string imagePath = ImagesFolder + DEFAULT_IMAGE_NAME + imageNumber + ".png";
try
{
imageToDecode = (Bitmap)Image.FromFile(ImagesFolder + DEFAULT_IMAGE_NAME + idImage + ".png");
img = (Bitmap)Image.FromFile(imagePath);
}
catch
{
MessageBox.Show("Image could not be found");
return null;
MessageBox.Show("Unable to reach the image at " + imagePath);
}
if (img != null)
{
FullImage = img;
foreach (Zone z in MainZones)
{
z.Image = img;
}
}
}
public string Decode(int idImage)
{
string result = "";
ChangeImage(idImage);
List<List<Object>> mainResults = new List<List<Object>>();
//Decode
foreach (Zone z in MainZones)
{
foreach (Zone zz in z.Zones)
{
mainResults.Add(zz.Decode());
}
}
//Display
foreach (List<Object> objects in mainResults)
{
for (int objId = 1; objId <= objects.Count; objId++)
{
switch (objId)
{
case 1:
//Position
result += "Position : " + (int)objects[objId] + " ";
break;
case 2:
//Gap
result += "Gap to leader : " + ConvertMsToTime((int)objects[objId]) + " ";
break;
case 3:
//LapTime
result += "Lap time : " + ConvertMsToTime((int)objects[objId]) + " ";
break;
case 4:
//DRS
result += "DRS : " + (bool)objects[objId] + "";
break;
case 5:
//Tyres
Tyre tyre = (Tyre)objects[objId];
result += "Tyre : " + tyre.type + " laps with the tyre : " + tyre.numberOfLaps + " ";
break;
case 6:
//Name
result += "Driver name : " + (string)objects[objId] + " ";
break;
case 7:
//Sector 1
result += "Sector 1 : " + ConvertMsToTime((int)objects[objId]) + " ";
break;
case 8:
//Sector 1
result += "Sector 2 : " + ConvertMsToTime((int)objects[objId]) + " ";
break;
case 9:
//Sector 1
result += "Sector 3 : " + ConvertMsToTime((int)objects[objId]) + " ";
break;
default:
result += "Unknown source : " + objects[objId].ToString();
break;
}
result += Environment.NewLine;
}
}
return result;
}
public static string ConvertMsToTime(int amountOfMs)
{
int minuts = Convert.ToInt32((float)amountOfMs / 1000f / 60f);
int seconds = Convert.ToInt32((float)minuts * 60f - (float)amountOfMs / 1000f);
int ms = amountOfMs - ((minuts * 60 * 1000) + (seconds * 1000));
return minuts + ":" + seconds + ":" + ms;
}
public Bitmap Draw(int idImage)
{
string debugDumpFolder = @"C:\Users\Moi\Desktop\imgDump\Decode\";
@@ -182,11 +271,11 @@ namespace OCR_Decode
zz.ZoneImage.Save(driverFolder + "FullImage.png");
g.DrawRectangle(Pens.Red,z.Bounds);
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));
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++;
+40 -2
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
namespace OCR_Decode
{
@@ -13,8 +14,11 @@ namespace OCR_Decode
private Bitmap _image;
private string _name;
public Rectangle Bounds { get => _bounds; private set => _bounds = value; }
public Bitmap Image { get => _image; private set => _image = value; }
public string Name { get => _name; set => _name = value; }
public Bitmap Image { get => _image; set => _image = value; }
public string Name { get => _name; protected set => _name = value; }
public static DirectoryInfo TESS_DATA_FOLDER = new DirectoryInfo(@"C:\Users\Moi\Pictures\SeleniumScreens\TessData");
public Bitmap WindowImage
{
get
@@ -30,5 +34,39 @@ namespace OCR_Decode
Image = image;
Bounds = bounds;
}
public virtual Object DecodePng()
{
return " ";
}
public static byte[] ImageToByte(Image img)
{
using (var stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}
public static Bitmap ConvertToBlackAndWhite(Bitmap inputBmp, int Treshold = 165)
{
Bitmap result = new Bitmap(inputBmp.Width, inputBmp.Height);
for (int y = 0; y < inputBmp.Height; y++)
{
for (int x = 0; x < inputBmp.Width; x++)
{
Color pixelColor = inputBmp.GetPixel(x, y);
if (pixelColor.R <= Treshold && pixelColor.G <= Treshold && pixelColor.B <= Treshold)
{
pixelColor = Color.FromArgb(0, 0, 0);
}
else
{
pixelColor = Color.FromArgb(255, 255, 255);
}
result.SetPixel(x, y, pixelColor);
}
}
return result;
}
}
}
+29 -5
View File
@@ -24,13 +24,28 @@ namespace OCR_Decode
return sample;
}
}
public Bitmap Image
{
get { return _image; }
set
{
_image = Image;
foreach (Window w in Windows)
{
w.Image = Image;
}
foreach (Zone z in Zones)
{
z.Image = Image;
}
}
}
public Rectangle Bounds { get => _bounds;protected set => _bounds = value; }
public List<Zone> Zones { get => _zones; set => _zones = value; }
public List<Window> Windows { get => _windows; set => _windows = value; }
public Bitmap Image { get => _image; set => _image = value; }
public Rectangle Bounds { get => _bounds; protected set => _bounds = value; }
public List<Zone> Zones { get => _zones; protected set => _zones = value; }
public List<Window> Windows { get => _windows; protected set => _windows = value; }
public Zone(Bitmap image,Rectangle bounds)
public Zone(Bitmap image, Rectangle bounds)
{
Image = image;
Bounds = bounds;
@@ -46,5 +61,14 @@ namespace OCR_Decode
{
Windows.Add(window);
}
public virtual List<Object> Decode()
{
List<Object> result = new List<Object>();
foreach (Window w in Windows)
{
result.Add(w.DecodePng());
}
return result;
}
}
}