Rohalt
Rohalt

Reputation: 147

How to process this captcha image for Pytesseract?

I want to solve automatically captchas like this one (all of them with red background and white letters) with Pytesseract

Captcha image

I have been trying processing image to make Pytesseract be able to read it, but no success. Would be great to receive your ideas to process this image. Here my code:

import cv2
import pytesseract

tessdata_dir_config = '--tessdata-dir "C:\\Program Files\\Tesseract-OCR\\tessdata"'
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'

img = cv2.imread("captcha.png")
img = cv2.resize(img, None, fx=2, fy=2)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
adaptive = cv2.adaptiveThreshold(
    gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 85, 20)
print((pytesseract.image_to_string(img, config=tessdata_dir_config)).strip())
print((pytesseract.image_to_string(gray, config=tessdata_dir_config)).strip())
print((pytesseract.image_to_string(adaptive, config=tessdata_dir_config)).strip())

cv2.imshow("Captcha", img) # Output: IMQW
cv2.imshow("Gray", gray) # Output: IMOW
cv2.imshow("Adaptive", adaptive) # Output: IMOW,

cv2.waitKey(7000)

Upvotes: 0

Views: 15533

Answers (1)

Ahx
Ahx

Reputation: 7985

I have a three-step solution


    1. Resize
    1. Closing
    1. Threshold

Step-1: Resize


Resizing the image enables the OCR-algorithm to detect the character or digit strokes in the input image.

Step-2: Closing


Closing is a morphological operation aims to remove the small-holes in the input image.

If we look carefully Q and W characters consists of lots of small holes.

Step-3: Threhsold


We will apply simple-threhsolding to binarize the image. Our aim to remove any leftover artifacts from the image.

Resize Closing Threshold
enter image description here enter image description here enter image description here

Result:

IMQW

Code:


import cv2
from pytesseract import image_to_string

img = cv2.imread("QUfxY.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(h, w) = gry.shape[:2]
gry = cv2.resize(gry, (w*2, h*2))
cls = cv2.morphologyEx(gry, cv2.MORPH_CLOSE, None)
thr = cv2.threshold(cls, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
txt = image_to_string(thr)
print(txt)

Upvotes: 12

Related Questions