Reputation: 183
I have a photo like this:
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.
Upvotes: 0
Views: 1563
Reputation: 46600
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
Morph open
Morph close
Invert, apply slight blur, and OCR
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()
Upvotes: 2
Reputation: 943
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.
Upvotes: 0