Skip to content

📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

Optimize python inference part

SWHL2408ec64cd20
diff --git a/README.md b/README.md
index bc223b8..3a7b011 100755
--- a/README.md
+++ b/README.md
@@ -23,13 +23,14 @@
- [RapidOCR (捷智OCR)](#rapidocr-捷智ocr)
- [简介](#简介)
- [近期更新](#近期更新)
- - [🎄2021-12-18 update](#2021-12-18-update)
+ - [🔉2022-02-24 update](#2022-02-24-update)
+ - [2021-12-18 update](#2021-12-18-update)
- [2021-11-28 update](#2021-11-28-update)
+ - [之前更新记录](#之前更新记录)
- [2021-11-13 update](#2021-11-13-update)
- [2021-10-27 update](#2021-10-27-update)
- [2021-09-13 update](#2021-09-13-update)
- [2021-09-11 update](#2021-09-11-update)
- - [之前更新记录](#之前更新记录)
- [2021-08-07 update](#2021-08-07-update)
- [2021-07-17 update](#2021-07-17-update)
- [2021-07-04 update](#2021-07-04-update)
@@ -66,9 +67,13 @@
- 基于百度的开源PaddleOCR 模型及训练,任何人可以使用本推理库,也可以根据自己的需求使用百度的paddlepaddle框架进行模型优化。
## 近期更新
-#### 🎄2021-12-18 update
+#### 🔉2022-02-24 update
+- 优化python目录下的推理代码
+- 添加调用不同语言模型的推理代码示例
+- 详情参见:[python/README](./python/README.md)
+
+#### 2021-12-18 update
- 添加[Google Colab Demo](https://colab.research.google.com/github/RapidAI/RapidOCR/blob/main/RapidOCRDemo.ipynb)
-- 更改`python/rapidOCR.sh`中默认det模型
#### 2021-11-28 update
- 更新[ocrweb](http://rapidocr.51pda.cn:9003/)部分
@@ -76,6 +81,10 @@
- 更新说明文档
- 更换文本检测模型为`ch_PP-OCRv2_det_infer.onnx`,推理更快,更准
+#### 之前更新记录
+<details>
+ <summary>点击查看</summary>
+
#### 2021-11-13 update
- 添加python版本中文本检测和识别可调节的超参数,主要有`box_thresh|unclip_ratio|text_score`,详情见[参数调节](python/README.md#相关调节参数)
- 将文本识别中字典位置以参数方式给出,便于灵活配置,详情见[keys_path](python/rapidOCR.sh)
@@ -95,10 +104,6 @@
- PP-OCRv2识别模型效果无明显提升,模型大小增加了3.58M。
- 模型上传到[百度网盘 提取码:30jv](https://pan.baidu.com/s/1qkqWK4wRdMjqGGbzR-FyWg)
-#### 之前更新记录
-<details>
- <summary>点击查看</summary>
-
#### 2021-08-07 update
- 现在正在做的
- [ ] PP-Structure 表格结构和cell坐标预测 正在整理中
diff --git a/README_en.md b/README_en.md
index fe7dfc9..d8bb223 100644
--- a/README_en.md
+++ b/README_en.md
@@ -21,8 +21,9 @@
- [RapidOCR](#rapidocr)
- [Introduction](#introduction)
- - [Recently updates、](#recently-updates)
- - [🎄2021-12-18 update](#2021-12-18-update)
+ - [Recently updates](#recently-updates)
+ - [🔉2022-02-24 update](#2022-02-24-update)
+ - [2021-12-18 update](#2021-12-18-update)
- [2021-11-28 update](#2021-11-28-update)
- [2021-11-13 update](#2021-11-13-update)
- [2021-10-27 update](#2021-10-27-update)
@@ -63,8 +64,13 @@
- Based on Baidu's open source PaddleOCR model and training, anyone can use this inference library, or use Baidu's paddlepaddle framework for model optimization according to their own needs.
-## Recently updates、
-#### 🎄2021-12-18 update
+## Recently updates
+#### 🔉2022-02-24 update
+- Optimize the inference code of the python part.
+- Add inference code examples that use the different language models.
+- For details, see: [python/README](./python/README.md)
+
+#### 2021-12-18 update
- Add [Google Colab Demo](https://colab.research.google.com/github/RapidAI/RapidOCR/blob/main/RapidOCRDemo.ipynb).
- Change the default det model of the `python/rapidOCR.sh`
@@ -74,6 +80,10 @@
- Add docs of the ocrweb.
- Change the det model(`ch_PP-OCRv2_det_infer.onnx`), faster and more accurate.
+
+<details>
+ <summary>Previous update logs</summary>
+
#### 2021-11-13 update
- Add adjustable super parameters for text detection and recognition in Python version, mainly `box_thresh|unclip_ratio|text_score`, see [parameter adjustment](python/README.md#相关调节参数) for details
- The dictionary position in text recognition is given in parameter mode to facilitate flexible configuration. See [keys_path](python/rapidOCR.sh) for details
@@ -95,9 +105,6 @@
- Upload the model to [Baidu online disk extraction code: 30jv](https://pan.baidu.com/s/1qkqWK4wRdMjqGGbzR-FyWg) or [Google Drive](https://drive.google.com/drive/folders/1x_a9KpCo_1blxH1xFOfgKVkw1HYRVywY?usp=sharing)
-<details>
- <summary>Previous update logs</summary>
-
#### 2021-08-07 update
- TODO:
- [ ] PP structure table structure and cell coordinate prediction are being sorted out.
diff --git a/python/README.md b/python/README.md
index 09f9f8c..9bad51b 100755
--- a/python/README.md
+++ b/python/README.md
@@ -1,36 +1,77 @@
-### 运行
+### 使用步骤
1. 下载相应模型和用于显示的字体文件
- - [提取码:30jv](https://pan.baidu.com/s/1qkqWK4wRdMjqGGbzR-FyWg)
- - 下载之后放在`models`目录即可
-2. 运行`rapidOCR.py`文件, 即可
- ```shell
- cd python
- python rapidOCR.py
- ```
-
-
-### 文本检测+方向分类+文本识别
-
-```python
-from ch_ppocr_mobile_v2_cls import TextClassifier
-from ch_ppocr_mobile_v2_det import TextDetector
-from ch_ppocr_mobile_v2_rec import TextRecognizer
-
-det_model_path = 'models/ch_ppocr_mobile_v2.0_det_infer.onnx'
-cls_model_path = 'models/ch_ppocr_mobile_v2.0_cls_infer.onnx'
-rec_model_path = 'models/ch_ppocr_mobile_v2.0_rec_infer.onnx'
-
-image_path = r'test_images/det_images/1.jpg'
-keys_path = 'ch_ppocr_mobile_v2_rec/ppocr_keys_v1.txt'
-
-text_sys = TextSystem(det_model_path,
- rec_model_path,
- use_angle_cls=True,
- cls_model_path=cls_model_path,
- keys_path=keys_path)
-dt_boxes, rec_res = text_sys(image_path)
-visualize(image_path, dt_boxes, rec_res)
-```
+ - [百度网盘 提取码:30jv](https://pan.baidu.com/s/1qkqWK4wRdMjqGGbzR-FyWg) | [Google Drive](https://drive.google.com/drive/folders/1x_a9KpCo_1blxH1xFOfgKVkw1HYRVywY?usp=sharing)
+
+ - 下载之后放在`models`目录即可,最终`models`目录结构如下:
+ ```text
+ models
+ |-- ch_PP-OCRv2_det_infer.onnx
+ |-- ch_ppocr_mobile_v2.0_cls_infer.onnx
+ |-- ch_ppocr_mobile_v2.0_det_infer.onnx
+ |-- ch_ppocr_server_v2.0_det_infer.onnx
+ |-- ch_ppocr_server_v2.0_rec_infer.onnx
+ |-- en_number_mobile_v2.0_rec_infer.onnx
+ |-- japan_rec_crnn.onnx
+ `-- msyh.ttc
+ ```
+
+2. 运行
+ - 接口调用方式运行
+ - 如果想要使用其他语言的识别模型,可以只对`rec_model_path`和`keys_path`作对应更改即可
+ - **!!!各个不同语言的推理代码区别只在于模型和字典文件!!!**
+ ```python
+ from rapid_ocr_api import TextSystem, visualize
+
+ det_model_path = 'models/ch_ppocr_mobile_v2.0_det_infer.onnx'
+ cls_model_path = 'models/ch_ppocr_mobile_v2.0_cls_infer.onnx'
+
+ # 中英文识别
+ rec_model_path = 'models/ch_ppocr_mobile_v2.0_rec_infer.onnx'
+ keys_path = 'ch_ppocr_mobile_v2_rec/ppocr_keys_v1.txt'
+
+ text_sys = TextSystem(det_model_path,
+ rec_model_path,
+ use_angle_cls=True,
+ cls_model_path=cls_model_path,
+ keys_path=keys_path)
+
+ image_path = r'test_images/det_images/ch_en_num.jpg'
+ dt_boxes, rec_res = text_sys(image_path)
+ visualize(image_path, dt_boxes, rec_res)
+
+ # 只有中英文和数字识别
+ rec_model_path = 'models/en_number_mobile_v2.0_rec_infer.onnx'
+ keys_path = 'en_number_ppocr_mobile_v2_rec/en_dict.txt'
+
+ text_sys = TextSystem(det_model_path,
+ rec_model_path,
+ use_angle_cls=True,
+ cls_model_path=cls_model_path,
+ keys_path=keys_path)
+
+ image_path = r'test_images/det_images/en_num.png'
+ dt_boxes, rec_res = text_sys(image_path)
+ visualize(image_path, dt_boxes, rec_res)
+
+ # 日语识别
+ rec_model_path = 'models/japan_rec_crnn.onnx'
+ keys_path = 'japan_ppocr_mobile_v2_rec/japan_dict.txt'
+
+ text_sys = TextSystem(det_model_path,
+ rec_model_path,
+ use_angle_cls=True,
+ cls_model_path=cls_model_path,
+ keys_path=keys_path)
+
+ image_path = r'test_images/det_images/japan.png'
+ dt_boxes, rec_res = text_sys(image_path)
+ visualize(image_path, dt_boxes, rec_res)
+ ```
+
+ - 命令行运行
+ ```shell
+ $ bash test_demo.sh
+ ```
### 相关调节参数
|参数名称|作用|建议取值范围|默认值|代码位置|
diff --git a/python/rapidOCR.sh b/python/rapidOCR.sh
deleted file mode 100644
index dd7cc9b..0000000
--- a/python/rapidOCR.sh
+++ /dev/null
@@ -1,12 +0,0 @@
-#! /bin/bash
-
-# The img_path can be file path or folder path.
-img_path="test_images/det_images/1.jpg"
-
-keys_path="ch_ppocr_mobile_v2_rec/ppocr_keys_v1.txt"
-python rapidOCR.py --det_model_path models/ch_PP-OCRv2_det_infer.onnx \
- --cls_model_path models/ch_ppocr_mobile_v2.0_cls_infer.onnx \
- --rec_model_path models/ch_ppocr_mobile_v2.0_rec_infer.onnx \
- --image_path ${img_path} \
- --text_score 0.5 \
- --keys_path ${keys_path}
diff --git a/python/rapidOCR.py b/python/rapid_ocr_api.py
similarity index 95%
rename from python/rapidOCR.py
rename to python/rapid_ocr_api.py
index e2b42ff..f5a6886 100644
--- a/python/rapidOCR.py
+++ b/python/rapid_ocr_api.py
@@ -79,6 +79,10 @@ def check_and_read_gif(img_path):
def draw_ocr_box_txt(image, boxes, txts,
scores=None, text_score=0.5,
font_path="./models/msyh.ttc"):
+ if not Path(font_path).exists():
+ raise FileNotFoundError(f'The {font_path} does not exists! \n'
+ f'Please download the file in the https://drive.google.com/drive/folders/1x_a9KpCo_1blxH1xFOfgKVkw1HYRVywY')
+
h, w = image.height, image.width
img_left = image.copy()
img_right = Image.new('RGB', (w, h), (255, 255, 255))
@@ -118,8 +122,7 @@ def draw_ocr_box_txt(image, boxes, txts,
cur_y += char_size[1]
else:
font_size = max(int(box_height * 0.8), 10)
- font = ImageFont.truetype(font_path, font_size,
- encoding="utf-8")
+ font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
draw_right.text([box[0][0], box[0][1]], txt,
fill=(0, 0, 0), font=font)
@@ -155,6 +158,8 @@ class TextSystem(object):
cls_model_path=None,
keys_path=None):
super(TextSystem).__init__()
+ self.text_score = 0.5
+
self.text_detector = TextDetector(det_model_path)
self.text_recognizer = TextRecognizer(rec_model_path, keys_path)
self.use_angle_cls = use_angle_cls
@@ -252,7 +257,7 @@ class TextSystem(object):
filter_boxes, filter_rec_res = [], []
for box, rec_reuslt in zip(dt_boxes, rec_res):
text, score = rec_reuslt
- if score >= text_score:
+ if score >= self.text_score:
filter_boxes.append(box)
filter_rec_res.append(rec_reuslt)
return filter_boxes, filter_rec_res
@@ -270,12 +275,10 @@ if __name__ == '__main__':
parser.add_argument('--image_path', type=str,
default='test_images/det_images/')
- parser.add_argument('--text_score', type=float, default=0.5)
parser.add_argument('--keys_path', type=str,
default="ch_ppocr_mobile_v2_rec/ppocr_keys_v1.txt")
args = parser.parse_args()
- text_score = args.text_score
if args.det_model_path.find('server') != -1:
from ch_ppocr_server_v2_det import TextDetector
@@ -287,6 +290,8 @@ if __name__ == '__main__':
else:
if args.rec_model_path.find('en') != -1:
from en_number_ppocr_mobile_v2_rec import TextRecognizer
+ elif args.rec_model_path.find('japan') != -1:
+ from japan_ppocr_mobile_v2_rec import TextRecognizer
else:
from ch_ppocr_mobile_v2_rec import TextRecognizer
diff --git a/python/test_demo.py b/python/test_demo.py
new file mode 100644
index 0000000..46d7903
--- /dev/null
+++ b/python/test_demo.py
@@ -0,0 +1,51 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+from rapid_ocr_api import TextSystem, visualize
+
+det_model_path = 'models/ch_ppocr_mobile_v2.0_det_infer.onnx'
+cls_model_path = 'models/ch_ppocr_mobile_v2.0_cls_infer.onnx'
+
+# 中英文识别
+rec_model_path = 'models/ch_ppocr_mobile_v2.0_rec_infer.onnx'
+keys_path = 'ch_ppocr_mobile_v2_rec/ppocr_keys_v1.txt'
+
+text_sys = TextSystem(det_model_path,
+ rec_model_path,
+ use_angle_cls=True,
+ cls_model_path=cls_model_path,
+ keys_path=keys_path)
+
+image_path = r'test_images/det_images/ch_en_num.jpg'
+dt_boxes, rec_res = text_sys(image_path)
+visualize(image_path, dt_boxes, rec_res)
+
+
+# 只有中英文和数字识别
+rec_model_path = 'models/en_number_mobile_v2.0_rec_infer.onnx'
+keys_path = 'en_number_ppocr_mobile_v2_rec/en_dict.txt'
+
+text_sys = TextSystem(det_model_path,
+ rec_model_path,
+ use_angle_cls=True,
+ cls_model_path=cls_model_path,
+ keys_path=keys_path)
+
+image_path = r'test_images/det_images/en_num.png'
+dt_boxes, rec_res = text_sys(image_path)
+visualize(image_path, dt_boxes, rec_res)
+
+
+# 日语识别
+rec_model_path = 'models/japan_rec_crnn.onnx'
+keys_path = 'japan_ppocr_mobile_v2_rec/japan_dict.txt'
+
+text_sys = TextSystem(det_model_path,
+ rec_model_path,
+ use_angle_cls=True,
+ cls_model_path=cls_model_path,
+ keys_path=keys_path)
+
+image_path = r'test_images/det_images/japan.png'
+dt_boxes, rec_res = text_sys(image_path)
+visualize(image_path, dt_boxes, rec_res)
diff --git a/python/test_demo.sh b/python/test_demo.sh
new file mode 100644
index 0000000..e905249
--- /dev/null
+++ b/python/test_demo.sh
@@ -0,0 +1,16 @@
+#! /bin/bash
+
+det_model_path="models/ch_PP-OCRv2_det_infer.onnx"
+cls_model_path="models/ch_ppocr_mobile_v2.0_cls_infer.onnx"
+
+# 这里可以按照自己需求,做相关更改
+rec_model_path="models/ch_ppocr_mobile_v2.0_rec_infer.onnx"
+keys_path="ch_ppocr_mobile_v2_rec/ppocr_keys_v1.txt"
+
+img_path="test_images/det_images/ch_en_num.jpg"
+
+python rapid_ocr_api.py --det_model_path ${det_model_path} \
+ --cls_model_path ${cls_model_path} \
+ --rec_model_path ${rec_model_path} \
+ --image_path ${img_path} \
+ --keys_path ${keys_path}
diff --git a/python/test_images/det_images/1.jpg b/python/test_images/det_images/ch_en_num.jpg
old mode 100755
new mode 100644
similarity index 100%
rename from python/test_images/det_images/1.jpg
rename to python/test_images/det_images/ch_en_num.jpg
diff --git a/python/test_images/det_images/en_num.png b/python/test_images/det_images/en_num.png
new file mode 100644
index 0000000..6ef7767
Binary files /dev/null and b/python/test_images/det_images/en_num.png differ
diff --git a/python/test_images/det_images/japan.png b/python/test_images/det_images/japan.png
new file mode 100644
index 0000000..733c64e
Binary files /dev/null and b/python/test_images/det_images/japan.png differ