mirror of
https://github.com/sml2h3/ddddocr.git
synced 2025-05-02 15:21:26 +08:00
1.5.1 新增ocr结果概率表返回以及输出结果限定,重新整理文档
This commit is contained in:
parent
0fe1283187
commit
f0ee6b122e
459
README.md
459
README.md
@ -1,63 +1,237 @@
|
||||

|
||||
|
||||
# 带带弟弟OCR通用验证码离线本地识别SDK免费开源版
|
||||
|
||||
# 当前版本为1.4.11
|
||||
# DdddOcr 带带弟弟OCR通用验证码离线本地识别SDK免费开源版
|
||||
|
||||
## 下一版本更新计划,重新训练 中英数识别模型,即将支持自定义划定输出范围,如纯英文/纯数字/部分英文+部分数字/特殊符号/中文/各种混合等等,如果有已经标注好的数据也可以发送邮件(sml2h3@gmail.com)与我分享
|
||||
DdddOcr,其由作者与kerlomz共同合作完成,通过大批量生成随机数据后进行深度网络训练,本身并非针对任何一家验证码厂商而制作,本库使用效果完全靠玄学,可能可以识别,可能不能识别。
|
||||
|
||||
## 1.4.11 更新时间2024.01.10
|
||||
DdddOcr、最简依赖的理念,尽量减少用户的配置和使用成本,希望给每一位测试者带来舒适的体验
|
||||
|
||||
新增对透明黑色png格式图片得识别支持: `classification` 方法 新增 `png_fix` 参数,默认为False
|
||||
项目地址: [点我传送](https://github.com/sml2h3/ddddocr)
|
||||
|
||||
## 1.4.3更新内容
|
||||
<!-- PROJECT SHIELDS -->
|
||||
|
||||
本次升级的主要原因为,[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 的开源进行适配,使[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练出的模型可以直接无缝导入到ddddocr里面来使用
|
||||
[![Contributors][contributors-shield]][contributors-url]
|
||||
[![Forks][forks-shield]][forks-url]
|
||||
[![Stargazers][stars-shield]][stars-url]
|
||||
[![Issues][issues-shield]][issues-url]
|
||||
[![MIT License][license-shield]][license-url]
|
||||
|
||||
### 支持使用ddddocr调用 [dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练后的自定义模型
|
||||
<!-- PROJECT LOGO -->
|
||||
<br />
|
||||
|
||||
[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练后会在models目录里导出charsets.json和onnx模型
|
||||
<p align="center">
|
||||
<a href="https://github.com/shaojintian/Best_README_template/">
|
||||
<img src="https://cdn.wenanzhe.com/img/logo.png!/crop/700x500a400a500" alt="Logo">
|
||||
</a>
|
||||
<p align="center">
|
||||
一个容易使用的通用验证码识别python库
|
||||
<br />
|
||||
<a href="https://github.com/shaojintian/Best_README_template"><strong>探索本项目的文档 »</strong></a>
|
||||
<br />
|
||||
<br />
|
||||
·
|
||||
<a href="https://github.com/sml2h3/ddddocr/issues">报告Bug</a>
|
||||
·
|
||||
<a href="https://github.com/sml2h3/ddddocr/issues">提出新特性</a>
|
||||
</p>
|
||||
|
||||
如下所示,import_onnx_path为onnx所在地址,charsets_path为onnx所在地址
|
||||
```python
|
||||
import ddddocr
|
||||
</p>
|
||||
|
||||
ocr = ddddocr.DdddOcr(det=False, ocr=False, import_onnx_path="myproject_0.984375_139_13000_2022-02-26-15-34-13.onnx", charsets_path="charsets.json")
|
||||
|
||||
## 目录
|
||||
|
||||
with open('888e28774f815b01e871d474e5c84ff2.jpg', 'rb') as f:
|
||||
image_bytes = f.read()
|
||||
- [上手指南](#上手指南)
|
||||
- [环境支持](#环境支持)
|
||||
- [安装步骤](#安装步骤)
|
||||
- [文件目录说明](#文件目录说明)
|
||||
- [项目底层支持](#项目底层支持)
|
||||
- [使用文档](#使用文档)
|
||||
- [基础ocr识别能力](#基础ocr识别能力)
|
||||
- [目标检测能力](#目标检测能力)
|
||||
- [滑块检测](#滑块检测)
|
||||
- [OCR概率输出](#OCR概率输出)
|
||||
- [自定义OCR训练模型导入](#自定义OCR训练模型导入)
|
||||
- [版本控制](#版本控制)
|
||||
- [作者](#作者)
|
||||
- [鸣谢](#鸣谢)
|
||||
|
||||
res = ocr.classification(image_bytes)
|
||||
print(res)
|
||||
### 上手指南
|
||||
|
||||
###### 环境支持
|
||||
|
||||
|
||||
|
||||
| 系统 | CPU | GPU | 最大支持py版本 | 备注 |
|
||||
|------------------|-----|------|----------|--------------------------------------------------------------------|
|
||||
| Windows 64位 | √ | √ | 3.11 | 部分版本windows需要安装<a href="https://www.ghxi.com/yxkhj.html">vc运行库</a> |
|
||||
| Windows 32位 | × | × | - | |
|
||||
| Linux 64 / ARM64 | √ | √ | 3.11 | |
|
||||
| Linux 32 | × | × | - | |
|
||||
| Macos X64 | √ | √ | 3.11 | M1/M2/M3...芯片参考<a href="https://github.com/sml2h3/ddddocr/issues/67">#67</a> |
|
||||
|
||||
###### **安装步骤**
|
||||
|
||||
**i. 从pypi安装**
|
||||
```sh
|
||||
pip install ddddocr
|
||||
```
|
||||
|
||||
**ii. 从源码安装**
|
||||
```sh
|
||||
git clone https://github.com/sml2h3/ddddocr.git
|
||||
cd ddddocr
|
||||
python setup.py
|
||||
```
|
||||
|
||||
**请勿直接在ddddocr项目的根目录内直接import ddddocr**,请确保你的开发项目目录名称不为ddddocr,此为基础常识。
|
||||
|
||||
### 文件目录说明
|
||||
eg:
|
||||
|
||||
```
|
||||
ddddocr
|
||||
├── MANIFEST.in
|
||||
├── LICENSE
|
||||
├── README.md
|
||||
├── /ddddocr/
|
||||
│ │── __init__.py 主代码库文件
|
||||
│ │── common.onnx 新ocr模型
|
||||
│ │── common_det.onnx 目标检测模型
|
||||
│ │── common_old.onnx 老ocr模型
|
||||
│ │── logo.png
|
||||
│ │── README.md
|
||||
│ │── requirements.txt
|
||||
├── logo.png
|
||||
└── setup.py
|
||||
|
||||
```
|
||||
|
||||
# 捐赠 (如果项目有帮助到您,可以选择捐赠一些费用用于ddddocr的后续版本维护,本项目长期维护)
|
||||
### 项目底层支持
|
||||
|
||||

|
||||

|
||||
本项目基于[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练所得,训练底层框架位pytorch,ddddocr推理底层抵赖于[onnxruntime](https://pypi.org/project/onnxruntime/),故本项目的最大兼容性与python版本支持主要取决于[onnxruntime](https://pypi.org/project/onnxruntime/)。
|
||||
|
||||
# 赞助合作商
|
||||
### 使用文档
|
||||
|
||||
| | 赞助合作商 | 推荐理由 |
|
||||
|------------------------------------------------------------|------------|--------------------------------------------------------------------------------------------------|
|
||||
|  | [YesCaptcha](https://yescaptcha.com/i/NSwk7i) | 谷歌reCaptcha验证码 / hCaptcha验证码 / funCaptcha验证码商业级识别接口 [点我](https://yescaptcha.com/i/NSwk7i) 直达VIP4 |
|
||||
|  | [Malenia](https://malenia.iinti.cn/malenia-doc/) | Malenia企业级代理IP网关平台/代理IP分销软件 |
|
||||
##### i. 基础ocr识别能力
|
||||
|
||||
# 1.4.0版本更新内容
|
||||
主要用于识别单行文字,即文字部分占据图片的主体部分,例如常见的英数验证码等,本项目可以对中文、英文(随机大小写or通过设置结果范围圈定大小写)、数字以及部分特殊字符。
|
||||
|
||||
本次更新新增了两种滑块识别算法,算法非深度神经网络实现,仅使用opencv和PIL完成。
|
||||
```python
|
||||
# example.py
|
||||
import ddddocr
|
||||
|
||||
## 算法1
|
||||
小滑块为单独的png图片,背景是透明图,如下图
|
||||
ocr = ddddocr.DdddOcr()
|
||||
|
||||

|
||||
image = open("example.jpg", "rb").read()
|
||||
result = ocr.classification(image)
|
||||
print(result)
|
||||
```
|
||||
|
||||
然后背景为带小滑块坑位的,如下图
|
||||
|
||||

|
||||
本库内置有两套ocr模型,默认情况下不会自动切换,需要在初始化ddddocr的时候通过参数进行切换
|
||||
|
||||
```python
|
||||
```python
|
||||
# example.py
|
||||
import ddddocr
|
||||
|
||||
ocr = ddddocr.DdddOcr(beta=True) # 切换为第二套ocr模型
|
||||
|
||||
image = open("example.jpg", "rb").read()
|
||||
result = ocr.classification(image)
|
||||
print(result)
|
||||
```
|
||||
|
||||
**提示**
|
||||
对于部分透明黑色png格式图片得识别支持: `classification` 方法 使用 `png_fix` 参数,默认为False
|
||||
|
||||
```python
|
||||
ocr.classification(image, png_fix=True)
|
||||
```
|
||||
|
||||
**注意**
|
||||
|
||||
之前发现很多人喜欢在每次ocr识别的时候都重新初始化ddddocr,即每次都执行```ocr = ddddocr.DdddOcr()```,这是错误的,通常来说只需要初始化一次即可,因为每次初始化和初始化后的第一次识别速度都非常慢
|
||||
|
||||
|
||||
**参考例图**
|
||||
|
||||
包括且不限于以下图片
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/20210715211733855.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/78b7f57d-371d-4b65-afb2-d19608ae1892.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142305.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142325.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/2AMLyA_fd83e1f1800e829033417ae6dd0e0ae0.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/aabd_181ae81dd5526b8b89f987d1179266ce.jpg" alt="captcha" width="150">
|
||||
<br />
|
||||
<img src="https://cdn.wenanzhe.com/img/2bghz_b504e9f9de1ed7070102d21c6481e0cf.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/0000_z4ecc2p65rxc610x.jpg" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/2acd_0586b6b36858a4e8a9939db8a7ec07b7.jpg" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/2a8r_79074e311d573d31e1630978fe04b990.jpg" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/aftf_C2vHZlk8540y3qAmCM.bmp" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20211226144057.png" alt="captcha" width="150">
|
||||
|
||||
##### ii. 目标检测能力
|
||||
|
||||
主要用于快速检测出图像中可能的目标主体位置,由于被检测出的目标不一定为文字,所以本功能仅提供目标的bbox位置 **(在⽬标检测⾥,我们通常使⽤bbox(bounding box,缩写是 bbox)来描述⽬标位置。bbox是⼀个矩形框,可以由矩形左上⻆的 x 和 y 轴坐标与右下⻆的 x 和 y 轴坐标确定)**
|
||||
|
||||
如果使用过程中无需调用ocr功能,可以在初始化时通过传参`ocr=False`关闭ocr功能,开启目标检测需要传入参数`det=True`
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
import cv2
|
||||
|
||||
det = ddddocr.DdddOcr(det=True)
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
|
||||
bboxes = det.detection(image)
|
||||
print(bboxes)
|
||||
|
||||
im = cv2.imread("test.jpg")
|
||||
|
||||
for bbox in bboxes:
|
||||
x1, y1, x2, y2 = bbox
|
||||
im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
|
||||
|
||||
cv2.imwrite("result.jpg", im)
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
**参考例图**
|
||||
|
||||
包括且不限于以下图片
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_1.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_2.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_3.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_4.jpg" alt="captcha" width="200">
|
||||
<br />
|
||||
<img src="https://cdn.wenanzhe.com/img/result.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/result2.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/result4.jpg" alt="captcha" width="200">
|
||||
|
||||
##### Ⅲ. 滑块检测
|
||||
|
||||
本项目的滑块检测功能并非AI识别实现,均为opencv内置算法实现。可能对于截图党用户没那么友好~,如果使用过程中无需调用ocr功能或目标检测功能,可以在初始化时通过传参`ocr=False`关闭ocr功能或`det=False`来关闭目标检测功能
|
||||
|
||||
本功能内置两套算法实现,适用于两种不同情况,具体请参考以下说明
|
||||
|
||||
**a.算法1**
|
||||
|
||||
算法1原理是通过滑块图像的边缘在背景图中计算找到相对应的坑位,可以分别获取到滑块图和背景图,滑块图为透明背景图
|
||||
|
||||
滑块图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/b.png" alt="captcha" width="50">
|
||||
|
||||
背景图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/a.png" alt="captcha" width="350">
|
||||
|
||||
```python
|
||||
det = ddddocr.DdddOcr(det=False, ocr=False)
|
||||
|
||||
with open('target.png', 'rb') as f:
|
||||
@ -70,7 +244,10 @@ print(res)
|
||||
|
||||
print(res)
|
||||
```
|
||||
*提示:如果小图无过多背景部分,则可以添加simple_target参数, 通常为jpg或者bmp格式的图片*
|
||||
由于滑块图可能存在透明边框的问题,导致计算结果不一定准确,需要自行估算滑块图透明边框的宽度用于修正得出的bbox
|
||||
|
||||
*提示:如果滑块无过多背景部分,则可以添加simple_target参数, 通常为jpg或者bmp格式的图片*
|
||||
|
||||
```python
|
||||
slide = ddddocr.DdddOcr(det=False, ocr=False)
|
||||
|
||||
@ -84,16 +261,20 @@ print(res)
|
||||
|
||||
print(res)
|
||||
```
|
||||
## 算法2
|
||||
一张图为带坑位的原图,如下图
|
||||
|
||||

|
||||
**a.算法2**
|
||||
|
||||
一张图为原图,如下图
|
||||
|
||||

|
||||
算法2是通过比较两张图的不同之处进行判断滑块目标坑位的位置
|
||||
|
||||
```python
|
||||
参考图a,带有目标坑位阴影的全图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/bg.jpg" alt="captcha" width="350">
|
||||
|
||||
参考图b,全图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/fullpage.jpg" alt="captcha" width="350">
|
||||
|
||||
```python
|
||||
slide = ddddocr.DdddOcr(det=False, ocr=False)
|
||||
|
||||
with open('bg.jpg', 'rb') as f:
|
||||
@ -109,165 +290,97 @@ print(res)
|
||||
print(res)
|
||||
```
|
||||
|
||||
## 更新内容2
|
||||
添加全局ocr关闭参数,初始化时传入
|
||||
##### Ⅳ. OCR概率输出
|
||||
|
||||
`dddd = ddddocr.DdddOcr(ocr=False)`
|
||||
为了提供更灵活的ocr结果控制与范围限定,项目支持对ocr结果进行范围限定。
|
||||
|
||||
则为关闭ocr功能,如果det = True,则会自动关闭ocr
|
||||
可以通过在调用`classification`方法的时候传参`probability=True`,此时`classification`方法将返回全字符表的概率
|
||||
当然也可以通过`set_ranges`方法设置输出字符范围来限定返回的结果。
|
||||
|
||||
Ⅰ. `set_ranges` 方法限定返回字符返回
|
||||
|
||||
# 1.3.1版本更新内容
|
||||
本方法接受1个参数,如果输入为int类型为内置的字符集限制,string类型则为自定义的字符集
|
||||
|
||||
想必很多做验证码的新手,一定头疼碰到点选类型的图像,做样本费时费力,神经网络不会写,训练设备太昂贵,模型效果又不好。
|
||||
如果为int类型,请参考下表
|
||||
|
||||
市场上常见的点选类验证码图片如下图所示
|
||||
| 参数值 | 意义 |
|
||||
|-----|-----------------------------------|
|
||||
| 0 | 纯整数0-9 |
|
||||
| 1 | 纯小写英文a-z |
|
||||
| 2 | 纯大写英文A-Z |
|
||||
| 3 | 小写英文a-z + 大写英文A-Z |
|
||||
| 4 | 小写英文a-z + 整数0-9 |
|
||||
| 5 | 大写英文A-Z + 整数0-9 |
|
||||
| 6 | 小写英文a-z + 大写英文A-Z + 整数0-9 |
|
||||
| 7 | 默认字符库 - 小写英文a-z - 大写英文A-Z - 整数0-9 |
|
||||
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
那么今天,他来了,ddddocr带着重磅更新大摇大摆的走来了。
|
||||
# 简介
|
||||
ddddocr是由sml2h3开发的专为验证码厂商进行对自家新版本验证码难易强度进行验证的一个python库,其由作者与kerlomz共同合作完成,通过大批量生成随机数据后进行深度网络训练,本身并非针对任何一家验证码厂商而制作,本库使用效果完全靠玄学,可能可以识别,可能不能识别。
|
||||
|
||||
ddddocr奉行着开箱即用、最简依赖的理念,尽量减少用户的配置和使用成本,希望给每一位测试者带来舒适的体验
|
||||
|
||||
项目地址: [点我传送](https://github.com/sml2h3/ddddocr)
|
||||
|
||||
# 更新说明
|
||||
|
||||
本次更新其实分为两部分,其中有一部分是在1.2.0版本就已经更新了,但是在这里还是有必要提一下的。
|
||||
|
||||
## 第一部分 OCR识别部分
|
||||
|
||||
在1.2.0开始,ddddocr的识别部分进行了一次beta更新,主要更新在于网络结构主体的升级,其训练数据并没有发生过多的改变,所以理论上在识别结果上,原先可能识别效果的很好的图形在1.2.0上有一小部分概率会有一定程度的下降,也有可能原本识别不好的图形在1.2.0之后效果却变得特别好。
|
||||
测试代码:
|
||||
|
||||
如果为string类型请传入一段不包含空格的文本,其中的每个字符均为一个待选词
|
||||
如:`"0123456789+-x/=""`
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
|
||||
ocr = ddddocr.DdddOcr()
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
image = open("test.jpg", "rb").read()
|
||||
ocr.set_ranges("0123456789+-x/=")
|
||||
result = ocr.classification(image, probability=True)
|
||||
s = ""
|
||||
for i in result['probability']:
|
||||
s += result['charsets'][i.index(max(i))]
|
||||
|
||||
res = ocr.classification(image)
|
||||
print(res)
|
||||
```
|
||||
通过在初始化ddddocr的时候使用beta参数即可快速切换新模型
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
|
||||
ocr = ddddocr.DdddOcr(beta=True)
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
|
||||
res = ocr.classification(image)
|
||||
print(res)
|
||||
```
|
||||
|
||||
OCR部分应该已经有很多人做了测试,在这里就放一部分网友的测试图片。
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
等等更多图片等你测试哟~
|
||||
|
||||
## 第二部分 目标检测部分
|
||||
在本次1.3.0的更新中,目标检测部分隆重登场!
|
||||
目标检测部分同样也是由大量随机合成数据训练而成,对于现在已有的点选验证码图片或者未知的验证码图片都有可能具备一定的识别能力,适用于文字点选和图标点选。
|
||||
简单来说,对于点选类的验证码,可以快速的检测出图片上的文字或者图标。
|
||||
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
import cv2
|
||||
|
||||
det = ddddocr.DdddOcr(det=True)
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
|
||||
poses = det.detection(image)
|
||||
print(poses)
|
||||
|
||||
im = cv2.imread("test.jpg")
|
||||
|
||||
for box in poses:
|
||||
x1, y1, x2, y2 = box
|
||||
im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
|
||||
|
||||
cv2.imwrite("result.jpg", im)
|
||||
print(s)
|
||||
|
||||
```
|
||||
|
||||
举些例子:
|
||||
##### Ⅴ. 自定义OCR训练模型导入
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
本项目支持导入来自于 [dddd_trainer](https://github.com/sml2h3/dddd_trainer) 进行自定义训练后的模型,参考导入代码为
|
||||
|
||||
以上只是目前我能找到的点选验证码图片,做了一个简单的测试。
|
||||
```python
|
||||
import ddddocr
|
||||
|
||||
# 安装
|
||||
ocr = ddddocr.DdddOcr(det=False, ocr=False, import_onnx_path="myproject_0.984375_139_13000_2022-02-26-15-34-13.onnx", charsets_path="charsets.json")
|
||||
|
||||
## 环境支持
|
||||
with open('test.jpg', 'rb') as f:
|
||||
image_bytes = f.read()
|
||||
|
||||
`python <= 3.9`
|
||||
res = ocr.classification(image_bytes)
|
||||
print(res)
|
||||
|
||||
`Windows/Linux/Macos..`
|
||||
```
|
||||
|
||||
暂时不支持Macbook M1(X),M1(X)用户需要自己编译onnxruntime才可以使用
|
||||
### 版本控制
|
||||
|
||||
## 安装命令
|
||||
该项目使用Git进行版本管理。您可以在repository参看当前可用版本。
|
||||
|
||||
`pip install ddddocr`
|
||||
### 作者
|
||||
|
||||
以上命令将自动安装符合自己电脑环境的最新ddddocr
|
||||
|
||||
## 拓展 一键部署ddddocr api,支持docker部署
|
||||
|
||||
[github](https://github.com/sml2h3/ocr_api_server)
|
||||
|
||||
[gitee](https://gitee.com/fkgeek/ocr_api_server)
|
||||
|
||||
## 爬虫框架推荐
|
||||
|
||||
[feapder](https://github.com/Boris-code/feapder)
|
||||
|
||||
[crawlab](https://github.com/crawlab-team/crawlab)
|
||||
|
||||
# 交流群 (个人微信太懒了不一定会通过)
|
||||
sml2h3@gamil.com
|
||||
|
||||

|
||||

|
||||
<img src="https://cdn.wenanzhe.com/img/mmqrcode1640418911274.png!/scale/50" alt="wechat" width="150">
|
||||
|
||||
*好友数过多不一定通过,有问题可以在issue进行交流*
|
||||
|
||||
### 版权说明
|
||||
|
||||
该项目签署了MIT 授权许可,详情请参阅 [LICENSE](https://github.com/sml2h3/ddddocr/blob/master/LICENSE)
|
||||
|
||||
|
||||
<!-- links -->
|
||||
[your-project-path]:sml2h3/ddddocr
|
||||
[contributors-shield]: https://img.shields.io/github/contributors/sml2h3/ddddocr?style=flat-square
|
||||
[contributors-url]: https://github.com/shaojintian/Best_README_template/graphs/contributors
|
||||
[forks-shield]: https://img.shields.io/github/forks/sml2h3/ddddocr?style=flat-square
|
||||
[forks-url]: https://github.com/shaojintian/Best_README_template/network/members
|
||||
[stars-shield]: https://img.shields.io/github/stars/sml2h3/ddddocr?style=flat-square
|
||||
[stars-url]: https://github.com/shaojintian/Best_README_template/stargazers
|
||||
[issues-shield]: https://img.shields.io/github/issues/sml2h3/ddddocr?style=flat-square
|
||||
[issues-url]: https://img.shields.io/github/issues/sml2h3/ddddocr.svg
|
||||
[license-shield]: https://img.shields.io/github/license/sml2h3/ddddocr?style=flat-square
|
||||
[license-url]: https://github.com/sml2h3/ddddocr/blob/master/LICENSE
|
||||
|
||||
|
||||
# 主要贡献者
|
||||
|
||||
<span style="margin: 0 5px;" ><a href="https://github.com/kerlomz" ><img src="https://cdn.wenanzhe.com/img/16838877.jpg!/scale/50" /></a></span>
|
||||
|
||||
## Star 历史
|
||||
|
||||
[](https://star-history.com/#sml2h3/ddddocr&Date)
|
||||
|
@ -1,63 +1,237 @@
|
||||

|
||||
|
||||
# 带带弟弟OCR通用验证码离线本地识别SDK免费开源版
|
||||
|
||||
# 当前版本为1.4.11
|
||||
# DdddOcr 带带弟弟OCR通用验证码离线本地识别SDK免费开源版
|
||||
|
||||
## 下一版本更新计划,重新训练 中英数识别模型,即将支持自定义划定输出范围,如纯英文/纯数字/部分英文+部分数字/特殊符号/中文/各种混合等等,如果有已经标注好的数据也可以发送邮件(sml2h3@gmail.com)与我分享
|
||||
DdddOcr,其由作者与kerlomz共同合作完成,通过大批量生成随机数据后进行深度网络训练,本身并非针对任何一家验证码厂商而制作,本库使用效果完全靠玄学,可能可以识别,可能不能识别。
|
||||
|
||||
## 1.4.11 更新时间2024.01.10
|
||||
DdddOcr、最简依赖的理念,尽量减少用户的配置和使用成本,希望给每一位测试者带来舒适的体验
|
||||
|
||||
新增对透明黑色png格式图片得识别支持: `classification` 方法 新增 `png_fix` 参数,默认为False
|
||||
项目地址: [点我传送](https://github.com/sml2h3/ddddocr)
|
||||
|
||||
## 1.4.3更新内容
|
||||
<!-- PROJECT SHIELDS -->
|
||||
|
||||
本次升级的主要原因为,[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 的开源进行适配,使[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练出的模型可以直接无缝导入到ddddocr里面来使用
|
||||
[![Contributors][contributors-shield]][contributors-url]
|
||||
[![Forks][forks-shield]][forks-url]
|
||||
[![Stargazers][stars-shield]][stars-url]
|
||||
[![Issues][issues-shield]][issues-url]
|
||||
[![MIT License][license-shield]][license-url]
|
||||
|
||||
### 支持使用ddddocr调用 [dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练后的自定义模型
|
||||
<!-- PROJECT LOGO -->
|
||||
<br />
|
||||
|
||||
[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练后会在models目录里导出charsets.json和onnx模型
|
||||
<p align="center">
|
||||
<a href="https://github.com/shaojintian/Best_README_template/">
|
||||
<img src="https://cdn.wenanzhe.com/img/logo.png!/crop/700x500a400a500" alt="Logo">
|
||||
</a>
|
||||
<p align="center">
|
||||
一个容易使用的通用验证码识别python库
|
||||
<br />
|
||||
<a href="https://github.com/shaojintian/Best_README_template"><strong>探索本项目的文档 »</strong></a>
|
||||
<br />
|
||||
<br />
|
||||
·
|
||||
<a href="https://github.com/sml2h3/ddddocr/issues">报告Bug</a>
|
||||
·
|
||||
<a href="https://github.com/sml2h3/ddddocr/issues">提出新特性</a>
|
||||
</p>
|
||||
|
||||
如下所示,import_onnx_path为onnx所在地址,charsets_path为onnx所在地址
|
||||
```python
|
||||
import ddddocr
|
||||
</p>
|
||||
|
||||
ocr = ddddocr.DdddOcr(det=False, ocr=False, import_onnx_path="myproject_0.984375_139_13000_2022-02-26-15-34-13.onnx", charsets_path="charsets.json")
|
||||
|
||||
## 目录
|
||||
|
||||
with open('888e28774f815b01e871d474e5c84ff2.jpg', 'rb') as f:
|
||||
image_bytes = f.read()
|
||||
- [上手指南](#上手指南)
|
||||
- [环境支持](#环境支持)
|
||||
- [安装步骤](#安装步骤)
|
||||
- [文件目录说明](#文件目录说明)
|
||||
- [项目底层支持](#项目底层支持)
|
||||
- [使用文档](#使用文档)
|
||||
- [基础ocr识别能力](#基础ocr识别能力)
|
||||
- [目标检测能力](#目标检测能力)
|
||||
- [滑块检测](#滑块检测)
|
||||
- [OCR概率输出](#OCR概率输出)
|
||||
- [自定义OCR训练模型导入](#自定义OCR训练模型导入)
|
||||
- [版本控制](#版本控制)
|
||||
- [作者](#作者)
|
||||
- [鸣谢](#鸣谢)
|
||||
|
||||
res = ocr.classification(image_bytes)
|
||||
print(res)
|
||||
### 上手指南
|
||||
|
||||
###### 环境支持
|
||||
|
||||
|
||||
|
||||
| 系统 | CPU | GPU | 最大支持py版本 | 备注 |
|
||||
|------------------|-----|------|----------|--------------------------------------------------------------------|
|
||||
| Windows 64位 | √ | √ | 3.11 | 部分版本windows需要安装<a href="https://www.ghxi.com/yxkhj.html">vc运行库</a> |
|
||||
| Windows 32位 | × | × | - | |
|
||||
| Linux 64 / ARM64 | √ | √ | 3.11 | |
|
||||
| Linux 32 | × | × | - | |
|
||||
| Macos X64 | √ | √ | 3.11 | M1/M2/M3...芯片参考<a href="https://github.com/sml2h3/ddddocr/issues/67">#67</a> |
|
||||
|
||||
###### **安装步骤**
|
||||
|
||||
**i. 从pypi安装**
|
||||
```sh
|
||||
pip install ddddocr
|
||||
```
|
||||
|
||||
**ii. 从源码安装**
|
||||
```sh
|
||||
git clone https://github.com/sml2h3/ddddocr.git
|
||||
cd ddddocr
|
||||
python setup.py
|
||||
```
|
||||
|
||||
**请勿直接在ddddocr项目的根目录内直接import ddddocr**,请确保你的开发项目目录名称不为ddddocr,此为基础常识。
|
||||
|
||||
### 文件目录说明
|
||||
eg:
|
||||
|
||||
```
|
||||
ddddocr
|
||||
├── MANIFEST.in
|
||||
├── LICENSE
|
||||
├── README.md
|
||||
├── /ddddocr/
|
||||
│ │── __init__.py 主代码库文件
|
||||
│ │── common.onnx 新ocr模型
|
||||
│ │── common_det.onnx 目标检测模型
|
||||
│ │── common_old.onnx 老ocr模型
|
||||
│ │── logo.png
|
||||
│ │── README.md
|
||||
│ │── requirements.txt
|
||||
├── logo.png
|
||||
└── setup.py
|
||||
|
||||
```
|
||||
|
||||
# 捐赠 (如果项目有帮助到您,可以选择捐赠一些费用用于ddddocr的后续版本维护,本项目长期维护)
|
||||
### 项目底层支持
|
||||
|
||||

|
||||

|
||||
本项目基于[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练所得,训练底层框架位pytorch,ddddocr推理底层抵赖于[onnxruntime](https://pypi.org/project/onnxruntime/),故本项目的最大兼容性与python版本支持主要取决于[onnxruntime](https://pypi.org/project/onnxruntime/)。
|
||||
|
||||
# 赞助合作商
|
||||
### 使用文档
|
||||
|
||||
| | 赞助合作商 | 推荐理由 |
|
||||
|------------------------------------------------------------|------------|--------------------------------------------------------------------------------------------------|
|
||||
|  | [YesCaptcha](https://yescaptcha.com/i/NSwk7i) | 谷歌reCaptcha验证码 / hCaptcha验证码 / funCaptcha验证码商业级识别接口 [点我](https://yescaptcha.com/i/NSwk7i) 直达VIP4 |
|
||||
|  | [Malenia](https://malenia.iinti.cn/malenia-doc/) | Malenia企业级代理IP网关平台/代理IP分销软件 |
|
||||
##### i. 基础ocr识别能力
|
||||
|
||||
# 1.4.0版本更新内容
|
||||
主要用于识别单行文字,即文字部分占据图片的主体部分,例如常见的英数验证码等,本项目可以对中文、英文(随机大小写or通过设置结果范围圈定大小写)、数字以及部分特殊字符。
|
||||
|
||||
本次更新新增了两种滑块识别算法,算法非深度神经网络实现,仅使用opencv和PIL完成。
|
||||
```python
|
||||
# example.py
|
||||
import ddddocr
|
||||
|
||||
## 算法1
|
||||
小滑块为单独的png图片,背景是透明图,如下图
|
||||
ocr = ddddocr.DdddOcr()
|
||||
|
||||

|
||||
image = open("example.jpg", "rb").read()
|
||||
result = ocr.classification(image)
|
||||
print(result)
|
||||
```
|
||||
|
||||
然后背景为带小滑块坑位的,如下图
|
||||
|
||||

|
||||
本库内置有两套ocr模型,默认情况下不会自动切换,需要在初始化ddddocr的时候通过参数进行切换
|
||||
|
||||
```python
|
||||
```python
|
||||
# example.py
|
||||
import ddddocr
|
||||
|
||||
ocr = ddddocr.DdddOcr(beta=True) # 切换为第二套ocr模型
|
||||
|
||||
image = open("example.jpg", "rb").read()
|
||||
result = ocr.classification(image)
|
||||
print(result)
|
||||
```
|
||||
|
||||
**提示**
|
||||
对于部分透明黑色png格式图片得识别支持: `classification` 方法 使用 `png_fix` 参数,默认为False
|
||||
|
||||
```python
|
||||
ocr.classification(image, png_fix=True)
|
||||
```
|
||||
|
||||
**注意**
|
||||
|
||||
之前发现很多人喜欢在每次ocr识别的时候都重新初始化ddddocr,即每次都执行```ocr = ddddocr.DdddOcr()```,这是错误的,通常来说只需要初始化一次即可,因为每次初始化和初始化后的第一次识别速度都非常慢
|
||||
|
||||
|
||||
**参考例图**
|
||||
|
||||
包括且不限于以下图片
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/20210715211733855.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/78b7f57d-371d-4b65-afb2-d19608ae1892.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142305.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142325.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/2AMLyA_fd83e1f1800e829033417ae6dd0e0ae0.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/aabd_181ae81dd5526b8b89f987d1179266ce.jpg" alt="captcha" width="150">
|
||||
<br />
|
||||
<img src="https://cdn.wenanzhe.com/img/2bghz_b504e9f9de1ed7070102d21c6481e0cf.png" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/0000_z4ecc2p65rxc610x.jpg" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/2acd_0586b6b36858a4e8a9939db8a7ec07b7.jpg" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/2a8r_79074e311d573d31e1630978fe04b990.jpg" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/aftf_C2vHZlk8540y3qAmCM.bmp" alt="captcha" width="150">
|
||||
<img src="https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20211226144057.png" alt="captcha" width="150">
|
||||
|
||||
##### ii. 目标检测能力
|
||||
|
||||
主要用于快速检测出图像中可能的目标主体位置,由于被检测出的目标不一定为文字,所以本功能仅提供目标的bbox位置 **(在⽬标检测⾥,我们通常使⽤bbox(bounding box,缩写是 bbox)来描述⽬标位置。bbox是⼀个矩形框,可以由矩形左上⻆的 x 和 y 轴坐标与右下⻆的 x 和 y 轴坐标确定)**
|
||||
|
||||
如果使用过程中无需调用ocr功能,可以在初始化时通过传参`ocr=False`关闭ocr功能,开启目标检测需要传入参数`det=True`
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
import cv2
|
||||
|
||||
det = ddddocr.DdddOcr(det=True)
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
|
||||
bboxes = det.detection(image)
|
||||
print(bboxes)
|
||||
|
||||
im = cv2.imread("test.jpg")
|
||||
|
||||
for bbox in bboxes:
|
||||
x1, y1, x2, y2 = bbox
|
||||
im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
|
||||
|
||||
cv2.imwrite("result.jpg", im)
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
**参考例图**
|
||||
|
||||
包括且不限于以下图片
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_1.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_2.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_3.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/page1_4.jpg" alt="captcha" width="200">
|
||||
<br />
|
||||
<img src="https://cdn.wenanzhe.com/img/result.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/result2.jpg" alt="captcha" width="200">
|
||||
<img src="https://cdn.wenanzhe.com/img/result4.jpg" alt="captcha" width="200">
|
||||
|
||||
##### Ⅲ. 滑块检测
|
||||
|
||||
本项目的滑块检测功能并非AI识别实现,均为opencv内置算法实现。可能对于截图党用户没那么友好~,如果使用过程中无需调用ocr功能或目标检测功能,可以在初始化时通过传参`ocr=False`关闭ocr功能或`det=False`来关闭目标检测功能
|
||||
|
||||
本功能内置两套算法实现,适用于两种不同情况,具体请参考以下说明
|
||||
|
||||
**a.算法1**
|
||||
|
||||
算法1原理是通过滑块图像的边缘在背景图中计算找到相对应的坑位,可以分别获取到滑块图和背景图,滑块图为透明背景图
|
||||
|
||||
滑块图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/b.png" alt="captcha" width="50">
|
||||
|
||||
背景图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/a.png" alt="captcha" width="350">
|
||||
|
||||
```python
|
||||
det = ddddocr.DdddOcr(det=False, ocr=False)
|
||||
|
||||
with open('target.png', 'rb') as f:
|
||||
@ -70,7 +244,10 @@ print(res)
|
||||
|
||||
print(res)
|
||||
```
|
||||
*提示:如果小图无过多背景部分,则可以添加simple_target参数, 通常为jpg或者bmp格式的图片*
|
||||
由于滑块图可能存在透明边框的问题,导致计算结果不一定准确,需要自行估算滑块图透明边框的宽度用于修正得出的bbox
|
||||
|
||||
*提示:如果滑块无过多背景部分,则可以添加simple_target参数, 通常为jpg或者bmp格式的图片*
|
||||
|
||||
```python
|
||||
slide = ddddocr.DdddOcr(det=False, ocr=False)
|
||||
|
||||
@ -84,16 +261,20 @@ print(res)
|
||||
|
||||
print(res)
|
||||
```
|
||||
## 算法2
|
||||
一张图为带坑位的原图,如下图
|
||||
|
||||

|
||||
**a.算法2**
|
||||
|
||||
一张图为原图,如下图
|
||||
|
||||

|
||||
算法2是通过比较两张图的不同之处进行判断滑块目标坑位的位置
|
||||
|
||||
```python
|
||||
参考图a,带有目标坑位阴影的全图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/bg.jpg" alt="captcha" width="350">
|
||||
|
||||
参考图b,全图
|
||||
|
||||
<img src="https://cdn.wenanzhe.com/img/fullpage.jpg" alt="captcha" width="350">
|
||||
|
||||
```python
|
||||
slide = ddddocr.DdddOcr(det=False, ocr=False)
|
||||
|
||||
with open('bg.jpg', 'rb') as f:
|
||||
@ -109,165 +290,97 @@ print(res)
|
||||
print(res)
|
||||
```
|
||||
|
||||
## 更新内容2
|
||||
添加全局ocr关闭参数,初始化时传入
|
||||
##### Ⅳ. OCR概率输出
|
||||
|
||||
`dddd = ddddocr.DdddOcr(ocr=False)`
|
||||
为了提供更灵活的ocr结果控制与范围限定,项目支持对ocr结果进行范围限定。
|
||||
|
||||
则为关闭ocr功能,如果det = True,则会自动关闭ocr
|
||||
可以通过在调用`classification`方法的时候传参`probability=True`,此时`classification`方法将返回全字符表的概率
|
||||
当然也可以通过`set_ranges`方法设置输出字符范围来限定返回的结果。
|
||||
|
||||
Ⅰ. `set_ranges` 方法限定返回字符返回
|
||||
|
||||
# 1.3.1版本更新内容
|
||||
本方法接受1个参数,如果输入为int类型为内置的字符集限制,string类型则为自定义的字符集
|
||||
|
||||
想必很多做验证码的新手,一定头疼碰到点选类型的图像,做样本费时费力,神经网络不会写,训练设备太昂贵,模型效果又不好。
|
||||
如果为int类型,请参考下表
|
||||
|
||||
市场上常见的点选类验证码图片如下图所示
|
||||
| 参数值 | 意义 |
|
||||
|-----|-----------------------------------|
|
||||
| 0 | 纯整数0-9 |
|
||||
| 1 | 纯小写英文a-z |
|
||||
| 2 | 纯大写英文A-Z |
|
||||
| 3 | 小写英文a-z + 大写英文A-Z |
|
||||
| 4 | 小写英文a-z + 整数0-9 |
|
||||
| 5 | 大写英文A-Z + 整数0-9 |
|
||||
| 6 | 小写英文a-z + 大写英文A-Z + 整数0-9 |
|
||||
| 7 | 默认字符库 - 小写英文a-z - 大写英文A-Z - 整数0-9 |
|
||||
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
那么今天,他来了,ddddocr带着重磅更新大摇大摆的走来了。
|
||||
# 简介
|
||||
ddddocr是由sml2h3开发的专为验证码厂商进行对自家新版本验证码难易强度进行验证的一个python库,其由作者与kerlomz共同合作完成,通过大批量生成随机数据后进行深度网络训练,本身并非针对任何一家验证码厂商而制作,本库使用效果完全靠玄学,可能可以识别,可能不能识别。
|
||||
|
||||
ddddocr奉行着开箱即用、最简依赖的理念,尽量减少用户的配置和使用成本,希望给每一位测试者带来舒适的体验
|
||||
|
||||
项目地址: [点我传送](https://github.com/sml2h3/ddddocr)
|
||||
|
||||
# 更新说明
|
||||
|
||||
本次更新其实分为两部分,其中有一部分是在1.2.0版本就已经更新了,但是在这里还是有必要提一下的。
|
||||
|
||||
## 第一部分 OCR识别部分
|
||||
|
||||
在1.2.0开始,ddddocr的识别部分进行了一次beta更新,主要更新在于网络结构主体的升级,其训练数据并没有发生过多的改变,所以理论上在识别结果上,原先可能识别效果的很好的图形在1.2.0上有一小部分概率会有一定程度的下降,也有可能原本识别不好的图形在1.2.0之后效果却变得特别好。
|
||||
测试代码:
|
||||
|
||||
如果为string类型请传入一段不包含空格的文本,其中的每个字符均为一个待选词
|
||||
如:`"0123456789+-x/=""`
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
|
||||
ocr = ddddocr.DdddOcr()
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
image = open("test.jpg", "rb").read()
|
||||
ocr.set_ranges("0123456789+-x/=")
|
||||
result = ocr.classification(image, probability=True)
|
||||
s = ""
|
||||
for i in result['probability']:
|
||||
s += result['charsets'][i.index(max(i))]
|
||||
|
||||
res = ocr.classification(image)
|
||||
print(res)
|
||||
```
|
||||
通过在初始化ddddocr的时候使用beta参数即可快速切换新模型
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
|
||||
ocr = ddddocr.DdddOcr(beta=True)
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
|
||||
res = ocr.classification(image)
|
||||
print(res)
|
||||
```
|
||||
|
||||
OCR部分应该已经有很多人做了测试,在这里就放一部分网友的测试图片。
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
等等更多图片等你测试哟~
|
||||
|
||||
## 第二部分 目标检测部分
|
||||
在本次1.3.0的更新中,目标检测部分隆重登场!
|
||||
目标检测部分同样也是由大量随机合成数据训练而成,对于现在已有的点选验证码图片或者未知的验证码图片都有可能具备一定的识别能力,适用于文字点选和图标点选。
|
||||
简单来说,对于点选类的验证码,可以快速的检测出图片上的文字或者图标。
|
||||
|
||||
|
||||
```python
|
||||
import ddddocr
|
||||
import cv2
|
||||
|
||||
det = ddddocr.DdddOcr(det=True)
|
||||
|
||||
with open("test.jpg", 'rb') as f:
|
||||
image = f.read()
|
||||
|
||||
poses = det.detection(image)
|
||||
print(poses)
|
||||
|
||||
im = cv2.imread("test.jpg")
|
||||
|
||||
for box in poses:
|
||||
x1, y1, x2, y2 = box
|
||||
im = cv2.rectangle(im, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=2)
|
||||
|
||||
cv2.imwrite("result.jpg", im)
|
||||
print(s)
|
||||
|
||||
```
|
||||
|
||||
举些例子:
|
||||
##### Ⅴ. 自定义OCR训练模型导入
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
本项目支持导入来自于 [dddd_trainer](https://github.com/sml2h3/dddd_trainer) 进行自定义训练后的模型,参考导入代码为
|
||||
|
||||
以上只是目前我能找到的点选验证码图片,做了一个简单的测试。
|
||||
```python
|
||||
import ddddocr
|
||||
|
||||
# 安装
|
||||
ocr = ddddocr.DdddOcr(det=False, ocr=False, import_onnx_path="myproject_0.984375_139_13000_2022-02-26-15-34-13.onnx", charsets_path="charsets.json")
|
||||
|
||||
## 环境支持
|
||||
with open('test.jpg', 'rb') as f:
|
||||
image_bytes = f.read()
|
||||
|
||||
`python <= 3.9`
|
||||
res = ocr.classification(image_bytes)
|
||||
print(res)
|
||||
|
||||
`Windows/Linux/Macos..`
|
||||
```
|
||||
|
||||
暂时不支持Macbook M1(X),M1(X)用户需要自己编译onnxruntime才可以使用
|
||||
### 版本控制
|
||||
|
||||
## 安装命令
|
||||
该项目使用Git进行版本管理。您可以在repository参看当前可用版本。
|
||||
|
||||
`pip install ddddocr`
|
||||
### 作者
|
||||
|
||||
以上命令将自动安装符合自己电脑环境的最新ddddocr
|
||||
|
||||
## 拓展 一键部署ddddocr api,支持docker部署
|
||||
|
||||
[github](https://github.com/sml2h3/ocr_api_server)
|
||||
|
||||
[gitee](https://gitee.com/fkgeek/ocr_api_server)
|
||||
|
||||
## 爬虫框架推荐
|
||||
|
||||
[feapder](https://github.com/Boris-code/feapder)
|
||||
|
||||
[crawlab](https://github.com/crawlab-team/crawlab)
|
||||
|
||||
# 交流群 (个人微信太懒了不一定会通过)
|
||||
sml2h3@gamil.com
|
||||
|
||||

|
||||

|
||||
<img src="https://cdn.wenanzhe.com/img/mmqrcode1640418911274.png!/scale/50" alt="wechat" width="150">
|
||||
|
||||
*好友数过多不一定通过,有问题可以在issue进行交流*
|
||||
|
||||
### 版权说明
|
||||
|
||||
该项目签署了MIT 授权许可,详情请参阅 [LICENSE](https://github.com/sml2h3/ddddocr/blob/master/LICENSE)
|
||||
|
||||
|
||||
<!-- links -->
|
||||
[your-project-path]:sml2h3/ddddocr
|
||||
[contributors-shield]: https://img.shields.io/github/contributors/sml2h3/ddddocr?style=flat-square
|
||||
[contributors-url]: https://github.com/shaojintian/Best_README_template/graphs/contributors
|
||||
[forks-shield]: https://img.shields.io/github/forks/sml2h3/ddddocr?style=flat-square
|
||||
[forks-url]: https://github.com/shaojintian/Best_README_template/network/members
|
||||
[stars-shield]: https://img.shields.io/github/stars/sml2h3/ddddocr?style=flat-square
|
||||
[stars-url]: https://github.com/shaojintian/Best_README_template/stargazers
|
||||
[issues-shield]: https://img.shields.io/github/issues/sml2h3/ddddocr?style=flat-square
|
||||
[issues-url]: https://img.shields.io/github/issues/sml2h3/ddddocr.svg
|
||||
[license-shield]: https://img.shields.io/github/license/sml2h3/ddddocr?style=flat-square
|
||||
[license-url]: https://github.com/sml2h3/ddddocr/blob/master/LICENSE
|
||||
|
||||
|
||||
# 主要贡献者
|
||||
|
||||
<span style="margin: 0 5px;" ><a href="https://github.com/kerlomz" ><img src="https://cdn.wenanzhe.com/img/16838877.jpg!/scale/50" /></a></span>
|
||||
|
||||
## Star 历史
|
||||
|
||||
[](https://star-history.com/#sml2h3/ddddocr&Date)
|
||||
|
@ -51,6 +51,7 @@ class DdddOcr(object):
|
||||
self.use_import_onnx = False
|
||||
self.__word = False
|
||||
self.__resize = []
|
||||
self.__charset_range = []
|
||||
self.__channel = 1
|
||||
if import_onnx_path != "":
|
||||
det = False
|
||||
@ -2552,7 +2553,47 @@ class DdddOcr(object):
|
||||
return []
|
||||
return result
|
||||
|
||||
def classification(self, img, png_fix: bool = False):
|
||||
def set_ranges(self, charset_range: int | str):
|
||||
if isinstance(charset_range, int):
|
||||
if charset_range == 0:
|
||||
# 数字
|
||||
self.__charset_range = list("0123456789")
|
||||
elif charset_range == 1:
|
||||
# 小写英文
|
||||
self.__charset_range = list("abcdefghijklmnopqrstuvwxyz")
|
||||
elif charset_range == 2:
|
||||
# 大写英文
|
||||
self.__charset_range = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
elif charset_range == 3:
|
||||
# 混合英文
|
||||
self.__charset_range = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
elif charset_range == 4:
|
||||
# 小写英文+数字
|
||||
self.__charset_range = list("abcdefghijklmnopqrstuvwxyz") + list(
|
||||
"0123456789")
|
||||
elif charset_range == 5:
|
||||
# 大写英文+数字
|
||||
self.__charset_range = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") + list(
|
||||
"0123456789")
|
||||
elif charset_range == 6:
|
||||
# 混合大小写+数字
|
||||
self.__charset_range = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + list(
|
||||
"0123456789")
|
||||
elif charset_range == 7:
|
||||
# 除去英文,数字
|
||||
delete_range = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + list("0123456789")
|
||||
self.__charset_range = [item for item in self.__charset if item not in delete_range]
|
||||
elif isinstance(charset_range, str):
|
||||
charset_range_list = list(charset_range)
|
||||
self.__charset_range = charset_range_list
|
||||
else:
|
||||
raise TypeError("暂时不支持该类型数据的输入")
|
||||
|
||||
# 去重
|
||||
self.__charset_range = list(set(self.__charset_range)) + [""]
|
||||
|
||||
|
||||
def classification(self, img, png_fix: bool = False, probability=False):
|
||||
if self.det:
|
||||
raise TypeError("当前识别类型为目标检测")
|
||||
if not isinstance(img, (bytes, str, pathlib.PurePath, Image.Image)):
|
||||
@ -2601,19 +2642,62 @@ class DdddOcr(object):
|
||||
result = []
|
||||
|
||||
last_item = 0
|
||||
|
||||
if self.__word:
|
||||
for item in ort_outs[1]:
|
||||
result.append(self.__charset[item])
|
||||
else:
|
||||
for item in ort_outs[0][0]:
|
||||
if item == last_item:
|
||||
continue
|
||||
if not self.use_import_onnx:
|
||||
# 概率输出仅限于使用官方模型
|
||||
if probability:
|
||||
ort_outs = ort_outs[0]
|
||||
ort_outs = np.exp(ort_outs) / np.sum(np.exp(ort_outs))
|
||||
ort_outs_sum = np.sum(ort_outs, axis=2)
|
||||
ort_outs_probability = np.empty_like(ort_outs)
|
||||
for i in range(ort_outs.shape[0]):
|
||||
ort_outs_probability[i] = ort_outs[i] / ort_outs_sum[i]
|
||||
ort_outs_probability = np.squeeze(ort_outs_probability).tolist()
|
||||
result = {}
|
||||
if len(self.__charset_range) == 0:
|
||||
# 返回全部
|
||||
result['charsets'] = self.__charset
|
||||
result['probability'] = ort_outs_probability
|
||||
else:
|
||||
result['charsets'] = self.__charset_range
|
||||
probability_result_index = []
|
||||
for item in self.__charset_range:
|
||||
if item in self.__charset:
|
||||
probability_result_index.append(self.__charset.index(item))
|
||||
else:
|
||||
# 未知字符
|
||||
probability_result_index.append(-1)
|
||||
probability_result = []
|
||||
for item in ort_outs_probability:
|
||||
probability_result.append([item[i] if i != -1 else -1 for i in probability_result_index ])
|
||||
result['probability'] = probability_result
|
||||
return result
|
||||
else:
|
||||
last_item = item
|
||||
if item != 0:
|
||||
result.append(self.__charset[item])
|
||||
last_item = 0
|
||||
argmax_result = np.squeeze(np.argmax(ort_outs[0], axis=2))
|
||||
for item in argmax_result:
|
||||
if item == last_item:
|
||||
continue
|
||||
else:
|
||||
last_item = item
|
||||
if item != 0:
|
||||
result.append(self.__charset[item])
|
||||
return ''.join(result)
|
||||
|
||||
return ''.join(result)
|
||||
else:
|
||||
last_item = 0
|
||||
for item in ort_outs[0][0]:
|
||||
if item == last_item:
|
||||
continue
|
||||
else:
|
||||
last_item = item
|
||||
if item != 0:
|
||||
result.append(self.__charset[item])
|
||||
return ''.join(result)
|
||||
|
||||
def detection(self, img_bytes: bytes = None, img_base64: str = None):
|
||||
if not self.det:
|
||||
|
Binary file not shown.
Binary file not shown.
BIN
ddddocr/logo.png
Normal file
BIN
ddddocr/logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
5
setup.py
5
setup.py
@ -14,7 +14,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
||||
|
||||
setup(
|
||||
name="ddddocr",
|
||||
version="1.4.11",
|
||||
version="1.5.1",
|
||||
author="sml2h3",
|
||||
description="带带弟弟OCR",
|
||||
long_description=long_description,
|
||||
@ -28,11 +28,12 @@ setup(
|
||||
"Programming Language :: Python :: 3.9",
|
||||
"Programming Language :: Python :: 3.10",
|
||||
"Programming Language :: Python :: 3.11",
|
||||
"Programming Language :: Python :: 3.12",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
],
|
||||
install_requires=['numpy', 'onnxruntime', 'Pillow', 'opencv-python-headless'],
|
||||
python_requires='<3.12',
|
||||
python_requires='<3.13',
|
||||
include_package_data=True,
|
||||
install_package_data=True,
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user