Search code examples
pythonocrpython-tesseract

PyTesseract fails to recognize decimal number


Basically I would like to recognize the strings of this image.

numbers for recognition

But for some reason all I get is an empty string (sometimes a random number 2 or 3).

import cv2
import numpy as np
import pytesseract

img = cv2.imread("screenshot.png")

gry = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_bound = np.array([0, 0, 0])
upper_bound = np.array([0, 0, 255])
thr = cv2.inRange(gry, lower_bound, upper_bound)
thr = cv2.resize(thr, (0, 0), fx=2, fy=2)
thr = cv2.bitwise_not(thr)

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

txt = pytesseract.image_to_string(thr, config="--psm 6 digits")

print(txt)


Solution

  • Your picture is weak on the left side. Please play a bit with the gray scale, the resize etc. and use black & white. I think you will never rich a perfect result:

    import pytesseract
    import cv2
    
    image_file = "Bild.png"
    
    # load the input image, convert it from BGR to RGB channel ordering,
    image = cv2.imread(image_file, cv2.IMREAD_UNCHANGED)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    print('Original Dimensions : ',image.shape)
     
    scale_percent = 60 # percent of original size
    width = int(rgb.shape[1] * scale_percent / 100)
    height = int(rgb.shape[0] * scale_percent / 100)
    dim = (width, height)
      
    # resize image
    resized = cv2.resize(rgb, dim, interpolation = cv2.INTER_AREA)
    print('Resized Dimensions : ',resized.shape)
    
    grayImage = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    (thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 182, 255, cv2.THRESH_BINARY) # 0...255
    
    bw = cv2.imshow('Black white image', blackAndWhiteImage)
    inverted_image = cv2.bitwise_not(blackAndWhiteImage)
    iv = cv2.imshow("Inverted", inverted_image)
    
    
    # Configuration
    options = "" # with text
    #options = "outputbase digits" # only digits
    
    # OCR the input image using Tesseract
    text = pytesseract.image_to_string(inverted_image, config=options)
    print(text)
    
    cv2.imshow('Resized', resized)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Output:

    10,48x 1,46x 2,72x 1,00x 2,06x 2,31x
    

    enter image description here