I wanted to be able to check if specific text is displayed somewhere on the screen and, if possible, determine its location so that the program could display a popup above the specific text.
I tried to use OCR (Tesseract) to scan screenshots but it correctly sees the text about 20% of the time, even after trying multiple image formats. Is this a common problem, is Tesseract not a great OCR, or are there any ways to improve the quality of the screenshots for the OCR to work better?
I capture and save the screenshots this way:
Bitmap captureBitmap = new Bitmap(1920, 1080, PixelFormat.Format32bppPArgb);
Rectangle captureRectangle = Screen.AllScreens[0].Bounds;
Graphics captureGraphics = Graphics.FromImage(captureBitmap);
captureGraphics.CopyFromScreen(captureRectangle.Left, captureRectangle.Top, 0, 0, captureRectangle.Size);
captureBitmap.Save(@"./Capture.png", System.Drawing.Imaging.ImageFormat.Png);
The OCR (Tesseract) is implemented this way:
var ImagePath = "./Capture.png";
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
{
using (var img = Pix.LoadFromFile(ImagePath))
{
using (var page = engine.Process(img))
{
var text = page.GetText();
if (text.Contains(selectedword))
{
MessageBox.Show("This word is here");
}
}
}
}
Are there any better ways to scan the screen / screenshots for specific text?
question from:
https://stackoverflow.com/questions/66049112/how-to-find-specific-text-on-the-screen-and-its-location-c-sharp 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…