108 lines
4.5 KiB
C#
108 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using Tesseract;
|
|
|
|
namespace OCR_tester
|
|
{
|
|
public class MainZone : Zone
|
|
{
|
|
//This can be a constant as its unlikely it will change in the next 2 years but if the grid opens up it could be interesting to keep
|
|
const int NUMBER_OF_DRIVERS = 20;
|
|
public MainZone(Image fullImage, Rectangle bounds) : base(fullImage, bounds)
|
|
{
|
|
AutoCalibrate();
|
|
Name = "Main";
|
|
}
|
|
public void AutoCalibrate()
|
|
{
|
|
List<Rectangle> detectedText = new List<Rectangle>();
|
|
Zones = new List<Zone>();
|
|
|
|
TesseractEngine engine = new TesseractEngine(Window.tessDataFolder.FullName, "eng", EngineMode.Default);
|
|
Image image = ZoneImage;
|
|
var tessImage = Pix.LoadFromMemory(Window.ImageToByte(image));
|
|
|
|
Page page = engine.Process(tessImage);
|
|
using (var iter = page.GetIterator())
|
|
{
|
|
iter.Begin();
|
|
do
|
|
{
|
|
Rect boundingBox;
|
|
if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out boundingBox))
|
|
{
|
|
//var text = iter.GetText(PageIteratorLevel.Word).ToUpper();
|
|
//We remove all the rectangles that are definitely too big
|
|
if (boundingBox.Height < image.Height / NUMBER_OF_DRIVERS) {
|
|
//Now we add a filter to only get the boxes in the right because they are much more reliable in size
|
|
if (boundingBox.X1 > image.Width / 2)
|
|
{
|
|
//Now we check if an other square box has been found roughly in the same y axis
|
|
bool match = false;
|
|
//The tolerance is roughly half the size that a window will be
|
|
int tolerance = (image.Height / NUMBER_OF_DRIVERS) / 2;
|
|
|
|
foreach (Rectangle rect in detectedText)
|
|
{
|
|
if (rect.Y > boundingBox.Y1 - tolerance && rect.Y < boundingBox.Y1 + tolerance)
|
|
{
|
|
//There already is a rectangle in this line
|
|
match = true;
|
|
}
|
|
}
|
|
//if nothing matched we can add it
|
|
if(!match)
|
|
detectedText.Add(new Rectangle(boundingBox.X1, boundingBox.Y1, boundingBox.Width, boundingBox.Height));
|
|
}
|
|
}
|
|
}
|
|
} while (iter.Next(PageIteratorLevel.Word));
|
|
}
|
|
foreach (Rectangle Rectangle in detectedText)
|
|
{
|
|
Rectangle windowRectangle;
|
|
Size windowSize = new Size(image.Width, image.Height / NUMBER_OF_DRIVERS);
|
|
Point windowLocation = new Point(0, (Rectangle.Y + Rectangle.Height / 2) - windowSize.Height / 2);
|
|
windowRectangle = new Rectangle(windowLocation, windowSize);
|
|
|
|
Zones.Add(new DriverZone(ZoneImage, windowRectangle));
|
|
}
|
|
}
|
|
public override Bitmap Draw()
|
|
{
|
|
Bitmap image = ZoneImage;
|
|
Graphics g = Graphics.FromImage(image);
|
|
string imgDumbFil = @"C:\Users\Moi\Desktop\imgDump\";
|
|
|
|
int count = 0;
|
|
foreach (Zone z in Zones)
|
|
{
|
|
g.DrawRectangle(Pens.Red,z.Bounds);
|
|
string dir = imgDumbFil + "driver" + count + @"\";
|
|
|
|
if (Directory.Exists(dir))
|
|
Directory.Delete(dir, true);
|
|
|
|
Directory.CreateDirectory(dir);
|
|
z.ZoneImage.Save(Path.Combine(dir,("driver"+count+".png")));
|
|
int count2 = 0;
|
|
foreach (Window w in z.Windows)
|
|
{
|
|
g.DrawRectangle(Pens.Blue,new Rectangle(w.Bounds.X,w.Bounds.Y + count * z.Bounds.Height,w.Bounds.Width,w.Bounds.Height));
|
|
w.WindowImage.Save(dir+w.Name+".png");
|
|
count2++;
|
|
}
|
|
count++;
|
|
}
|
|
image.Save(@"C:\Users\Moi\Desktop\imgDump\test.png");
|
|
return image;
|
|
}
|
|
|
|
}
|
|
}
|