5
0
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:
sml2h3 2024-05-06 11:43:42 +08:00
parent 0fe1283187
commit f0ee6b122e
8 changed files with 667 additions and 356 deletions

459
README.md
View File

@ -1,63 +1,237 @@
![header.png](https://cdn.wenanzhe.com/img/68747470733a2f2f7a332e617831782e636f6d2f323032312f30372f30322f5236496832382e6a7067.jfif)
# 带带弟弟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的后续版本维护本项目长期维护
### 项目底层支持
![Test](https://cdn.wenanzhe.com/img/zhifubao.jpg!/scale/30)
![Test](https://cdn.wenanzhe.com/img/weixin.jpg!/scale/30)
本项目基于[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练所得训练底层框架位pytorchddddocr推理底层抵赖于[onnxruntime](https://pypi.org/project/onnxruntime/)故本项目的最大兼容性与python版本支持主要取决于[onnxruntime](https://pypi.org/project/onnxruntime/)。
# 赞助合作商
### 使用文档
| | 赞助合作商 | 推荐理由 |
|------------------------------------------------------------|------------|--------------------------------------------------------------------------------------------------|
| ![YesCaptcha](https://cdn.wenanzhe.com/img/yescaptcha.png) | [YesCaptcha](https://yescaptcha.com/i/NSwk7i) | 谷歌reCaptcha验证码 / hCaptcha验证码 / funCaptcha验证码商业级识别接口 [点我](https://yescaptcha.com/i/NSwk7i) 直达VIP4 |
| ![Malenia](https://cdn.wenanzhe.com/img/malenia.png!/scale/50) | [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()
![Test](https://cdn.wenanzhe.com/img/b.png)
image = open("example.jpg", "rb").read()
result = ocr.classification(image)
print(result)
```
然后背景为带小滑块坑位的,如下图
![Test](https://cdn.wenanzhe.com/img/a.png)
本库内置有两套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位置 **在⽬标检测⾥我们通常使⽤bboxbounding 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
一张图为带坑位的原图,如下图
![Test](https://cdn.wenanzhe.com/img/bg.jpg)
**a.算法2**
一张图为原图,如下图
![Test](https://cdn.wenanzhe.com/img/fullpage.jpg)
算法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 |
![Test](https://cdn.wenanzhe.com/img/0446fe794381489f90719d5e0506f2da.jpg)
![Test](https://cdn.wenanzhe.com/img/6175e944c1dc408a89aabe4f7fc07fca.jpg)
![Test](https://cdn.wenanzhe.com/img/20211226135747.png)
![Test](https://cdn.wenanzhe.com/img/f34390d4911c45ce9058dc2e7e9d847a.jpg)
那么今天他来了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部分应该已经有很多人做了测试在这里就放一部分网友的测试图片。
![Test](https://cdn.wenanzhe.com/img/20210715211733855.png)
![Test](https://cdn.wenanzhe.com/img/78b7f57d-371d-4b65-afb2-d19608ae1892.png)
![Test](https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142305.png)
![Test](https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142325.png)
![Test](https://cdn.wenanzhe.com/img/2AMLyA_fd83e1f1800e829033417ae6dd0e0ae0.png)
![Test](https://cdn.wenanzhe.com/img/aabd_181ae81dd5526b8b89f987d1179266ce.jpg)
![Test](https://cdn.wenanzhe.com/img/2bghz_b504e9f9de1ed7070102d21c6481e0cf.png)
![Test](https://cdn.wenanzhe.com/img/0000_z4ecc2p65rxc610x.jpg)
![Test](https://cdn.wenanzhe.com/img/2acd_0586b6b36858a4e8a9939db8a7ec07b7.jpg)
![Test](https://cdn.wenanzhe.com/img/2a8r_79074e311d573d31e1630978fe04b990.jpg)
![Test](https://cdn.wenanzhe.com/img/aftf_C2vHZlk8540y3qAmCM.bmp)
![Test](https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20211226144057.png)
等等更多图片等你测试哟~
## 第二部分 目标检测部分
在本次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训练模型导入
![Test](https://cdn.wenanzhe.com/img/page1_1.jpg)
![Test](https://cdn.wenanzhe.com/img/page1_2.jpg)
![Test](https://cdn.wenanzhe.com/img/page1_3.jpg)
![Test](https://cdn.wenanzhe.com/img/page1_4.jpg)
![Test](https://cdn.wenanzhe.com/img/result.jpg)
![Test](https://cdn.wenanzhe.com/img/result2.jpg)
![Test](https://cdn.wenanzhe.com/img/result4.jpg)
本项目支持导入来自于 [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
![六群链接](https://cdn.wenanzhe.com/img/group.jpg!/scale/50)
![Test](https://cdn.wenanzhe.com/img/mmqrcode1640418911274.png!/scale/50)
<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 历史
[![Star History Chart](https://api.star-history.com/svg?repos=sml2h3/ddddocr&type=Date)](https://star-history.com/#sml2h3/ddddocr&Date)

View File

@ -1,63 +1,237 @@
![header.png](https://cdn.wenanzhe.com/img/68747470733a2f2f7a332e617831782e636f6d2f323032312f30372f30322f5236496832382e6a7067.jfif)
# 带带弟弟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的后续版本维护本项目长期维护
### 项目底层支持
![Test](https://cdn.wenanzhe.com/img/zhifubao.jpg!/scale/30)
![Test](https://cdn.wenanzhe.com/img/weixin.jpg!/scale/30)
本项目基于[dddd_trainer](https://github.com/sml2h3/dddd_trainer) 训练所得训练底层框架位pytorchddddocr推理底层抵赖于[onnxruntime](https://pypi.org/project/onnxruntime/)故本项目的最大兼容性与python版本支持主要取决于[onnxruntime](https://pypi.org/project/onnxruntime/)。
# 赞助合作商
### 使用文档
| | 赞助合作商 | 推荐理由 |
|------------------------------------------------------------|------------|--------------------------------------------------------------------------------------------------|
| ![YesCaptcha](https://cdn.wenanzhe.com/img/yescaptcha.png) | [YesCaptcha](https://yescaptcha.com/i/NSwk7i) | 谷歌reCaptcha验证码 / hCaptcha验证码 / funCaptcha验证码商业级识别接口 [点我](https://yescaptcha.com/i/NSwk7i) 直达VIP4 |
| ![Malenia](https://cdn.wenanzhe.com/img/malenia.png!/scale/50) | [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()
![Test](https://cdn.wenanzhe.com/img/b.png)
image = open("example.jpg", "rb").read()
result = ocr.classification(image)
print(result)
```
然后背景为带小滑块坑位的,如下图
![Test](https://cdn.wenanzhe.com/img/a.png)
本库内置有两套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位置 **在⽬标检测⾥我们通常使⽤bboxbounding 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
一张图为带坑位的原图,如下图
![Test](https://cdn.wenanzhe.com/img/bg.jpg)
**a.算法2**
一张图为原图,如下图
![Test](https://cdn.wenanzhe.com/img/fullpage.jpg)
算法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 |
![Test](https://cdn.wenanzhe.com/img/0446fe794381489f90719d5e0506f2da.jpg)
![Test](https://cdn.wenanzhe.com/img/6175e944c1dc408a89aabe4f7fc07fca.jpg)
![Test](https://cdn.wenanzhe.com/img/20211226135747.png)
![Test](https://cdn.wenanzhe.com/img/f34390d4911c45ce9058dc2e7e9d847a.jpg)
那么今天他来了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部分应该已经有很多人做了测试在这里就放一部分网友的测试图片。
![Test](https://cdn.wenanzhe.com/img/20210715211733855.png)
![Test](https://cdn.wenanzhe.com/img/78b7f57d-371d-4b65-afb2-d19608ae1892.png)
![Test](https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142305.png)
![Test](https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211226142325.png)
![Test](https://cdn.wenanzhe.com/img/2AMLyA_fd83e1f1800e829033417ae6dd0e0ae0.png)
![Test](https://cdn.wenanzhe.com/img/aabd_181ae81dd5526b8b89f987d1179266ce.jpg)
![Test](https://cdn.wenanzhe.com/img/2bghz_b504e9f9de1ed7070102d21c6481e0cf.png)
![Test](https://cdn.wenanzhe.com/img/0000_z4ecc2p65rxc610x.jpg)
![Test](https://cdn.wenanzhe.com/img/2acd_0586b6b36858a4e8a9939db8a7ec07b7.jpg)
![Test](https://cdn.wenanzhe.com/img/2a8r_79074e311d573d31e1630978fe04b990.jpg)
![Test](https://cdn.wenanzhe.com/img/aftf_C2vHZlk8540y3qAmCM.bmp)
![Test](https://cdn.wenanzhe.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20211226144057.png)
等等更多图片等你测试哟~
## 第二部分 目标检测部分
在本次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训练模型导入
![Test](https://cdn.wenanzhe.com/img/page1_1.jpg)
![Test](https://cdn.wenanzhe.com/img/page1_2.jpg)
![Test](https://cdn.wenanzhe.com/img/page1_3.jpg)
![Test](https://cdn.wenanzhe.com/img/page1_4.jpg)
![Test](https://cdn.wenanzhe.com/img/result.jpg)
![Test](https://cdn.wenanzhe.com/img/result2.jpg)
![Test](https://cdn.wenanzhe.com/img/result4.jpg)
本项目支持导入来自于 [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
![六群链接](https://cdn.wenanzhe.com/img/group.jpg!/scale/50)
![Test](https://cdn.wenanzhe.com/img/mmqrcode1640418911274.png!/scale/50)
<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 历史
[![Star History Chart](https://api.star-history.com/svg?repos=sml2h3/ddddocr&type=Date)](https://star-history.com/#sml2h3/ddddocr&Date)

View File

@ -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

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@ -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,
)