0

Im try read text from bitmap(capture screen) by Tesseract using C#, but result wrong word language. My code:

    Class TesseractApi{
    
            public static auto_login.text_rect get_all_text(Bitmap bitmp = null)
            {
                Bitmap bmp;
    
                if (bitmp != null)
                    bmp = bitmp;
                else bmp = ScreenShot.Capture();
    
                auto_login.text_rect results = new auto_login.text_rect();
                Tesseract.PageIteratorLevel myLevel = PageIteratorLevel.TextLine;
    
                using (var img = PixConverter.ToPix(bmp))
                {
                    using (var page = engine.Process(img))
                    {
                        using (var iter = page.GetIterator())
                        {
                            iter.Begin();
                            do
                            {
                                if (iter.TryGetBoundingBox(myLevel, out var rect))
                                {
                                    var curText = iter.GetText(myLevel).ToLower();
    
                                    results.Add(new auto_login.text_rect()
                                    {
                                        text = curText,
                                        x = rect.X1,
                                        y = rect.Y1
                                    });
    
                                }
                            } while (iter.Next(myLevel));
    
                            //iter.Dispose();
                            //page.Dispose();
                            img.Dispose();
                            bmp.Dispose();
                        }
                    }
                }
                return results;
            }
}

Calling...

Bitmap bmp = new Bitmap("test/screen.png");
text_rect text_arg = TesseractApi.get_all_text(bmp); 

pic capture from screen enter image description here

Result:

anench

1.

1 .

l ‘ ' 1”: ,_ c.

wm63mm,”“mm, , y

almweunnswwnedw! .

(puddellhzslo-ne? aunnel 7

. [puppet] n-s wowed om ' ; ,

quppaqhasmmudchinnnl a 0 / v: ‘ \ 1mm -

if f / - ‘ j 1 ' ‘ ., aurlnoiennovshndwnmum v

. \ i 4 "mm ‘

‘ ‘ i ‘,/ \ \ dwnoc-lermwunnmn i

‘ em mum i

i want get word "WAVE", and get position Bound

see pic word "WAVE"

enter image description here

what is wrong with my code

NajiMakhoul
  • 1,623
  • 2
  • 16
  • 30
Jack Huỳnh
  • 79
  • 2
  • 3
  • The first thing you have to do is to avoid the JPEG format for this, it's not really its thing. Take the screenshot and save it as PNG or TIFF. Then, try to convert to grayscale or black and white. Try the method here: [Replacing Color of an Image](https://stackoverflow.com/a/47529000/7444103), using the inverted Matrix: I suppose, given the Image you're showing, that it can work better (the words will become almost all black and the rest will drift towards white). – Jimi Jul 16 '20 at 08:23
  • Also try a Grayscale conversion; see the `ToGrayScale` method shown [here](https://stackoverflow.com/a/61584671/7444103) -- About the B&W conversion, try with a low threshold (10), it will increase the white areas and make the text pop. – Jimi Jul 16 '20 at 08:25
  • tesseract will be happy if you feed a black-n-white image. This might help you https://medium.com/cashify-engineering/improve-accuracy-of-ocr-using-image-preprocessing-8df29ec3a033 – EJL Jul 21 '20 at 07:59
  • Thanks everybody comment, i have solved this my question. 1. we should be define picture "WAVE" by icon 10x10 2. get position X-Y then capture area timer number 3. we clear color by SetPixel orange to black – Jack Huỳnh Dec 24 '20 at 07:33

0 Answers0