【验证码识别】使用pytesseract识别数字验证码


前言

验证码是最简单有效防止恶意登录的方法,但是验证码对于我们自动化控制又带来了挑战,对于验证码的识别除了调用网上的OCR API,有没有一个不花钱的方法?

有,本文就来介绍一下,我们如何在本地进行验证码的识别,尤其是数字验证码的识别。


提示:以下是本篇文章正文内容,下面案例可供参考

一、pytesseract是什么?

pytesseract是Google做的OCR库,可以识别图片中的文字。pytesseract需要安装并配置好Tesseract OCR引擎才能正常工作。如果没有安装,可以参考官方文档进行安装和配置:https://github.com/tesseract-ocr/tesseract。或者参考其它博主的文章:
pytesseract安装和基本使用_吨吨不打野的博客-CSDN博客 https://blog.csdn.net/Castlehe/article/details/118751833

二、基本应用

import pytesseract
from PIL import Image

# 读取验证码图片
image = Image.open('captcha.png')

# 将图片转为灰度图像
image = image.convert('L')

# 对图像进行二值化处理
threshold = 127
table = []
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)
image = image.point(table, '1')

# 使用pytesseract进行识别
code = pytesseract.image_to_string(image)

print(code)

在以上示例中,我们首先使用PIL库打开验证码图片,然后将其转换为灰度图像,并进行二值化处理。然后,我们再使用pytesseract库对图像进行识别,并将结果打印出来。

能做到这步,说明pytesseract环境已配置OK,并且已经就别基本的识别功能,但要准确的识别数字验证码。建议您往下看。

三、进阶应用(指定识别字符白名单提高识别正确率)

import pytesseract

def captcha_recognize(img_path):
    # --psm 7 单行识别 , --oem 3 使用 LSTM OCR 引擎 , -c tessedit_char_whitelist=0123456789 只识别数字字符,也可以设置英文字母哦。
    num = pytesseract.image_to_string(img_path,lang='eng', \
            config='--psm 7 --oem 3 -c tessedit_char_whitelist=0123456789')
    return num

if __name__ == '__main__':
    # 测试
    num  = captcha_recognize(r'C:\yzm.png')
    print(num)

注:tesseract提供了OCR引擎模式。有如下四种:

  • 0 = 仅限原始Tesseract
  • 1 = 只有神经网络LSTM
  • 2 = Tesseract LSTM
  • 3 = 基于可用的默认值
    目前LSTM是无法支持白名单的

注:如下设置可聚焦识别数字和大小写字母:tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ


总结

本文的目的,旨在高效的识别数字验证码,当然pytesseract还有更多用途,感兴趣的朋友可以试试。


备注

在调用 tessarct 时,我们需要提供许多标志。三个最重要的是 -l 、 --oem 和 --psm 。

  • -l 参数控制输入文本的语言。我们将在此示例中使用 eng(英语),但您可以在此处查看 Tesseract 支持的所有语言:tesseract --list-langs

  • –oem 参数或 OCR 引擎模式控制 Tesseract 使用的算法类型。 您可以通过执行以下命令查看可用的 OCR 引擎模式:tesseract --help-oem

OCR Engine modes:
  0    Legacy engine only.
  1    Neural nets LSTM engine only.
  2    Legacy + LSTM engines.
  3    Default, based on what is available.

我们将使用 --oem 1 表示我们希望仅使用深度学习 LSTM 引擎。

  • –psm 参数控制 Tesseract 使用的自动页面分割模式:tesseract --help-psm
Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
       bypassing hacks that are Tesseract-specific.

对于 OCR 文本,模式 6 和 7 运行良好,但如果需要 OCR 处理大块文本,可尝试 3,默认模式。 每当您发现自己获得不正确的 OCR 结果时,强烈建议您调整 --psm,因为它会对您的输出 OCR 结果产生巨大影响。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT里的交易员

分享是一种快乐,打赏是一种肯定

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值