0

I have a photo like this:

captcha

I have tried to read it with pytesseract in Python:

from PIL import Image
import pytesseract
import numpy 
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
im = Image.open("11.jpg")
text = pytesseract.image_to_string(im,lang = "eng")
print(text)

but pytesseract cant read it. I tried opencv too but I can't find a solution.

2
  • Hi ! "pytesseract cant read it " -> what is the error message you are getting?
    – vvvvv
    Commented Feb 2, 2020 at 17:13
  • 3
    You realise that the purpose of the captcha is to be deliberately very hard / impossible for OCR to read, yes?
    – SiHa
    Commented Feb 2, 2020 at 17:15

2 Answers 2

2

Before applying OCR onto the image, you need to preprocess the image. A simple preprocessing approach is to enlarge the image, obtain a binary image using Otsu's threshold, perform morphological operations, then OCR the image.

Enlarge, Gaussian blur, and Otsu's threshold

enter image description here

Morph open

enter image description here

Morph close

enter image description here

Invert, apply slight blur, and OCR

enter image description here

Result from Pytesseract OCR image_to_string using the --psm 6 configuration option to treat the image as a single block of text.

xc2kc2

Code

import cv2
import pytesseract
import imutils

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

# Resize, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.jpg')
image = imutils.resize(image, width=400)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Perform morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
close = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=3)

# Invert, Blur, and perform text extraction
invert = 255 - cv2.GaussianBlur(close, (3,3), 0)
data = pytesseract.image_to_string(invert, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('close', close)
cv2.imshow('invert', invert)
cv2.waitKey()
1
  • Very nice and straightforward solution. It seems this particular CAPTCHA string isn't as strong as it is supposed to be. The system has been broken. Commented Feb 4, 2020 at 0:38
0

This blog has a post where the blogger mentions trying to use tesseract, gocr and ocrad to read captcha text.

The key step is cleaning the image before you try to read it. The example that is given on that site uses a simple threshold filter but since your image is in colour that might not work as well.

You should experiment with different image processing techniques to see if you can clean up the image sufficiently to recognize the text.

Having said the above, I will echo @SiHa's comment and suggest that this activity is unethical. Attempting to subvert the CAPTCHA protection shows a lack of respect for the owner of the server, whether they are doing it to protect their bandwidth or their business.

Not the answer you're looking for? Browse other questions tagged or ask your own question.