使用tesseract实现简单图形验证码识别

经验总结

识别类似这种纯数字的,建议使用3.02版本的tesseract。
识别中文字符,使用5.0即可。
需要自己训练模型的,使用4.0版本,因为4.0版本的文档稍微多一些,且支持使用自己训练的模型。

安装

按照官方文档提示进行安装
语言库选择math,chinese simplified.
windows下需要添加环境变量

变量名TESSDATA_PREFIX
变量值F:\Program Files (x86)\Tesseract-OCR\tessdata

测试

数字识别

在这里插入图片描述
进入tesseract安装目录
执行

tesseract.exe 22.png result

结果会存储在result.txt中
在这里插入图片描述

英文识别

在这里插入图片描述

中文文字识别

ps:对于中文的识别,5.0版本更加准确。
中文识别需要安装对应的中文训练集,下载地址:https://github.com/tesseract-ocr/tesseract/wiki
运行时需要指定训练集

tesseract.exe 44.png E:\result -l chi_sim

多语言应指定多个训练集

tesseract.exe 44.png E:\result -l chi_sim+eng

在这里插入图片描述

python对接

安装pytesseract: pip install pytesseract

import pytesseract
from PIL import Image
image = Image.open("44.png")
print(pytesseract.image_to_string(image,lang="chi_sim"))

在这里插入图片描述

可能会出现异常

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it’s not in your path

环境变量的问题,只需要把Tesseract-ORC添加到系统环境即可。
或者在识别之前修改tesseract_cmd的值,使其指向tesseract.exe

pytesseract.pytesseract.tesseract_cmd = r"D:\Tesseract-OCR\tesseract.exe"

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值