Search code examples
pythonocrpython-tesseract

Python Tesseract OCR isn't properly detecting both numbers


This is the image I'm using. This is the output that was returned with print(pytesseract.image_to_string(img)):

@& [COE] daKizz

6 Grasslands

Q Badlands

@g Swamp

VALUE: @ 7,615,; HIRE)

I've tried using the config second parameter (from psm 0 - 13, and "digits") but it doesn't make much difference. I'm considering looking into training tesseract to recognize the type of numbers in an image like that but I'm hoping there are other options.

I've also tried cleaning up the picture but I'm getting the same output

How can I fix this?


Solution

  • I guess you are missing the image processing part.

    From the documentation "Improving the quality of the output" you may be interested in Image Processing section.

    One way of reaching the desired numbers is inRange thresholding

    To apply in-range, we first need to convert the image in hsv colorspace. Since we want the image between the range of pixels.

    The result will be:

    enter image description here

    Now if you read it with "digits" option:

    53.8.
    53.8.
    453.3.
    
    7.615.725.834
    
    • One possible question is: Why inRange threshold?

    Well, if you try with the other methods (simple or adaptive) you will see inRange is more accurate for the given input image.

    • Another possible question is: Can we use the values of the cv2.inRange method for other images.

    You may not get the desired result. For other images you need to find other range values.

    Code:

    import cv2
    import pytesseract
    import numpy as np
    
    bgr_image = cv2.imread("vNYcf.jpg")
    hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_image, np.array([0, 180, 218]), np.array([60, 255, 255]))
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
    dilate = cv2.dilate(mask, kernel, iterations=1)
    thresh = 255 - cv2.bitwise_and(dilate, mask)
    text = pytesseract.image_to_string(thresh, config="--psm 6 digits")
    print(text)