Skip to content

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

Add --vis param of rapid-layout

SWHL984b41c8e1c9
diff --git a/README.md b/README.md
index cf76c33..cb41f7f 100755
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href="https://github.com/RapidAI/RapidOCR/graphs/contributors"><img src="https://img.shields.io/github/contributors/RapidAI/RapidOCR?color=9ea"></a>
<a href="https://pypi.org/project/rapidocr-onnxruntime/"><img src="https://img.shields.io/pypi/dm/rapidocr-onnxruntime?color=9cf"></a>
- <a href="https://pypi.org/project/rapidocr-onnxruntime/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapidocr-onnxruntime?style=plastic"></a>
+ <a href="https://pypi.org/project/rapidocr-onnxruntime/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapidocr-onnxruntime"></a>
<a href="https://github.com/RapidAI/RapidOCR/stargazers"><img src="https://img.shields.io/github/stars/RapidAI/RapidOCR?color=ccf"></a>
</p>
diff --git a/docs/README_en.md b/docs/README_en.md
index 558ea07..80f1d95 100644
--- a/docs/README_en.md
+++ b/docs/README_en.md
@@ -16,7 +16,7 @@
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
<a href="https://github.com/RapidAI/RapidOCR/graphs/contributors"><img src="https://img.shields.io/github/contributors/RapidAI/RapidOCR?color=9ea"></a>
<a href="https://pypi.org/project/rapidocr-onnxruntime/"><img src="https://img.shields.io/pypi/dm/rapidocr-onnxruntime?color=9cf"></a>
- <a href="https://pypi.org/project/rapidocr-onnxruntime/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapidocr-onnxruntime?style=plastic"></a>
+ <a href="https://pypi.org/project/rapidocr-onnxruntime/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapidocr-onnxruntime"></a>
<a href="https://github.com/RapidAI/RapidOCR/stargazers"><img src="https://img.shields.io/github/stars/RapidAI/RapidOCR?color=ccf"></a>
</p>
diff --git a/docs/doc_whl_rapid_layout.md b/docs/doc_whl_rapid_layout.md
index 9f8b35d..506f17a 100644
--- a/docs/doc_whl_rapid_layout.md
+++ b/docs/doc_whl_rapid_layout.md
@@ -2,6 +2,7 @@
<p>
<a href=""><img src="https://img.shields.io/badge/Python->=3.7,<=3.10-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
+ <a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout"></a>
</p>
### 1. Install package by pypi.
@@ -10,29 +11,47 @@ $ pip install rapidocr-layout
```
### 2. Run by script.
+- RapidLayout has the default `model_path` value, you can set the different value of `model_path` to use different models, e.g. `layout_engine = RapidLayout(model_path='layout_publaynet.onnx')`
+- See details, for [README_Layout](https://github.com/RapidAI/RapidOCR/blob/f133ff008a1c60edd6e0ed882da83873aa7b113a/python/rapid_structure/docs/README_Layout.md) .
+- 📌 `layout.png` source: [link](https://github.com/RapidAI/RapidOCR/blob/f133ff008a1c60edd6e0ed882da83873aa7b113a/python/rapid_structure/test_images/layout.png)
+
```python
import cv2
from rapid_layout import RapidLayout
layout_engine = RapidLayout()
-img = cv2.imread('test_images/ch.png')
+img = cv2.imread('layout.png')
layout_res, elapse = layout_engine(img)
print(layout_res)
```
### 3. Run by command line.
-```bash
-$ rapid_layout --img_path test_images/layout.png
-```
+- Usage:
+ ```bash
+ $ rapid_layout -h
+ usage: rapid_layout [-h] [-v] -img IMG_PATH [-m MODEL_PATH]
+
+ optional arguments:
+ -h, --help show this help message and exit
+ -v, --vis Wheter to visualize the layout results.
+ -img IMG_PATH, --img_path IMG_PATH
+ Path to image for layout.
+ -m MODEL_PATH, --model_path MODEL_PATH
+ The model path used for inference.
+ ```
+- Example:
+ ```bash
+ $ rapid_layout -v -img layout.png
+ ```
### 4. Result.
- Return value.
- ```text
+ ```python
[
- {'bbox': array([321.4160495, 91.53214898, 562.06141263, 199.85522603]), 'label': 'text'},
- {'bbox': array([58.67292211, 107.29000663, 300.25448676, 199.68142]), 'label': 'table_caption'}
+ {'bbox': array([321.4160495, 91.53214898, 562.06141263, 199.85522603]), 'label': 'text'},
+ {'bbox': array([58.67292211, 107.29000663, 300.25448676, 199.68142]), 'label': 'table_caption'}
]
```
- Visualize result.
diff --git a/python/rapid_structure/docs/README_Layout.md b/python/rapid_structure/docs/README_Layout.md
index f67731a..a18d5d1 100644
--- a/python/rapid_structure/docs/README_Layout.md
+++ b/python/rapid_structure/docs/README_Layout.md
@@ -2,7 +2,7 @@
<p align="left">
<a href=""><img src="https://img.shields.io/badge/Python->=3.7,<=3.10-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
- <a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout?style=plastic"></a>
+ <a href="https://pypi.org/project/rapid-layout/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-layout"></a>
<a href="https://pypi.org/project/rapid-layout/"><img src="https://img.shields.io/pypi/dm/rapid-layout?color=9cf"></a>
</p>
@@ -29,19 +29,34 @@
import cv2
from rapid_layout import RapidLayout
- # 提供model_path参数,可以自行指定上述3个模型,默认是layout_cdla.onnx
- # model_path='xxx.onnx'
+ # RapidLayout类提供model_path参数,可以自行指定上述3个模型,默认是layout_cdla.onnx
+ # layout_engine = RapidLayout(model_path='layout_publaynet.onnx')
layout_engine = RapidLayout()
img = cv2.imread('test_images/layout.png')
layout_res, elapse = layout_engine(img)
```
+
3. 终端运行
- ```bash
- # 假设在rapid_structure目录下
- $ rapid_layout --img_path test_images/layout.png
- ```
+ - 用法:
+ ```bash
+ $ rapid_layout -h
+ usage: rapid_layout [-h] [-v] -img IMG_PATH [-m MODEL_PATH]
+
+ optional arguments:
+ -h, --help show this help message and exit
+ -v, --vis Wheter to visualize the layout results.
+ -img IMG_PATH, --img_path IMG_PATH
+ Path to image for layout.
+ -m MODEL_PATH, --model_path MODEL_PATH
+ The model path used for inference.
+ ```
+ - 示例:
+ ```bash
+ $ rapid_layout -v -img layout.png
+ ```
+
4. 结果
- 返回结果
```python
diff --git a/python/rapid_structure/docs/README_Table.md b/python/rapid_structure/docs/README_Table.md
index 4f35f7a..340be97 100644
--- a/python/rapid_structure/docs/README_Table.md
+++ b/python/rapid_structure/docs/README_Table.md
@@ -2,7 +2,7 @@
<p align="left">
<a href=""><img src="https://img.shields.io/badge/Python->=3.7,<=3.10-aff.svg"></a>
<a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
- <a href="https://pypi.org/project/rapid-table/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-table?style=plastic"></a>
+ <a href="https://pypi.org/project/rapid-table/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapid-table"></a>
<a href="https://pypi.org/project/rapid-table/"><img src="https://img.shields.io/pypi/dm/rapid-table?color=9cf"></a>
</p>
diff --git a/python/rapid_structure/rapid_layout/rapid_layout.py b/python/rapid_structure/rapid_layout/rapid_layout.py
index b7c29b8..9c6319e 100644
--- a/python/rapid_structure/rapid_layout/rapid_layout.py
+++ b/python/rapid_structure/rapid_layout/rapid_layout.py
@@ -11,12 +11,15 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
+import argparse
import time
from pathlib import Path
+
+import cv2
import numpy as np
from .utils import (OrtInferSession, PicoDetPostProcess, create_operators,
- read_yaml, transform)
+ read_yaml, transform, vis_layout)
root_dir = Path(__file__).resolve().parent
@@ -70,23 +73,27 @@ class RapidLayout():
def main():
- import argparse
- import cv2
-
parser = argparse.ArgumentParser()
- parser.add_argument('--img_path', type=str, required=True)
- parser.add_argument('--model_path', type=str,
- default=str(root_dir / 'models' / 'layout_cdla.onnx'))
+ parser.add_argument('-v', '--vis', action='store_true',
+ help='Wheter to visualize the layout results.')
+ parser.add_argument('-img', '--img_path', type=str, required=True,
+ help='Path to image for layout.')
+ parser.add_argument('-m', '--model_path', type=str,
+ default=str(root_dir / 'models' / 'layout_cdla.onnx'),
+ help='The model path used for inference.')
args = parser.parse_args()
layout_engine = RapidLayout(args.model_path)
img = cv2.imread(args.img_path)
-
layout_res, elapse = layout_engine(img)
-
print(layout_res)
+ if args.vis:
+ img_path = Path(args.img_path)
+ save_path = img_path.resolve().parent / f'vis_{img_path.name}'
+ vis_layout(img, layout_res, str(save_path))
+
if __name__ == '__main__':
main()
diff --git a/python/rapid_structure/rapid_layout/utils.py b/python/rapid_structure/rapid_layout/utils.py
index 88a7f87..6232d4b 100644
--- a/python/rapid_structure/rapid_layout/utils.py
+++ b/python/rapid_structure/rapid_layout/utils.py
@@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
+import copy
import warnings
import yaml
@@ -63,9 +64,6 @@ def transform(data, ops=None):
def create_operators(op_param_dict):
- """
- create operators based on the config
- """
ops = []
for op_name, param in op_param_dict.items():
if param is None:
@@ -106,9 +104,6 @@ class Resize():
class NormalizeImage():
- """ normalize image such as substract mean, divide std
- """
-
def __init__(self, scale=None, mean=None, std=None, order='chw', **kwargs):
if isinstance(scale, str):
scale = eval(scale)
@@ -130,8 +125,6 @@ class NormalizeImage():
class ToCHWImage():
- """ convert hwc image to chw image
- """
def __init__(self, **kwargs):
pass
@@ -159,13 +152,6 @@ def read_yaml(yaml_path):
class PicoDetPostProcess():
- """
- Args:
- input_shape (int): network input image size
- ori_shape (int): ori image shape of before padding
- scale_factor (float): scale factor of ori image
- enable_mkldnn (bool): whether to open MKLDNN
- """
def __init__(self,
labels,
strides=[8, 16, 32, 64],
@@ -409,3 +395,28 @@ class PicoDetPostProcess():
"""
hw = np.clip(right_bottom - left_top, 0.0, None)
return hw[..., 0] * hw[..., 1]
+
+
+def vis_layout(img: np.ndarray, layout_res: list, save_path: str) -> None:
+ font = cv2.FONT_HERSHEY_COMPLEX
+ font_scale = 1
+ font_color = (0, 0, 255)
+ font_thickness = 1
+
+ tmp_img = copy.deepcopy(img)
+ for v in layout_res:
+ bbox = np.round(v['bbox']).astype(np.int32)
+ label = v['label']
+
+ start_point = (bbox[0], bbox[1])
+ end_point = (bbox[2], bbox[3])
+
+ cv2.rectangle(tmp_img, start_point, end_point, (0, 255, 0), 2)
+
+ (w, h), _ = cv2.getTextSize(label, font, font_scale, font_thickness)
+ put_point = start_point[0], start_point[1] + h
+ cv2.putText(tmp_img, label, put_point, font, font_scale,
+ font_color, font_thickness)
+
+ cv2.imwrite(save_path, tmp_img)
+ print(f'The infer result has saved in {save_path}')