以下是示例图片:
我在Python中将图片输入了pytesseract.image_to_string()
,但返回的是空白。
更新:这是我尝试过的最新代码,它会为某些验证码返回正确的文本。
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)
调整大小后:e7rmp
模糊后:e7rmp
在thresh_img之后:ce7nmp
erode_img之后:ce7nap
morph_img之后:ce7nap
您可以看到thresh_img之后打印了正确的文本。腐蚀和变形后,情况变得更糟。
但是成功率几乎不到1%。在大多数情况下,我仍然会回答错误。
任何指针/想法真的会有所帮助吗? Here is a sample collection of captcha's,如果有人想尝试我的代码。