Just everything is better

This commit is contained in:
2022-10-19 16:14:02 +02:00
parent 8a05e7b54b
commit f0f78e81f9
6 changed files with 589 additions and 133 deletions
+88 -122
View File
@@ -18,15 +18,21 @@ namespace TestVideo
public partial class Form1 : Form
{
public Screen[] screens;
Screen currentScreen;
List<string> screenNames;
Extraction extraction;
int currentZoneIndex;
List<Zone.ZoneType> zoneTypes;
bool creatingZone;
Point[] newZonePoints;
Image displayedImage = null;
public Form1()
{
InitializeComponent();
screens = Screen.AllScreens;
currentScreen = screens[0];
extraction = new Extraction(screens[0]);
currentZoneIndex = -1;
screenNames = new List<string>();
foreach (Screen scr in screens)
{
@@ -34,150 +40,110 @@ namespace TestVideo
}
lsbScreens.DataSource = screenNames;
//TESTING TESSERACT
//string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Baneer.png";
//string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Test.png";
//string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Standings.png";
//string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Gaps.png";
//string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\SectorTimes.png";
string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Standings2.png";
tbxResult.Text = DecodeTessereactResults(testTesseract((Bitmap)Image.FromFile(path)), Image.FromFile(path), pbxOutput);
}
private Bitmap PrepareImage(Bitmap inputImage)
{
for (int y = 0; y < inputImage.Height; y++)
zoneTypes = new List<Zone.ZoneType>();
foreach (object type in Enum.GetValues(typeof(Zone.ZoneType)))
{
for (int x = 0; x < inputImage.Width; x++)
{
//We will apply a filter to put the image first in grayScale
//Then if the pixel is more white than black it will be fully white and same for black
//Grayscale = 0.299R + 0.587G + 0.114B
Color color = inputImage.GetPixel(x, y);
int grayScaleValue = Convert.ToInt32(0.299 * (float)color.R + 0.587 * (float)color.G + 0.114 * (float)color.B);
int finalValue = 0;
if (grayScaleValue > 255 / 2)
finalValue = 255;
inputImage.SetPixel(x, y, Color.FromArgb(finalValue, finalValue, finalValue));
}
zoneTypes.Add((Zone.ZoneType)type);
}
//inputImage.Save("C:/TEST/test.png",ImageFormat.Png);
return inputImage;
}
private string DecodeTessereactResults(string tesseractResult, Image inputImage, PictureBox resultPbx)
{
MessageBox.Show(tesseractResult);
string result = "";
string[] lettersData = tesseractResult.Split('\n');
using (Graphics g = Graphics.FromImage(inputImage))
{
foreach (string letterData in lettersData)
{
string[] data = letterData.Split(' ');
//This data is in a format that we cant process
if (data.Length != 6)
break;
string letter = data[0];
Point position = new Point(Convert.ToInt32(data[1]), Convert.ToInt32(data[2]));
Size size = new Size(Convert.ToInt32(data[3]), Convert.ToInt32(data[4]));
int unknownData = Convert.ToInt32(data[5]);
//A neat post processing Idea would be to separate letters that have a unusually big difference in X or Y axis to try and recognise words
//Use the data
result += letter;
g.DrawRectangle(new Pen(Brushes.Red, 5), new Rectangle(position, size));
}
}
resultPbx.Image = inputImage;
return result;
lsbZoneTypes.DataSource = zoneTypes;
newZonePoints = new Point[2] {new Point(-1,-1), new Point(-1,-1)};
}
private void btnStart_Click(object sender, EventArgs e)
{
if (tmrScreenshots.Enabled)
{
tmrScreenshots.Enabled = false;
btnStart.Text = "Start";
btnStart.Text = "Start recording";
}
else
{
tmrScreenshots.Enabled = true;
btnStart.Text = "Stop";
btnStart.Text = "Stop recording";
}
}
private Bitmap ScreenShot()
{
Bitmap screenshot;
screenshot = new Bitmap(currentScreen.Bounds.Width, currentScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
Graphics gfxScreenshot = Graphics.FromImage(screenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(
currentScreen.Bounds.X,
currentScreen.Bounds.Y,
0,
0,
currentScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
//testTesseract(screenshot);
//SaveScreenshot(screenshot);
MessageBox.Show(testTesseract(screenshot));
return screenshot;
}
private string SaveScreenshot(Bitmap image)
public void RefreshUi()
{
string path = "";
string directory = AppDomain.CurrentDomain.BaseDirectory + "Screens";
path = directory + "\\Screenshot" + DateTime.Now.ToString("yyyy_MM_dd-hh_mm_ss") + ".png";
if (Directory.Exists(directory))
{
image.Save(path, ImageFormat.Png);
}
else
{
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\Screens");
}
return path;
}
//http://leonwoo-tech.blogspot.com/2018/06/tip-how-to-properly-setup-opencv-ocr-to.html
private string testTesseract(Bitmap inputBmp)
{
string result = "";
//To use this toImage you need : NuGet\Install-Package Emgu.CV.Bitmap -Version 4.5.5.4823
inputBmp = PrepareImage(inputBmp);
Image<Bgr, byte> emguImage = inputBmp.ToImage<Bgr, byte>();
using (var image = emguImage)
{
using (Tesseract tesseractOcrProvider = new Tesseract(AppDomain.CurrentDomain.BaseDirectory + "\\Data", "eng", OcrEngineMode.Default))
{
tesseractOcrProvider.SetImage(image);
tesseractOcrProvider.Recognize();
result = tesseractOcrProvider.GetBoxText().TrimEnd();
}
}
//File.Delete(filePath);
return result;
lsbZones.DataSource = extraction.GetZonesNames();
extraction.DrawZones();
//extraction.UpdateZones();
pbxInput.Image = extraction.ScreenPreview;
}
private void tmrScreenshots_Tick(object sender, EventArgs e)
{
//pbxInput.Image.Dispose();
Image img = ScreenShot();
pbxInput.Image = img;
extraction.UpdateZones();
pbxInput.Image = extraction.ScreenPreview;
if(currentZoneIndex != -1)
tbxResult.Text = extraction.GetZoneRawText(currentZoneIndex);
//This is to prevent Ram from exploding
GC.Collect();
//img.Dispose();
}
private void lsbScreens_SelectedIndexChanged(object sender, EventArgs e)
{
currentScreen = screens[lsbScreens.SelectedIndex];
extraction.ChangeScreen(screens[lsbScreens.SelectedIndex]);
pbxInput.Image = extraction.ScreenPreview;
}
private void pbxInput_Click(object sender, EventArgs e)
{
if (creatingZone)
{
//Convert coordinates to image coordinates
Rectangle screenSize = extraction.CurrentScreen.Bounds;
float xRatio = (float)screenSize.Width / (float)pbxInput.Width;
float yRatio = (float)screenSize.Height / (float)pbxInput.Height;
Point tmpPoint = pbxInput.PointToClient(MousePosition);
Point newPoint = new Point(Convert.ToInt32((float)tmpPoint.X * xRatio),Convert.ToInt32((float)tmpPoint.Y * yRatio));
if (newZonePoints[0] == new Point(-1,-1))
{
//Its the first point
newZonePoints[0] = newPoint;
btnCreateZone.Text = "Creating Zone 1/2 points selected";
}
else
{
//Its the second point
newZonePoints[1] = newPoint;
btnCreateZone.Text = "Create Zone";
Zone.ZoneType newZoneType = zoneTypes[lsbZoneTypes.SelectedIndex];
Random rnd = new Random();
Point p1 = newZonePoints[0];
Point p2 = newZonePoints[1];
Size newSize = new Size(Math.Max(p1.X,p2.X) - Math.Min(p1.X, p2.X),Math.Max(p1.Y,p2.Y) - Math.Min(p1.Y, p2.Y));
extraction.AddZone(new Rectangle(newZonePoints[0],newSize),newZoneType,"Coucou");
newZonePoints = new Point[2] { new Point(-1, -1), new Point(-1, -1) };
creatingZone = false;
RefreshUi();
}
}
}
private void btnCreateZone_Click(object sender, EventArgs e)
{
creatingZone = !creatingZone;
if (creatingZone)
{
btnCreateZone.Text = "Creating Zone 0/2 points selected";
}
else
{
btnCreateZone.Text = "Create Zone";
}
}
private void lsbZones_SelectedIndexChanged(object sender, EventArgs e)
{
if (extraction.GetZonesNames().Any() && lsbZones.SelectedIndex >= 0)
{
currentZoneIndex = lsbZones.SelectedIndex;
}
}
}
}