178 lines
6.4 KiB
C#
178 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Emgu.CV;
|
|
using Emgu.CV.OCR;
|
|
using Emgu.CV.Structure;
|
|
using System.IO;
|
|
|
|
namespace TestVideo
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public Screen[] screens;
|
|
Screen currentScreen;
|
|
List<string> screenNames;
|
|
|
|
Image displayedImage = null;
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
screens = Screen.AllScreens;
|
|
currentScreen = screens[0];
|
|
screenNames = new List<string>();
|
|
foreach (Screen scr in screens)
|
|
{
|
|
screenNames.Add(scr.DeviceName);
|
|
}
|
|
lsbScreens.DataSource = screenNames;
|
|
|
|
//TESTING TESSERACT
|
|
|
|
string path = AppDomain.CurrentDomain.BaseDirectory + "Screens\\Baneer.png";
|
|
|
|
tbxResult.Text = DecodeTessereactResults(testTesseract(path), Image.FromFile(path), pbxOutput);
|
|
//MessageBox.Show(DecodeTessereactResults(testTesseract(path), Image.FromFile(path), pbxOutput));
|
|
}
|
|
private Bitmap PrepareImage(Bitmap inputImage)
|
|
{
|
|
using (Graphics g = Graphics.FromImage(inputImage))
|
|
{
|
|
for (int y = 0; y < inputImage.Height;y++)
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
return inputImage;
|
|
}
|
|
private string DecodeTessereactResults(string tesseractResult, Image inputImage, PictureBox resultPbx)
|
|
{
|
|
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]);
|
|
|
|
//Use the data
|
|
result += letter;
|
|
g.DrawRectangle(new Pen(Brushes.Red,5),new Rectangle(position,size));
|
|
}
|
|
}
|
|
|
|
resultPbx.Image = inputImage;
|
|
|
|
return result;
|
|
}
|
|
private void btnStart_Click(object sender, EventArgs e)
|
|
{
|
|
if (tmrScreenshots.Enabled)
|
|
{
|
|
tmrScreenshots.Enabled = false;
|
|
btnStart.Text = "Start";
|
|
}
|
|
else
|
|
{
|
|
tmrScreenshots.Enabled = true;
|
|
btnStart.Text = "Stop";
|
|
}
|
|
}
|
|
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(SaveScreenshot(screenshot)));
|
|
return screenshot;
|
|
}
|
|
private string SaveScreenshot(Bitmap image)
|
|
{
|
|
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(string filePath)
|
|
{
|
|
string result = "";
|
|
using (var image = new Image<Bgr, byte>(filePath))
|
|
{
|
|
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;
|
|
}
|
|
|
|
private void tmrScreenshots_Tick(object sender, EventArgs e)
|
|
{
|
|
//pbxInput.Image.Dispose();
|
|
Image img = ScreenShot();
|
|
pbxInput.Image = img;
|
|
GC.Collect();
|
|
//img.Dispose();
|
|
}
|
|
|
|
private void lsbScreens_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
currentScreen = screens[lsbScreens.SelectedIndex];
|
|
}
|
|
|
|
}
|
|
}
|