Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
797 views
in Technique[技术] by (71.8m points)

ocr - How to find specific text on the screen and its location? C#

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...