Tesseract实现简单的验证码识别

现在的网站普遍存在验证码,阻挡了一大批爬虫,如果使用的简单的验证码,那么可以使用Tesseract来实现简单的验证码识别

识别效果

Python实现

基于Tesseract的一个验证码识别程序如下:

# -*- coding:utf-8 -*-

from PIL import Image
import tesserocr


# 图片转灰度到二值化
def get_bin_img(img, threshold=128):
    img = img.convert('L')    # 灰度处理
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    img = img.point(table, '1') #二值化
    return img


if __name__ == '__main__':
    image = Image.open('1.jpg')
    image = get_bin_img(image, 127)
    result = tesserocr.image_to_text(image)
    print(result)

其主要步骤:

  • 读取图片
  • 图片转灰度图
  • 灰度图二值化,变黑白图
  • Tesseract 图片转文字
  • 输出识别出的结果

缺点:

只适合一些简单的验证码,如果文字有变形,有干扰线,有噪点,都是不适合的。虽然干扰线和噪点可以去除,但是干扰线在某些情况下并不好去除,噪点在二值化后,再使用九邻接点法判断去除噪点即可

发表评论 / Comment

用心评论~


Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/blog.dyboy.cn/content/templates/dyblog/footer.php:56) in /www/wwwroot/blog.dyboy.cn/include/lib/view.php on line 23