首页 > 编程知识 正文

python图形验证码识别(python验证码识别训练模型)

时间:2023-05-06 19:15:58 阅读:65037 作者:1250

文章目录1 .环境准备1.1pillow和安装pytesseract 1.2 tessera CT-ocr.exe 1.3更改pytesseract.py的ocr路径2 .测试识别效果3 .实战案例---古诗文网验证码

1 .环境准备1.1安装pillow和pytesseract

python模块库需要两个库: pillow和pytesseract。 直接安装pip install就好了。

pipinstallpillowpipinstallpytesseract 1.2安装Tesseract-OCR.exe下载地址: ocr下载地址

建议下载最新的稳定版本:

tessera CT-ocr-w64-setup-v5.0.0. 20190623.exe。

安装过程很简单,直接单击“下一步”结束。 在此期间,您可以设置默认安装路径,也可以自定义安装路径。 安装后,将该安装路径添加到环境变量中即可。 像我一样:

我的安装位置:

环境变量是这样添加的。

1.3更改了pytesseract.py的ocr路径。 在pip install pytesseract之后,可以在python解释器安装位置的软件包中找到pytesseract.py文件。

打开后更改:

至此,环境准备工作完成了。

2 .测试识别效果的ocr默认安装,可以从一开始就支持数字和字母的识别

准备验证码的照片:

将图像命名为captcha.png,并放置在与程序相同的目录中

importpytesseractfrompilimportimageimage=image.open (' captcha.png ' ) print ) pytesseract.image_to_string ) imp

再试一次对中文的识别吧。

识别前下载中文扩展语言包,

语言包地址

下载所需的语言包。 如下图所示,红框内为中文简体语言包。

下载后,请将软件包直接放入ocr程序安装目录的tessdata文件夹中。

找张照片吧:

importpytesseractfrompilimportimageimage=image.open (' 00.jpg ' ) print ) pytesseract.image_to_string ) image,

文本识别率可能不高,因此建议在图像识别之前,将图像灰度化后进行二值化

代码示例:

importpytesseractfrompilimportimagefile=r ' 00.jpg ' #首先对图像进行灰度二值化image=Image.open(file ) img=image.convert (文件) threshold=180table=[] forIinrange(256 ) : ifi threshold 3360 table.append (0) else: table.append(1)1) photo (1)图像的二值化)保存处理后的图像lang=) Chi_sim )表示识别简体中文。此外,参数config='---PSM6--- oem3-ctessedit _ char _ whitelist=01234567888 ' chi_sim ' ) print(content )3.实战案例古诗文网验证码自动识别注册importpytesseractfrompilimportimagefromseleniumimportwebdriverdefserdseractior 浏览器对象driver.maximize_window (驱动程序. implicitly _ wait ) 10 (驱动程序. get ) (urriver ) )的image=驱动程序打开3360 captcha=image.open (captcha _ path )图像对grap=captcha.convert )图像进行灰度处理的data=grap.load图像对象高度) )图像二值化for x in range(w ) w ) : for y in range(h ) h ) : if数据[ x,y ] 140:数据[ x, y] y )=255 code=pytesseract.image _ to _ string (grap ) #标识图像的returncodedeflogin ) driver, 代码) 3360 flag=true email=' 1242931802 @ QQ.com ' #注册的古诗网账户和密码password=' xxxx ' try : driver.find _ elememmend driver.find _ element _ by _ id (denglu ).click ) exceptexceptionasex : flag=falsereturnflagif _ _ name _ )。 from=http://so.Gushi Wen.org/user/collect.aspx ' captcha _ path='./获取captcha.png ' count=1driver=save _ cath code=recognize _ captcha (获取验证码print ) )验证码:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。