Skip to content

Commit f5fcdcf

Browse files
committed
chore: update docs
1 parent 6e61acd commit f5fcdcf

File tree

6 files changed

+102
-106
lines changed

6 files changed

+102
-106
lines changed

docs/blog/posts/about_model/adapt_PP-OCRv5_mobile_det.md

Lines changed: 99 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: RapidOCR集成PP-OCRv5_mobile_det模型记录
2+
title: RapidOCR集成PP-OCRv5_det模型(mobile/server)记录
33
date: 2025-05-26
44
authors: [SWHL]
55
categories:
@@ -10,7 +10,7 @@ hide:
1010
---
1111

1212

13-
> 该文章主要记录RapidOCR集成PP-OCRv5_mobile_det模型记录的,涉及模型转换,模型精度测试等步骤。
13+
> 该文章主要记录RapidOCR集成PP-OCRv5_mobile_det和PP-OCRv5_server_det模型记录的,涉及模型转换,模型精度测试等步骤。
1414
1515
<!-- more -->
1616

@@ -34,7 +34,7 @@ hide:
3434
安装`paddlex`:
3535

3636
```bash linenums="1"
37-
pip install "paddlex[ocr]==3.0.0rc1"
37+
pip install "paddlex[ocr]==3.0.0"
3838
```
3939

4040
测试PP-OCRv5_mobile_det模型能否正常识别:
@@ -43,158 +43,152 @@ pip install "paddlex[ocr]==3.0.0rc1"
4343

4444
运行以下代码时,模型会自动下载到 **/Users/用户名/.paddlex/official_models** 下。
4545

46+
测试图:[link](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png)
47+
4648
```python linenums="1"
4749

48-
from paddleocr import PaddleOCR
49-
# 初始化 PaddleOCR 实例
50+
from paddlex import create_model
5051

51-
ocr = PaddleOCR(
52-
use_doc_orientation_classify=False,
53-
use_doc_unwarping=False,
54-
use_textline_orientation=False)
52+
# mobile
53+
model = create_model(model_name="PP-OCRv5_mobile_det")
5554

56-
# 对示例图像执行 OCR 推理
57-
result = ocr.predict(
58-
input="https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_002.png")
55+
# server
56+
model = create_model(model_name="PP-OCRv5_server_det")
5957

60-
# 可视化结果并保存 json 结果
61-
for res in result:
58+
output = model.predict(input="images/general_ocr_001.png", batch_size=1)
59+
for res in output:
6260
res.print()
63-
res.save_to_img("output")
64-
res.save_to_json("output")
61+
res.save_to_img(save_path="./output/")
62+
res.save_to_json(save_path="./output/res.json")
6563
```
6664

6765
预期结果如下,表明成功运行:
6866

69-
![alt text](../images/general_ocr_002_ocr_res_img.png)
67+
![alt text](../images/general_ocr_001_res.png)
7068

7169
### 2. 模型转换
7270

7371
该部分主要参考文档: [docs](https://paddlepaddle.github.io/PaddleX/latest/pipeline_deploy/paddle2onnx.html?h=paddle2onnx#22)
7472

75-
PaddleX官方集成了paddle2onnx的转换代码:
76-
77-
```bash linenums="1"
78-
paddle2onnx --model_dir models/official_models/PP-OCRv5_mobile_det --model_filename inference.json --params_filename inference.pdiparams --save_file models/PP-OCRv5_mobile_det/inference.onnx
79-
```
73+
=== "转换PP-OCRv5_mobile_det"
8074

81-
输出日志如下,日志中存在报错信息,但是最终ONNX模型仍然生成了:
82-
83-
```bash linenums="1" hl_lines="11 16"
84-
/Users/xxxx/miniconda3/envs/py310/lib/python3.10/site-packages/paddle/utils/cpp_extension/extension_utils.py:711: UserWarning: No ccache found. Please be aware that recompiling all source files may be required. You can download and install ccache from: https://github.com/ccache/ccache/blob/master/doc/INSTALL.md
85-
warnings.warn(warning_message)
86-
[Paddle2ONNX] Start parsing the Paddle model file...
87-
[Paddle2ONNX] Use opset_version = 14 for ONNX export.
88-
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
89-
2025-05-26 11:20:46 [INFO] Try to perform constant folding on the ONNX model with Polygraphy.
90-
[W] 'colored' module is not installed, will not use colors when logging. To enable colors, please install the 'colored' module: python3 -m pip install colored
91-
[I] Folding Constants | Pass 1
92-
[W] colored module is not installed, will not use colors when logging. To enable colors, please install the colored module: python3 -m pip install colored
93-
[W] Inference failed. You may want to try enabling partitioning to see better results. Note: Error was:
94-
[ONNXRuntimeError] : 1 : FAIL : /Users/runner/work/1/s/onnxruntime/core/graph/model.cc:182 onnxruntime::Model::Model(ModelProto &&, const PathString &, const IOnnxRuntimeOpSchemaRegistryList *, const logging::Logger &, const ModelOptions &) Unsupported model IR version: 11, max supported IR version: 10
95-
[I] Total Nodes | Original: 925, After Folding: 612 | 313 Nodes Folded
96-
[I] Folding Constants | Pass 2
97-
[W] colored module is not installed, will not use colors when logging. To enable colors, please install the colored module: python3 -m pip install colored
98-
[W] Inference failed. You may want to try enabling partitioning to see better results. Note: Error was:
99-
[ONNXRuntimeError] : 1 : FAIL : /Users/runner/work/1/s/onnxruntime/core/graph/model.cc:182 onnxruntime::Model::Model(ModelProto &&, const PathString &, const IOnnxRuntimeOpSchemaRegistryList *, const logging::Logger &, const ModelOptions &) Unsupported model IR version: 11, max supported IR version: 10
100-
[I] Total Nodes | Original: 612, After Folding: 612 | 0 Nodes Folded
101-
2025-05-26 11:20:52 [INFO] ONNX model saved in models/PP-OCRv5_mobile_det/inference.onnx.
102-
```
75+
PaddleX官方集成了paddle2onnx的转换代码:
10376

104-
此时得到的模型,直接用`rapidocr`推理会报错:
77+
```bash linenums="1"
78+
paddlex --install paddle2onnx
79+
pip install onnx==1.16.0
10580

106-
```python linenums="1"
107-
from rapidocr import RapidOCR
81+
paddlex --paddle2onnx --paddle_model_dir models/official_models/PP-OCRv5_mobile_det --onnx_model_dir models/PP-OCRv5_mobile_det
82+
```
10883

109-
model_path = "models/PP-OCRv5_mobile_det/inference.onnx"
110-
engine = RapidOCR(params={"Det.model_path": model_path})
84+
输出日志如下,表明转换成功:
85+
86+
```bash linenums="1"
87+
Input dir: models/official_models/PP-OCRv5_mobile_det
88+
Output dir: models/PP-OCRv5_mobile_det
89+
Paddle2ONNX conversion starting...
90+
[Paddle2ONNX] Start parsing the Paddle model file...
91+
[Paddle2ONNX] Use opset_version = 14 for ONNX export.
92+
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
93+
2025-05-26 21:53:00 [INFO] Try to perform constant folding on the ONNX model with Polygraphy.
94+
[W] 'colored' module is not installed, will not use colors when logging. To enable colors, please install the 'colored' module: python3 -m pip install colored
95+
[I] Folding Constants | Pass 1
96+
[I] Total Nodes | Original: 925, After Folding: 502 | 423 Nodes Folded
97+
[I] Folding Constants | Pass 2
98+
[I] Total Nodes | Original: 502, After Folding: 502 | 0 Nodes Folded
99+
2025-05-26 21:53:08 [INFO] ONNX model saved in models/PP-OCRv5_mobile_det/inference.onnx.
100+
Paddle2ONNX conversion succeeded
101+
Copied models/official_models/PP-OCRv5_mobile_det/inference.yml to models/PP-OCRv5_mobile_det/inference.yml
102+
Done
103+
```
111104

112-
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
113-
result = engine(img_url)
114-
print(result)
105+
=== "转换PP-OCRv5_server_det"
115106

116-
result.vis("vis_result.jpg")
117-
```
107+
PaddleX官方集成了paddle2onnx的转换代码:
118108

119-
报错信息如下:
109+
```bash linenums="1"
110+
paddlex --install paddle2onnx
111+
pip install onnx==1.16.0
120112

121-
```bash linenums="1" hl_lines="15"
122-
[INFO] 2025-05-26 11:21:27,698 [RapidOCR] base.py:41: Using engine_name: onnxruntime
123-
Traceback (most recent call last):
124-
File "/Users/xxxx/projects/RapidOCR/python/demo.py", line 9, in <module>
125-
engine = RapidOCR(params={"Det.model_path": model_path})
126-
File "/Users/xxxx/projects/RapidOCR/python/rapidocr/main.py", line 60, in __init__
127-
self.text_det = TextDetector(config.Det)
128-
File "/Users/xxxx/projects/RapidOCR/python/rapidocr/ch_ppocr_det/main.py", line 45, in __init__
129-
self.session = get_engine(config.engine_name)(config)
130-
File "/Users/xxxx/projects/RapidOCR/python/rapidocr/inference_engine/onnxruntime.py", line 60, in __init__
131-
self.session = InferenceSession(
132-
File "/Users/xxxx/miniconda3/envs/py310/lib/python3.10/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 472, in __init__
133-
self._create_inference_session(providers, provider_options, disabled_optimizers)
134-
File "/Users/xxxx/miniconda3/envs/py310/lib/python3.10/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 550, in _create_inference_session
135-
sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
136-
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from /Users/xxxx/projects/LittleCode/models/PP-OCRv5_mobile_det/inference.onnx failed:/Users/runner/work/1/s/onnxruntime/core/graph/model.cc:182 onnxruntime::Model::Model(ModelProto &&, const PathString &, const IOnnxRuntimeOpSchemaRegistryList *, const logging::Logger &, const ModelOptions &) Unsupported model IR version: 11, max supported IR version: 10
137-
```
113+
paddlex --paddle2onnx --paddle_model_dir models/official_models/PP-OCRv5_server_det --onnx_model_dir models/PP-OCRv5_server_det
114+
```
138115

139-
经过一系列的查阅资料,发现了两种解决方案,经过测试,精度一样。
116+
输出日志如下,表明转换成功:
117+
118+
```bash linenums="1"
119+
Input dir: models/official_models/PP-OCRv5_server_det
120+
Output dir: models/PP-OCRv5_server_det
121+
Paddle2ONNX conversion starting...
122+
[Paddle2ONNX] Start parsing the Paddle model file...
123+
[Paddle2ONNX] Use opset_version = 14 for ONNX export.
124+
[Paddle2ONNX] PaddlePaddle model is exported as ONNX format now.
125+
2025-05-26 21:43:10 [INFO] Try to perform constant folding on the ONNX model with Polygraphy.
126+
[W] 'colored' module is not installed, will not use colors when logging. To enable colors, please install the 'colored' module: python3 -m pip install colored
127+
[I] Folding Constants | Pass 1
128+
[I] Total Nodes | Original: 1306, After Folding: 596 | 710 Nodes Folded
129+
[I] Folding Constants | Pass 2
130+
[I] Total Nodes | Original: 596, After Folding: 596 | 0 Nodes Folded
131+
2025-05-26 21:43:21 [INFO] ONNX model saved in models/PP-OCRv5_server_det/inference.onnx.
132+
Paddle2ONNX conversion succeeded
133+
Copied models/official_models/PP-OCRv5_server_det/inference.yml to models/PP-OCRv5_server_det/inference.yml
134+
Done
135+
```
140136

141-
=== 方案一
137+
### 3. 模型推理验证
142138

143-
安装`onnx==1.16.0`,然后再次尝试转换。我这里实际测过,的确可以成功转换。
139+
=== "验证PP-OCRv5_mobile_det模型"
144140

145-
=== 方案二
141+
该部分主要是在RapidOCR项目中测试能否直接使用onnx模型。要点主要是确定模型前后处理是否兼容。从PaddleOCR config文件中比较[PP-OCRv4](https://github.com/PaddlePaddle/PaddleOCR/blob/549d83a88b7c75144120e6ec03de80d3eb9e48a5/configs/det/PP-OCRv4/PP-OCRv4_mobile_det.yml)和[PP-OCRv5 mobile det](https://github.com/PaddlePaddle/PaddleOCR/blob/549d83a88b7c75144120e6ec03de80d3eb9e48a5/configs/det/PP-OCRv5/PP-OCRv5_mobile_det.yml)文件差异:
146142

147-
方案来自:onnxruntime issue [#23602](https://github.com/microsoft/onnxruntime/issues/23602#issuecomment-2642348849)
143+
![alt text](../images/v4_v5_mobile_det.png)
148144

149-
运行下面代码,将上一步所得模型重新指定一下**IR_VERSION**,就可以用`rapidocr`加载推理了
145+
从上图中可以看出,配置基本一模一样,因此现有`rapidocr`前后推理代码可以直接使用
150146

151147
```python linenums="1"
152-
import onnx
153-
from onnx import version_converter
148+
from rapidocr import RapidOCR
154149

155-
OPT_VERSION = 14
156-
IR_VERSION = 10
150+
model_path = "models/PP-OCRv5_mobile_det/inference.onnx"
151+
engine = RapidOCR(params={"Det.model_path": model_path})
157152

158-
source_path = "models/PP-OCRv5_mobile_det/inference.onnx"
159-
dist_path = "models/PP-OCRv5_mobile_det/inference_v2.onnx"
153+
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
154+
result = engine(img_url)
155+
print(result)
160156

161-
model = onnx.load(source_path)
162-
model.ir_version = IR_VERSION
163-
model = version_converter.convert_version(model, OPT_VERSION)
164-
onnx.save(model, dist_path)
157+
result.vis("vis_result.jpg")
165158
```
166159

167-
### 3. 模型推理验证
160+
![alt text](../images/v5_mobile_det_vis_result.jpg)
168161

169-
该部分主要是在RapidOCR项目中测试能否直接使用onnx模型。要点主要是确定模型前后处理是否兼容。从PaddleOCR config文件中比较[PP-OCRv4](https://github.com/PaddlePaddle/PaddleOCR/blob/549d83a88b7c75144120e6ec03de80d3eb9e48a5/configs/det/PP-OCRv4/PP-OCRv4_mobile_det.yml)和[PP-OCRv5 mobile det](https://github.com/PaddlePaddle/PaddleOCR/blob/549d83a88b7c75144120e6ec03de80d3eb9e48a5/configs/det/PP-OCRv5/PP-OCRv5_mobile_det.yml)文件差异:
162+
==== "验证PP-OCRv5_server_det模型"
170163

171-
![alt text](../images/v4_v5_mobile_det.png)
164+
该部分主要是在RapidOCR项目中测试能否直接使用onnx模型。要点主要是确定模型前后处理是否兼容。从PaddleOCR config文件中比较[PP-OCRv4_server_det](https://github.com/PaddlePaddle/PaddleOCR/blob/b0b31c38aef135617a98fbf89c92efd8b2eebd73/configs/det/PP-OCRv4/PP-OCRv4_server_det.yml)和[PP-OCRv5_server_det](https://github.com/PaddlePaddle/PaddleOCR/blob/b0b31c38aef135617a98fbf89c92efd8b2eebd73/configs/det/PP-OCRv5/PP-OCRv5_server_det.yml)文件差异:
172165

173-
从上图中可以看出,配置基本一模一样,因为现有`rapidocr`前后推理代码可以直接使用。
166+
![alt text](../images/v4_v5_server_det.png)
174167

175-
```python linenums="1"
176-
from rapidocr import RapidOCR
168+
从上图中可以看出,配置基本一模一样,backbone换了,但是前后处理配置是一样的。因此现有`rapidocr`前后推理代码可以直接使用。
177169

178-
model_path = "models/PP-OCRv5_mobile_det/inference.onnx"
179-
engine = RapidOCR(params={"Det.model_path": model_path})
170+
```python linenums="1"
171+
from rapidocr import RapidOCR
180172

181-
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
182-
result = engine(img_url)
183-
print(result)
173+
model_path = "models/PP-OCRv5_server_det/inference.onnx"
174+
engine = RapidOCR(params={"Det.model_path": model_path})
184175

185-
result.vis("vis_result.jpg")
186-
```
176+
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
177+
result = engine(img_url)
178+
print(result)
187179

188-
![alt text](../images/v5_mobile_det_vis_result.jpg)
180+
result.vis("vis_result.jpg")
181+
```
182+
183+
![alt text](../images/v5_server_det_vis_result.jpg)
189184

190185
### 4. 模型精度测试
191186

192187
!!! warning
193188

194189
测试集[text_det_test_dataset](https://huggingface.co/datasets/SWHL/text_det_test_dataset)包括卡证类、文档类和自然场景三大类。其中卡证类有82张,文档类有75张,自然场景类有55张。缺少手写体、繁体、日文、古籍文本、拼音、艺术字等数据。因此,该基于该测评集的结果仅供参考。
195190

196-
欢迎有兴趣的小伙伴,可以和我们一起共建更加完整的测评集。
197-
191+
欢迎有兴趣的小伙伴,可以和我们一起共建更加全面的测评集。
198192

199193
该部分主要使用[TextDetMetric](https://github.com/SWHL/TextDetMetric)和测试集[text_det_test_dataset](https://huggingface.co/datasets/SWHL/text_det_test_dataset)来评测。
200194

@@ -208,7 +202,7 @@ result.vis("vis_result.jpg")
208202

209203
### 5. 集成到rapidocr中
210204

211-
该部分主要包括将字典文件写入到ONNX模型中、托管模型到魔搭、更改rapidocr代码适配等。
205+
该部分主要包括将托管模型到魔搭、更改rapidocr代码适配等。
212206

213207
#### 托管模型到魔搭
214208

docs/blog/posts/about_model/model_summary.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
title: 开源OCR模型对比
33
date:
44
created: 2022-04-16
5-
updated: 2025-05-17
5+
updated: 2025-05-26
66
authors: [SWHL]
77
categories:
88
- 模型相关
99
comments: true
10+
hide:
11+
- toc
1012
---
1113

1214
> 本文主要给出了常见开源文本检测和文本识别模型的对比和评测,给大家一个使用参考。
1.56 MB
Loading
-556 KB
Binary file not shown.
182 KB
Loading
50.3 KB
Loading

0 commit comments

Comments
 (0)