Search code examples
pythonocrpython-tesseract

Pytesseract OCR doesn't recognize the digits


I am trying to read these images:

enter image description here enter image description here enter image description here

I have tried several options but I can't seem to read them correctly as 15/0, 30/0, 40/0.

    frame = frame[900:1000, 450:500]
    scale_percent = 200  # percent of original size
    width = int(frame.shape[1] * scale_percent / 100)
    height = int(frame.shape[0] * scale_percent / 100)
    dim = (width, height)
    frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
    cv2.imshow("cropped", frame)
    cv2.waitKey(0)
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    cv2.imshow("cropped", frame)
    cv2.waitKey(0)

    pytesseract.pytesseract.tesseract_cmd = (
        r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    )
    results = pytesseract.image_to_data(
        frame,
        output_type=Output.DICT,
        config="--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789",
    )
    # results = replace_chars(results)
    print(("-").join(results["text"]), "\n")

Solution

  • One way of solving is using inRange thresholding

    The result will be:

    enter image description here enter image description here enter image description here

    If you set page-segmentation-mode 6

    15
    0
    
    30
    0
    
    40
    0
    

    Code:

    import cv2
    import pytesseract
    from numpy import array
    
    image_list = ["LZxCs.png", "W06I0.png", "vvzE5.png"]
    
    for image in image_list:
        bgr_image = cv2.imread(image)
        hsv_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv_image, array([0, 0, 0]), array([165, 10, 255]))
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 3))
        dilate = cv2.dilate(mask, kernel, iterations=1)
        thresh = cv2.bitwise_and(dilate, mask)
        text = pytesseract.image_to_string(thresh, config='--psm 6')
        print(text)
    

    The second way is applying global-threshold

    enter image description here enter image description here enter image description here

    If you set page-segmentation-mode 6

    15
    0
    
    30
    0
    
    40
    0
    

    Code:

    import cv2
    import pytesseract
    
    image_list = ["LZxCs.png", "W06I0.png", "vvzE5.png"]
    
    for image in image_list:
        bgr_image = cv2.imread(image)
        gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
        thresh = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
        text = pytesseract.image_to_string(thresh, config='--psm 6')
        print(text)
        cv2.imwrite(f"/Users/ahx/Desktop/{image}", thresh)
        cv2.imshow('', thresh)
        cv2.waitKey(0)
    

    For more, you can check the documentation