绝了,dddd带带弟弟OCR识别验证码

前言

上一篇介绍了通过 python 的 pytesserract 模块进行识别验证码,但是他只能识别一些简单的验证码,比如像这种。

遇到稍微复杂一点的验证码,就会识别不了。


那咋办?

网上找了一圈,介绍了不同的第三方平台识别验证码,像百度 ocr 、打码兔、超级鹰等,其中百度 ocr 呼声最高。

链接: https://cloud.baidu.com/product/ocr_others/webimage

奈何去试了一下,上面 3T9Q 这个验证码都不能识别出来...

而且收费还不低。

算了,后面在 github 上找到一个开源库,而且还很强大。这个库叫 ddddocr ,
带带弟弟,
哲哥的博客地址:https://wenanzhe.com/

不单单可以学到技术,还可以学到不少的人生道理。左边技术,右边人生道理。

网友们对这个库高度评价,


ddddorc 安装使用

环境要求:

python <= 3.9
Windows/Linux/Macos..

以下是在windows上安装

更新 pip

python -m pip install --upgrade pip -i https://pypi.douban.com/simple

不更新 pip 有可能会安装失败。

安装 ddddocr

pip install ddddocr -i https://pypi.douban.com/simple


使用方法

# -*- coding:utf-8 -*-
import ddddocr                       # 导入 ddddocr
ocr = ddddocr.DdddOcr()              # 实例化
with open('002.png', 'rb') as f:     # 打开图片
    img_bytes = f.read()             # 读取图片
res = ocr.classification(img_bytes)  # 识别
print(res)

单个图片的识别:


多个图片识别:

# -*- coding:utf-8 -*-
import ddddocr                       # 导入 ddddocr
ocr = ddddocr.DdddOcr()
for i in range(1, 4):
    with open(str(i) + '.png', 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    print(res)



有些大小写还是不能识别出来。

封装一下:

# -*- coding:utf-8 -*-
import ddddocr
ocr = ddddocr.DdddOcr()

def ddocr(file):
    try:
        with open(file, 'rb') as f:
            img_bytes = f.read()
        res = ocr.classification(img_bytes)
        return res
    except:
        print("获取验证码失败,请继续!")

r = ddocr('3.png')
print(r)


分界线----------------------------------------------

运行过程中,有可能会遇到这个问题。

ddddocr模块的项目使用pyinstaller 打包后报错 ImportError: Microsoft Visual C++ Redistributable for Visual Studio 2019 not installed on the machine.


解决办法:
安装Microsoft Visual C++ Redistributable 2019

https://aka.ms/vs/16/release/VC_redist.x64.exe

直接点击就可以下载了,下载后直接安装即可。

原文地址:https://www.cnblogs.com/wwho/p/15814248.html