Вот примеры изображений:
![enter image description here](https://i.stack.imgur.com/UCTPI.jpg)
![enter image description here](https://i.stack.imgur.com/51RgK.jpg)
Я передал изображение на pytesseract.image_to_string()
в Python, но оно возвращается пустым.
Обновление: вот последний код, который я пробовал, и он возвращает правильный текст для некоторых кодов.
import time
import pytesseract
import cv2
import numpy as np
import sys
import os
import argparse
try:
import Image
except ImportError:
from PIL import Image
from subprocess import check_output
def solve(path):
print(path)
img = cv2.imread(path, 0)
# cv2.imshow("same", img)
# cv2.waitKey(0)
# crop image
start_width = img.shape[1] // 4
end_width = img.shape[1] - start_width
crop_img = img[0:img.shape[0], start_width:end_width]
# cv2.imshow("crop", crop_img)
# cv2.waitKey(0)
# resize image
scale_percent = 500 # percent of original size
width = int(crop_img.shape[1] * scale_percent / 100)
height = int(crop_img.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(crop_img, dim, interpolation=cv2.INTER_CUBIC)
cv2.imshow("Resized image", resized)
pytesseract_config = "-c tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyz --oem 1 --psm 8"
captcha = pytesseract.image_to_string(resized, config=pytesseract_config)
print("after resized: " + captcha)
cv2.waitKey(0)
# blur = cv2.medianBlur(resized, 3)
# blur = cv2.GaussianBlur(resized,(3,3),0)
blur = cv2.bilateralFilter(resized,9,100,100)
cv2.imshow("blur", blur)
captcha = pytesseract.image_to_string(blur, config=pytesseract_config)
print("after blur: " + captcha)
cv2.waitKey(0)
ret, thresh_img = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imshow('thresh_img', thresh_img)
captcha = pytesseract.image_to_string(thresh_img, config=pytesseract_config)
print("after thresh_img: " + captcha)
cv2.waitKey(0)
rows, cols = thresh_img.shape
horizontalsize = int(cols / 100)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontalsize, 3))
erode_img = cv2.erode(thresh_img, kernel)
cv2.imshow("erode_img", erode_img)
captcha = pytesseract.image_to_string(erode_img, config=pytesseract_config)
print("after erode_img: " + captcha)
cv2.waitKey(0)
morph_img = cv2.morphologyEx(erode_img, cv2.MORPH_CLOSE, kernel)
cv2.imshow("morph_img", morph_img)
captcha = pytesseract.image_to_string(morph_img, config=pytesseract_config)
print("after morph_img: " + captcha)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument('path', help='Captcha collection dir path')
args = argparser.parse_args()
path = args.path
for entry in os.listdir(path):
filename = os.path.join(path, entry)
if os.path.isfile(filename):
solve(filename)
Ниже приведены промежуточные изображения с последующей печатью текста для каждого изображения:
![enter image description here](https://i.stack.imgur.com/ykJVl.png)
после изменения размера: e7rmp
после размытия: e7rmp
после thresh_img: ce7nmp
после erode_img: ce7nap
после morph_img: ce7nap
Как видите, правильный текст был напечатан после thresh_img. Ситуация ухудшилась после разрушения и превращения.
Однако вероятность успеха едва ли составляет 1%. Я все еще получаю неправильный ответ большую часть времени.
Какие-нибудь указатели / мысли были бы действительно полезны? Вот пример коллекции капчи, если кто-то хочет попробовать мой код.