i agree with @Jon Betts
- tesseract is not very strong in OCR, only good in binary cases with right settings
- CAPTCHAs ment to fool OCRs!
but if you really need to do it, you need to come up with the manual procedure for it,
i created the code below specifically for the type of CAPTCHAs that you gave (but its completely rigid and is not generalized/optimized for all cases)
psudo code
- apply median blur
- apply a threshold to get Blue colors only (binary image output from this stage)
- apply opening to reduce small white pixels in binary image
- give the image to tesseract with options:
- limited whitelist of output chars
- OEM 3 : tesseract + cube
- PSM 8 : one word per image
Code
from PIL import Image import pytesseract import numpy as np import cv2 img = cv2.imread('a.jpg') img = cv2.medianBlur(img, 3) # extract blue parts img2 = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8) cond = np.bitwise_and(img[:, :, 0] >= 100, img[:, :, 2] < 100) img2[np.where(cond)] = 255 img = img2 # delete the noise kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) str1 = pytesseract.image_to_string(Image.fromarray(img), config='-c tessedit_char_whitelist=abcedfghijklmnopqrtuvwxyz0123456789 -oem 3 -psm 8') cv2.imwrite("frame.png", img) print(str1)
output
f2e4

image
in order to see full options of tesseract, type the following command tesseract --help-extra or refere to this_link