Search code examples
pythonocrtesseracttesseract-5.x

How to detect digits from image by using Tesseract 5?


I installed tesseract5 on WSL (Ubuntu 22.04.1LTS) and tried to detect numbers from images as follows, but Tesseract returned wrong answers. How can I get right answers?

My environment:

  • Windows 11 22H2
  • WSL2 Ubuntu 22.04.1LTS
  • tesseract 5.3.1-20-g58b7

I tried Tesseract like this

tesseract hoge.jpg output -l eng

and output.txt is

Fb¥
&/0

Here is hoge.jpg.

enter image description here

Thank you for helping in advance. I'm a Japanese student, so my English may be not so good. If you think it's not clear English, please change this post to make it more readable.


Solution

  • From bad picture you will never get good results. I played a bit and get this one:

    import subprocess
    import cv2
    import pytesseract
    
    # Image manipulation
    # Commands https://imagemagick.org/script/convert.php
    mag_img = r'D:\Programme\ImageMagic\magick.exe'
    con_bw = r"D:\Programme\ImageMagic\convert.exe" 
    
    in_file = r'ZZ_Numbers.jpg'
    out_file = r'ZZ_Numbers_bw.png'
    
    # Play with black and white and rotate for better results
    process = subprocess.run([con_bw, in_file, "-resize", "70%","-threshold","60%", "-rotate", "-17", "-brightness-contrast","-15x30",out_file])
    
    # Text ptocessing
    pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
    img = cv2.imread(out_file)
    
    # Parameters see tesseract doc 
    custom_config = r'--psm 11 --oem 3 tessedit_char_whitelist=0123456789' 
    
    tex = pytesseract.image_to_string(img, config=custom_config)
    print(tex)
    
    with open("cartootn.txt", 'w') as f:
        f.writelines(tex)
    
    cv2.imshow('image',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Output: enter image description here