99 - toc
1010links :
1111 - 开源OCR模型对比 : blog/posts/about_model/model_summary.md
12- - RapidOCR集成PP-OCRv4_server_rec_doc模型记录 : git blog/posts/about_model/adapt_PP-OCRv4_server_rec_doc.md
12+ - RapidOCR集成PP-OCRv4_server_rec_doc模型记录 : blog/posts/about_model/adapt_PP-OCRv4_server_rec_doc.md
1313---
1414
1515
@@ -195,21 +195,109 @@ for res in output:
195195
196196该部分主要使用[ TextDetMetric] ( https://github.com/SWHL/TextDetMetric ) 和测试集[ text_det_test_dataset] ( https://huggingface.co/datasets/SWHL/text_det_test_dataset ) 来评测。
197197
198- 相关测试步骤请参见[ TextDetMetric] ( https://github.com/SWHL/TextRecMetric ) 的README,一步一步来就行。我这里测试最终精度如下 :
198+ 相关测试步骤请参见[ TextDetMetric] ( https://github.com/SWHL/TextRecMetric ) 的README,一步一步来就行。我这里简单给出关键代码 :
199199
200- PP-OCRv5_mobile_det
200+ 其中,计算 ** pred.txt ** 代码如下:
201201
202- ``` json
203- {'precision': 0.7861, 'recall': 0.8266, 'hmean': 0.8058, 'avg_elapse': 0.1499 }
204- ```
202+ === "基于ONNXRuntime获得 ** pred.txt** 文本文件"
203+
204+ ```python linenums="1"
205+ import cv2
206+ import numpy as np
207+ from datasets import load_dataset
208+ from rapidocr import RapidOCR
209+ from tqdm import tqdm
210+
211+ model_path = "models/PP-OCRv5_mobile_det/inference.onnx"
212+ engine = RapidOCR(params={"Det.model_path": model_path})
213+
214+ dataset = load_dataset("SWHL/text_det_test_dataset")
215+ test_data = dataset["test"]
216+
217+ content = []
218+ for i, one_data in enumerate(tqdm(test_data)):
219+ img = np.array(one_data.get("image"))
220+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
221+
222+ ocr_results = engine(img, use_det=True, use_cls=False, use_rec=False)
223+ dt_boxes = ocr_results.boxes
224+
225+ dt_boxes = [] if dt_boxes is None else dt_boxes.tolist()
226+ elapse = ocr_results.elapse
227+
228+ gt_boxes = [v["points"] for v in one_data["shapes"]]
229+ content.append(f"{dt_boxes}\t{gt_boxes}\t{elapse}")
230+
231+ with open("pred.txt", "w", encoding="utf-8") as f:
232+ for v in content:
233+ f.write(f"{v}\n")
234+ ```
235+
236+ === "基于PaddleX获得 ** pred.txt** 文本文件"
237+
238+ ```python linenums="1"
239+ import time
240+ import cv2
241+ import numpy as np
242+ from datasets import load_dataset
243+ from tqdm import tqdm
244+
245+ from paddlex import create_model
246+
247+ model = create_model(model_name="PP-OCRv5_mobile_det")
248+
249+ dataset = load_dataset("SWHL/text_det_test_dataset")
250+ test_data = dataset["test"]
251+
252+ content = []
253+ for i, one_data in enumerate(tqdm(test_data)):
254+ img = np.array(one_data.get("image"))
255+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
256+
257+ t0 = time.perf_counter()
258+ ocr_results = next(model.predict(input=img, batch_size=1))
259+ dt_boxes = ocr_results["dt_polys"].tolist()
260+
261+ elapse = time.perf_counter() - t0
262+
263+ gt_boxes = [v["points"] for v in one_data["shapes"]]
264+ content.append(f"{dt_boxes}\t{gt_boxes}\t{elapse}")
205265
206- PP-OCRv5_server_det
266+ with open("pred.txt", "w", encoding="utf-8") as f:
267+ for v in content:
268+ f.write(f"{v}\n")
269+ ```
270+
271+ 计算指标代码:
272+
273+ ``` python linenums="1"
274+ from text_det_metric import TextDetMetric
207275
208- ``` json
209- {'precision': 0.7394, 'recall': 0.8442, 'hmean': 0.7883, 'avg_elapse': 2.1106 }
276+ metric = TextDetMetric()
277+ pred_path = " pred.txt"
278+ metric = metric(pred_path)
279+ print (metric)
210280```
211281
212- 该结果已经更新到[ 开源OCR模型对比] ( ./model_summary.md ) 中。
282+ 指标汇总如下:
283+
284+ | 模型| 推理引擎| Precision↑| Recall↑| H-mean↑| Elapse↓|
285+ | :---:| :---:| :---:| :---:| :---:| :---:|
286+ | PP-OCRv5_mobile_det| ONNXRuntime| 0.7861| 0.8266| 0.8058| 0.1499|
287+ | PP-OCRv5_mobile_det| PaddlePaddle| 0.7864| 0.8018| 0.794| 0.1954|
288+ | PP-OCRv4_mobile_det| ONNXRuntime| 0.8301| 0.8659| 0.8476| -|
289+ |||||||
290+ | PP-OCRv5_server_det| ONNXRuntime| 0.7394| 0.8442| 0.7883| 2.1106|
291+ | PP-OCRv5_server_det| PaddlePaddle| 0.8347| 0.8583| 0.8463| 2.1450|
292+ | PP-OCRv4_server_det| ONNXRuntime| 0.7922| 0.8128| 0.7691| -|
293+
294+ 从以上结果来看,可以得到以下结论:
295+
296+ 1 . mobile模型转换为ONNX格式后,指标有小幅提升,推理速度也有提升。
297+ 2 . mobile整体指标弱于PP-OCRv4的,应该是测评集覆盖不全导致的。
298+ 3 . v5 server模型转换为ONNX格式后,H-mean下降了5.8%。转换方式和mobile的相同,具体原因需要进一步排查。
299+
300+ 上述表格中基于ONNXRuntimde的结果已经更新到[ 开源OCR模型对比] ( ./model_summary.md ) 中。
213301
214302### 5. 集成到rapidocr中
215303
@@ -219,7 +307,7 @@ PP-OCRv5_server_det
219307
220308该部分主要是涉及模型上传到对应位置,并合理命名。注意上传完成后,需要打Tag,避免后续rapidocr whl包中找不到模型下载路径。
221309
222- 我这里已经上传到了魔搭上,详细链接参见:[ link] ( https://www.modelscope.cn/models/RapidAI/RapidOCR/files?version=v2.1.0 )
310+ 我这里已经上传到了魔搭上,详细链接参见:[ link] ( https://www.modelscope.cn/models/RapidAI/RapidOCR/files )
223311
224312#### 更改rapidocr代码适配
225313
0 commit comments