【问题标题】:Captcha preprocessing and solving with Opencv and pytesseract使用 Opencv 和 pytesseract 进行验证码预处理和求解
【发布时间】:2017-08-14 18:19:44
【问题描述】:

问题

我正在尝试用 Python 编写代码,以使用 Tesseract-OCR 进行图像预处理和识别。我的目标是可靠地解决这种形式的验证码。

Original captcha and result of each preprocessing step

目前的步骤

  1. 图像的灰度和阈值化

  2. 使用 PIL 增强图像

  3. 转换为 TIF 并缩放到 >300px

  4. 将其提供给 Tesseract-OCR(将所有大写字母列入白名单)

但是,我仍然得到一个相当不正确的读数 (EPQ M Q)。我可以采取哪些其他预处理步骤来提高准确性?我的代码和其他类似性质的验证码附在下面。

similar captchas I want to solve

代码

import cv2
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
def binarize_image_using_opencv(captcha_path, binary_image_path='input-black-n-white.jpg'):
     im_gray = cv2.imread(captcha_path, cv2.IMREAD_GRAYSCALE)
     (thresh, im_bw) = cv2.threshold(im_gray, 85, 255, cv2.THRESH_BINARY)
     # although thresh is used below, gonna pick something suitable
     im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
     cv2.imwrite(binary_image_path, im_bw)

     return binary_image_path

def preprocess_image_using_opencv(captcha_path):
     bin_image_path = binarize_image_using_opencv(captcha_path)

     im_bin = Image.open(bin_image_path)
     basewidth = 300  # in pixels
     wpercent = (basewidth/float(im_bin.size[0]))
     hsize = int((float(im_bin.size[1])*float(wpercent)))
     big = im_bin.resize((basewidth, hsize), Image.NEAREST)

     # tesseract-ocr only works with TIF so save the bigger image in that format
     tif_file = "input-NEAREST.tif"
     big.save(tif_file)

     return tif_file

def get_captcha_text_from_captcha_image(captcha_path):

     # Preprocess the image befor OCR
     tif_file = preprocess_image_using_opencv(captcha_path)



get_captcha_text_from_captcha_image("path/captcha.png")

im = Image.open("input-NEAREST.tif") # the second one 
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
im.save('captchafinal.tif')
text = pytesseract.image_to_string(Image.open('captchafinal.tif'), config="-c 
tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ -psm 6")
print(text)

【问题讨论】:

    标签: python image opencv tesseract python-tesseract


    【解决方案1】:

    主要问题来自字母的不同方向,而不是来自预处理阶段。您进行了应该可以正常工作的常见预处理,但您可以将阈值替换为 adaptive thresholding,以使您的程序在图像亮度方面更加通用。

    我在使用 tesseract 进行汽车牌照识别时遇到了同样的问题。从那次经验中,我意识到 tesseract 对图像上文本的方向非常敏感。当图像上的文本是水平的时,Tesseract 可以很好地识别字母。水平方向的文本越多,您可以获得的效果就越好。

    因此,您必须创建一种算法,该算法将从您的验证码图像中检测每个字母,检测其方向并将其旋转以使其水平,然后进行预处理,然后使用 tesseract 处理这个旋转的水平图像并将其输出存储在你得到的字符串。然后去检测下一个字母并执行相同的过程并在结果字符串中添加 tesseract 输出。您还需要image transformation function 来旋转您的字母。而且您必须考虑找到检测到的字母的角落。可能是 this project 会帮助你,因为他们会在图像上旋转文本以提高 tesseract 的质量。

    【讨论】:

    • 感谢您的一些想法。创建一个算法来检测每个字母似乎是一项艰巨的任务。对现有工具有什么建议吗?您还认为任何深度学习技术会在我的情况下占上风吗?
    • @SimonHolloway,要查找每个字母,请尝试使用 opencv 中可用的任何图像分割,例如分水岭算法 docs.opencv.org/3.2.0/d3/db4/tutorial_py_watershed.html
    • 我建议看一些关于手写识别的论文,这些论文使用了 CTC 和 LSTM 等神经网络。这样的识别器需要对字形旋转更加健壮。
    猜你喜欢
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2015-12-22
    • 1970-01-01
    相关资源
    最近更新 更多