Reading a CAPTCHA with opencv and pytesseract

Viewed 59

I am trying to improve an image in order to making the text more readable for OCR, but the problem is that some images have some missing pixels and OCR doesn't recognized it.

Here is my code:

import cv2 as cv
import pytesseract
import numpy as np


img = cv.imread("image1.jpeg")
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)

threshold = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 57, 13)

x = 255 - threshold

kernel = np.ones((3, 3),np.uint8)
closing = cv.morphologyEx(x, cv.MORPH_CLOSE, kernel)

captcha = pytesseract.image_to_string(closing, config="--psm 3")
print(captcha)

cv.imshow('close', closing)
cv.imshow('thresh', threshold)

cv.waitKey(0)
cv.destroyAllWindows()

This is the original image

enter image description here

This is threshold image

enter image description here

And this is the result after using closing morph

enter image description here

For some reason OCR returns the string le eth g

Any idea of how can I improve my code?

1 Answers

Sometimes preprocessing is not needed for the input images. When I tried the input image you gave:

enter image description here

I used the code:

import cv2 as cv
import pytesseract


img = cv.imread("/home/yns/Downloads/t.jpg")


captcha = pytesseract.image_to_string(img, config="--psm 6")
print(captcha)

and the result comes out as:

TTCo7

which is almos correct. it would be better to keep in mind tesseract is more accurate for the aligned texts so even in some CAPTCHA texts you get successful results it will not work fine at all.

For the reference here is the output of tesseract --version:

tesseract 4.1.3  leptonica-1.78.0   libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.2) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0  
Found AVX2  
Found AVX  
Found FMA  
Found SSE  
Found libarchive 3.2.2 zlib/1.2.11 liblzma/5.2.2 bz2lib/1.0.6 liblz4/1.7.1
Related