📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
[python]Move the dict into the inside of onnx.
SWHL683743935ca8
diff --git a/python/README.md b/python/README.mdindex 7fea7c1..9f8bf16 100755--- a/python/README.md+++ b/python/README.md@@ -24,27 +24,20 @@- 每个独立的模块下均有独立的`config.yaml`配置文件,可以单独使用- `det`部分:- `det`中`mobile`和`server`版,推理代码一致,直接更改配置文件中模型路径即可- - `det`中`v2`和`v3`两个版本,推理代码一致,直接更改配置文件中模型路径即可- ```yaml- Det:- module_name: ch_ppocr_v2_det- class_name: TextDetector- model_path: resources/models/ch_PP-OCRv3_det_infer.onnx- ```+ - `det`中`v2`和`v3`两个版本,推理代码一致。- `rec`部分:- `rec`中`mobile`和`server`版本,推理代码一致,直接更改配置文件中模型路径即可- `rec`中`v2`和`v3`两个版本,共用同一个推理代码。- 两版本差别仅在输入shape和模型。经过测试,采用`v3 rec模型`+`[3, 48, 320]`效果最好。- - 目前配置文件`config.yaml`中(如下所示)已经改为该最优组合。- ```yaml- module_name: ch_ppocr_v2_rec- class_name: TextRecognizer- model_path: resources/models/ch_PP-OCRv3_rec_infer.onnx-- rec_img_shape: [3, 48, 320]- rec_batch_num: 6- keys_path: resources/rec_dict/ppocr_keys_v1.txt- ```+ - 目前配置文件`config.yaml`中(如下所示)已是最优组合。+ ```yaml+ module_name: ch_ppocr_v3_rec+ class_name: TextRecognizer+ model_path: resources/models/ch_PP-OCRv3_rec_infer.onnx++ rec_img_shape: [3, 48, 320]+ rec_batch_num: 6+ ```- onnxruntime和openvino调用方式如下:```python# 基于onnxruntime引擎推理@@ -102,15 +95,15 @@ print(rec_res)```### 源码使用步骤-1. 下载当前下的`rapidocr_onnxruntime`/`rapidocr_openvino`目录到本地+1. 下载当前目录下的`rapidocr_onnxruntime`或者`rapidocr_openvino`目录到本地2. 下载链接下的`resources`目录(包含模型和显示的字体文件)- 下载链接:[百度网盘](https://pan.baidu.com/s/1PTcgXG2zEgQU6A_A3kGJ3Q?pwd=jhai) | [Google Drive](https://drive.google.com/drive/folders/1x_a9KpCo_1blxH1xFOfgKVkw1HYRVywY?usp=sharing)- `resources/models`下模型搭配已经为最优组合(速度和精度平衡)```text- ch_PP-OCRv3_det + ch_ppocr_mobile_v2.0_cls + ch_ppocr_mobile_v2.0_rec+ ch_PP-OCRv3_det + ch_ppocr_mobile_v2.0_cls + ch_PP-OCRv3_rec```- - 最终目录如下:+ - 最终目录如下,自行比对:```text.├── README.md@@ -119,26 +112,24 @@ print(rec_res)├── rapidocr_onnxruntime│ ├── __init__.py│ ├── ch_ppocr_v2_cls- │ ├── ch_ppocr_v2_det- │ ├── ch_ppocr_v2_rec+ │ ├── ch_ppocr_v3_det+ │ ├── ch_ppocr_v3_rec│ └── rapid_ocr_api.py├── rapidocr_openvino│ ├── __init__.py│ ├── README.md│ ├── ch_ppocr_v2_cls- │ ├── ch_ppocr_v2_det- │ ├── ch_ppocr_v2_rec+ │ ├── ch_ppocr_v3_det+ │ ├── ch_ppocr_v3_rec│ └── rapid_ocr_api.py├── requirements.txt├── resources│ ├── fonts- │ │ └── msyh.ttc- │ ├── models- │ │ ├── ch_PP-OCRv3_det_infer.onnx- │ │ ├── ch_ppocr_mobile_v2.0_cls_infer.onnx- │ │ └── ch_PP-OCRv3_rec_infer.onnx- │ └── rec_dict- │ └── ppocr_keys_v1.txt+ │ │ └── FZYTK.TTF+ │ └── models+ │ ├── ch_PP-OCRv3_det_infer.onnx+ │ ├── ch_ppocr_mobile_v2.0_cls_infer.onnx+ │ └── ch_PP-OCRv3_rec_infer.onnx└── test_images├── ch_en_num.jpg└── single_line_text.jpg@@ -176,11 +167,11 @@ print(rec_res)# 基于openvino引擎推理# from rapidocr_openvino import TextSystem- config_path = 'config.yaml'- text_sys = TextSystem(config_path)+ text_sys = TextSystem('config.yaml')image_path = r'test_images/det_images/ch_en_num.jpg'img = cv2.imread(image_path)+dt_boxes, rec_res = text_sys(img)print(rec_res)```@@ -229,7 +220,6 @@ print(rec_res)| ------------: | :----------: | :-----: | :----------------------------------------------||`rec_img_shape`| - |`[3, 48, 320]`| 输入文本识别模型的图像Shape(CHW) ||`rec_batch_num`| - | 6 | 批次推理的batch大小,一般采用默认值即可,太大并没有明显提速,效果还可能会差 |- |`keys_path`| - | - | 文本识别模型推理所使用字典文件,始识别哪种类型文本而定(中英、日文等) |### onnxruntime-gpu版推理配置diff --git a/python/config.yaml b/python/config.yamlindex a63f028..5b84273 100644--- a/python/config.yaml+++ b/python/config.yaml@@ -70,4 +70,3 @@ Rec:rec_img_shape: [3, 48, 320]rec_batch_num: 6- keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yamlindex 9e149d6..e2dab8c 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml@@ -10,4 +10,3 @@ CUDAExecutionProvider:rec_img_shape: [3, 48, 320]rec_batch_num: 6-keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.pyindex 8b8677c..392e659 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py@@ -18,7 +18,7 @@ class OrtInferSession(object):cuda_ep = 'CUDAExecutionProvider'cpu_ep = 'CPUExecutionProvider'cpu_provider_options = {- "arena_extend_strategy": "kSameAsRequested",+ "arena_extend_strategy": "kSameAsRequested",}EP_list = []diff --git a/python/rapidocr_openvino/ch_ppocr_v3_rec/config.yaml b/python/rapidocr_openvino/ch_ppocr_v3_rec/config.yamlindex c66fe52..a9e4c13 100644--- a/python/rapidocr_openvino/ch_ppocr_v3_rec/config.yaml+++ b/python/rapidocr_openvino/ch_ppocr_v3_rec/config.yaml@@ -2,4 +2,3 @@ model_path: resources/models/ch_PP-OCRv3_rec_infer.onnxrec_img_shape: [3, 32, 320]rec_batch_num: 6-keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/resources/rec_dict/ppocr_keys_v1.txt b/python/rapidocr_openvino/ch_ppocr_v3_rec/ppocr_keys_v1.txtsimilarity index 100%rename from python/resources/rec_dict/ppocr_keys_v1.txtrename to python/rapidocr_openvino/ch_ppocr_v3_rec/ppocr_keys_v1.txtdiff --git a/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py b/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.pyindex f96b761..c3b7e96 100644--- a/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py+++ b/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py@@ -18,6 +18,7 @@ import argparseimport mathimport timefrom typing import List+from pathlib import Pathimport cv2import numpy as np@@ -32,7 +33,9 @@ class TextRecognizer(object):def __init__(self, config):self.rec_image_shape = config['rec_img_shape']self.rec_batch_num = config['rec_batch_num']- self.character_dict_path = config['keys_path']++ dict_path = str(Path(__file__).parent / './ppocr_keys_v1.txt')+ self.character_dict_path = config.get( 'keys_path', dict_path)self.postprocess_op = CTCLabelDecode(self.character_dict_path)openvino_instance = OpenVINOInferSession(config)diff --git a/python/test_demo.py b/python/test_demo.pyindex c6c8d7d..5878b2f 100644--- a/python/test_demo.py+++ b/python/test_demo.py@@ -91,10 +91,10 @@ def visualize(image_path, boxes, rec_res, font_path="resources/fonts/FZYTK.TTF")if __name__ == '__main__':text_sys = TextSystem('config.yaml')- image_path = r'test_images/ch_en_num.jpg'+ image_path = 'test_images/ch_en_num.jpg'img = cv2.imread(image_path)dt_boxes, rec_res = text_sys(img)print(rec_res)visualize(image_path, dt_boxes, rec_res,- font_path='resources/fonts/msyh.ttc')+ font_path='resources/fonts/FZYTK.TTF')diff --git a/python/tests/benchmark/config.yaml b/python/tests/benchmark/config.yamlindex 4b5b414..5b84273 100644--- a/python/tests/benchmark/config.yaml+++ b/python/tests/benchmark/config.yaml@@ -1,11 +1,12 @@Global:text_score: 0.5use_angle_cls: true- print_verbose: false+ print_verbose: truemin_height: 30+ width_height_ratio: 8Det:- module_name: ch_ppocr_v2_det+ module_name: ch_ppocr_v3_detclass_name: TextDetectormodel_path: resources/models/ch_PP-OCRv3_det_infer.onnx@@ -36,6 +37,7 @@ Det:max_candidates: 1000unclip_ratio: 1.6use_dilation: true+ score_mode: fastCls:module_name: ch_ppocr_v2_cls@@ -55,7 +57,7 @@ Cls:label_list: ['0', '180']Rec:- module_name: ch_ppocr_v2_rec+ module_name: ch_ppocr_v3_recclass_name: TextRecognizermodel_path: resources/models/ch_PP-OCRv3_rec_infer.onnx@@ -68,4 +70,3 @@ Rec:rec_img_shape: [3, 48, 320]rec_batch_num: 6- keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/tests/benchmark/config_gpu.yaml b/python/tests/benchmark/config_gpu.yamlindex c8d3156..eaca238 100644--- a/python/tests/benchmark/config_gpu.yaml+++ b/python/tests/benchmark/config_gpu.yaml@@ -3,9 +3,10 @@ Global:use_angle_cls: trueprint_verbose: falsemin_height: 30+ width_height_ratio: 8Det:- module_name: ch_ppocr_v2_det+ module_name: ch_ppocr_v3_detclass_name: TextDetectormodel_path: resources/models/ch_PP-OCRv3_det_infer.onnx@@ -55,7 +56,7 @@ Cls:label_list: ['0', '180']Rec:- module_name: ch_ppocr_v2_rec+ module_name: ch_ppocr_v3_recclass_name: TextRecognizermodel_path: resources/models/ch_PP-OCRv3_rec_infer.onnx@@ -68,4 +69,3 @@ Rec:rec_img_shape: [3, 48, 320]rec_batch_num: 6- keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/tests/test_rec.py b/python/tests/test_rec.pyindex 69fef63..790162d 100644--- a/python/tests/test_rec.py+++ b/python/tests/test_rec.py@@ -12,7 +12,6 @@ TextRecognizer = base.init_module(module_name, class_name)yaml_path = base.root_dir / base.package_name / module_name / 'config.yaml'config = base.read_yaml(str(yaml_path))config['model_path'] = str(base.root_dir / config['model_path'])-config['keys_path'] = str(base.root_dir / config['keys_path'])text_rec = TextRecognizer(config)