Search code examples
pythonpython-tesseract

Tesseract in loop sometimes outputs extra digits


I am running tesseract in an infinite loop for debugging purposes to increase the chance of false detections (I am detecting timers on a window), and I notice that fairly frequently tesseract will add an extra digit when there clearly is none. When I save these false detections and rerun it through tesseract, there are no extra digits.

Here is an example of false detection, where the 16.9 was detected as 1169

Running the saved image through again yields 169. Not detecting the decimal point is fine, I can account for that, but extra digits appearing are a real problem.

Another example. 38.4 was detected as 3584

Here is the tesseract config:

d = pytesseract.image_to_data(img, lang='eng',config='-c tessedit_char_whitelist="0123456789x.% " --psm 6 --oem 3', output_type=Output.DICT)

I have tried psm 6, 7, 11 and 13 with still the same problem. I have tried changing the way I am taking screenshots, still the same problem.

Edit: For now I will simply check the width of the detection but I am still curious if anyone knows what might be causing this.


Solution

  • You need to

    Upsampling is needed for accurate recongition of the digits. Thresholding will output the features of the images.

    Threshold (BINARY + OTSU)
    enter image description here
    40% 76% 19.7 60% 16.9 21.8 24.3 x4
    enter image description here
    2.1 1.7 9.3 90% 34.1 38.4 6.9 x4

    Code:


    import cv2
    import pytesseract
    
    img_lst = ["GAaEm.jpg", "Z2qV5.jpg"]
    
    for img_nm in img_lst:
        img = cv2.imread(img_nm)
        gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        (h, w) = gry.shape[:2]
        gry = cv2.resize(gry, (w*3, h*3))
        thr = cv2.threshold(gry, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
        txt = pytesseract.image_to_string(thr)
        print(txt)
        cv2.imshow("thr", thr)
        cv2.waitKey(0)