Wednesday, 3 November 2021

how to improve pytesseract arguments to work properly

I would like to read this captcha using pytesseract:

enter image description here

I follow the advice here: Use pytesseract OCR to recognize text from an image

My code is:

import pytesseract
import cv2

def captcha_to_string(picture):
    image = cv2.imread(picture)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (3,3), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Morph open to remove noise and invert image
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
    invert = 255 - opening

    cv2.imwrite('thresh.jpg', thresh)
    cv2.imwrite('opening.jpg', opening)
    cv2.imwrite('invert.jpg', invert)

    # Perform text extraction
    text = pytesseract.image_to_string(invert, lang='eng', config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
    return text

But my code returns 8\n\x0c which is nonsense.

This is how thresh looks like:

enter image description here This is how opening looks like:

enter image description here This is how invert looks like:

enter image description here

Can you help me, how can I improve captcha_to_string function to read the captcha properly? Thanks a lot.



from how to improve pytesseract arguments to work properly

No comments:

Post a Comment