185 lines
6.5 KiB
C#
185 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.IO;
|
|
|
|
namespace OCR_tester
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
DirectoryInfo imagesFolder = new DirectoryInfo(@"C:\Users\Moi\Pictures\SeleniumScreens\DataSetHighRes");
|
|
const string DEFAULT_IMAGE_NAME = "screen_"; //You will need to add a number and extension after it ex: screen_2.png
|
|
const string ENGLISH_TESSDATA_FILENAME = "eng.traineddata";
|
|
const string DEFAULT_CREATEZONE_BUTTON_TEXT = "Create Zone";
|
|
const string DEFAULT_CREATEWINDOW_BUTTON_TEXT = "Create Window";
|
|
|
|
Point[] zonePoints;
|
|
Point[] windowPoints;
|
|
|
|
bool selectingZonePoints = false;
|
|
bool selectingWindowPoints = false;
|
|
|
|
MainZone mainZone;
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
reload();
|
|
}
|
|
private void reload()
|
|
{
|
|
pbxInput.Image = Image.FromFile(Path.Combine(imagesFolder.FullName, DEFAULT_IMAGE_NAME + "30.png"));
|
|
}
|
|
private void SwitchCreateZoneButton()
|
|
{
|
|
if (!selectingWindowPoints)
|
|
{
|
|
if (selectingWindowPoints)
|
|
SwitchCreateWindowButton();
|
|
selectingZonePoints = true;
|
|
zonePoints = new Point[] { new Point(-1, -1), new Point(-1, -1) };
|
|
|
|
}
|
|
else
|
|
{
|
|
selectingZonePoints = false;
|
|
}
|
|
|
|
UpdateCreateZoneButton();
|
|
}
|
|
private void SwitchCreateWindowButton()
|
|
{
|
|
if (!selectingWindowPoints)
|
|
{
|
|
if (selectingZonePoints)
|
|
SwitchCreateZoneButton();
|
|
selectingWindowPoints = true;
|
|
windowPoints = new Point[] { new Point(-1, -1), new Point(-1, -1) };
|
|
}
|
|
else
|
|
{
|
|
selectingWindowPoints = false;
|
|
}
|
|
|
|
UpdateCreateWindowButton();
|
|
}
|
|
private void UpdateCreateWindowButton()
|
|
{
|
|
btnCreateWindow.Text = "Points remaining : " + PointsRemaining(windowPoints);
|
|
}
|
|
private void UpdateCreateZoneButton()
|
|
{
|
|
btnCreateZone.Text = "Points remaining : " + PointsRemaining(zonePoints);
|
|
}
|
|
private void btnCreateZone_Click(object sender, EventArgs e)
|
|
{
|
|
SwitchCreateZoneButton();
|
|
}
|
|
private void btnCreateWindow_Click(object sender, EventArgs e)
|
|
{
|
|
SwitchCreateWindowButton();
|
|
}
|
|
private int PointsRemaining(Point[] points)
|
|
{
|
|
int remaining = 0;
|
|
foreach (Point p in points)
|
|
{
|
|
if (p.X == -1 && p.Y == -1)
|
|
remaining++;
|
|
}
|
|
return remaining;
|
|
}
|
|
|
|
private void pbxInput_Click(object sender, EventArgs e)
|
|
{
|
|
Point coordinates = pbxInput.PointToClient(new Point(MousePosition.X, MousePosition.Y));
|
|
|
|
float xOffset = (float)pbxInput.Image.Width / (float)pbxInput.Width;
|
|
float yOffset = (float)pbxInput.Image.Height / (float)pbxInput.Height;
|
|
if (selectingZonePoints)
|
|
{
|
|
int remaining = PointsRemaining(zonePoints);
|
|
int index = (zonePoints.Length) - remaining;
|
|
zonePoints[index] = new Point(Convert.ToInt32((float)coordinates.X * xOffset), Convert.ToInt32((float)coordinates.Y * yOffset));
|
|
remaining -= 1;
|
|
|
|
if (remaining == 0)
|
|
{
|
|
Image fullImage = pbxInput.Image;
|
|
Size newSize = new Size(zonePoints[1].X - zonePoints[0].X, zonePoints[1].Y - zonePoints[0].Y);
|
|
mainZone = new MainZone(fullImage, new Rectangle(zonePoints[0], newSize));
|
|
pbxInput.Image = mainZone.Draw();
|
|
pbxWindow.Image = mainZone.Zones[1].ZoneImage;
|
|
SwitchCreateZoneButton();
|
|
}
|
|
else
|
|
{
|
|
UpdateCreateZoneButton();
|
|
}
|
|
}
|
|
}
|
|
private void pbxWindow_Click(object sender, EventArgs e)
|
|
{
|
|
if (mainZone != null)
|
|
{
|
|
Point coordinates = pbxWindow.PointToClient(new Point(MousePosition.X, MousePosition.Y));
|
|
|
|
float xOffset = (float)pbxWindow.Image.Width / (float)pbxWindow.Width;
|
|
float yOffset = (float)pbxWindow.Image.Height / (float)pbxWindow.Height;
|
|
|
|
if (selectingWindowPoints)
|
|
{
|
|
int remaining = PointsRemaining(windowPoints);
|
|
int index = (windowPoints.Length) - remaining;
|
|
windowPoints[index] = new Point(Convert.ToInt32((float)coordinates.X * xOffset), Convert.ToInt32((float)coordinates.Y * yOffset));
|
|
remaining -= 1;
|
|
|
|
if (remaining == 0)
|
|
{
|
|
Image fullImage = pbxWindow.Image;
|
|
Point position = windowPoints[0];
|
|
Size newSize = new Size(windowPoints[1].X - windowPoints[0].X, windowPoints[1].Y - windowPoints[0].Y);
|
|
|
|
foreach (Zone z in mainZone.Zones)
|
|
{
|
|
z.AddWindow(new Rectangle(position,newSize));
|
|
}
|
|
pbxInput.Image = mainZone.Draw();
|
|
pbxWindow.Image = mainZone.Zones[1].Draw();
|
|
SwitchCreateWindowButton();
|
|
}
|
|
else
|
|
{
|
|
UpdateCreateWindowButton();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private void btnDeleteZone_Click(object sender, EventArgs e)
|
|
{
|
|
reload();
|
|
}
|
|
|
|
private void btnExport_Click(object sender, EventArgs e)
|
|
{
|
|
string path = @"C:\Users\Moi\Desktop\imgDump\";
|
|
string name = "Dump.json";
|
|
if (File.Exists(path + name))
|
|
{
|
|
File.Delete(path+name);
|
|
}
|
|
using (StreamWriter writer = new StreamWriter(path+name))
|
|
{
|
|
writer.WriteLine("{");
|
|
writer.WriteLine(mainZone.ToJSON());
|
|
writer.WriteLine("}");
|
|
}
|
|
}
|
|
}
|
|
}
|