Skip to content

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

[python] Update rapid layout module

SWHLfa5ee5b1dc82
diff --git a/docs/doc_whl_en_onnxruntime.md b/docs/doc_whl_en_onnxruntime.md
index e48d7ad..66274dd 100644
--- a/docs/doc_whl_en_onnxruntime.md
+++ b/docs/doc_whl_en_onnxruntime.md
@@ -6,18 +6,18 @@
### 1. Install package by pypi.
```bash
-pip install rapidocr-onnxruntime
+$ pip install rapidocr-onnxruntime
```
### 2. Download the models and config yaml.
```bash
-wget https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.1.0.zip
+$ wget https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.1.0.zip
# or by gitee
# wget https://gitee.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.1.0.zip
-unzip required_for_whl_v1.1.0.zip
-cd required_for_whl_v1.1.0
+$ unzip required_for_whl_v1.1.0.zip
+$ cd required_for_whl_v1.1.0
```
- The final directory of the folder:
diff --git a/docs/doc_whl_en_openvino.md b/docs/doc_whl_en_openvino.md
index 0b10791..1fad791 100644
--- a/docs/doc_whl_en_openvino.md
+++ b/docs/doc_whl_en_openvino.md
@@ -6,18 +6,18 @@
### 1. Install package by pypi.
```shell
-pip install rapidocr-openvino
+$ pip install rapidocr-openvino
```
### 2. Download the models and config yaml.
```bash
-wget https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.1.0.zip
+$ wget https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.1.0.zip
# or by gitee
# wget https://gitee.com/RapidAI/RapidOCR/releases/download/v1.1.0/required_for_whl_v1.1.0.zip
-unzip required_for_whl_v1.1.0.zip
-cd required_for_whl_v1.1.0
+$ unzip required_for_whl_v1.1.0.zip
+$ cd required_for_whl_v1.1.0
```
- The final directory of the folder:
diff --git a/docs/doc_whl_layout.md b/docs/doc_whl_layout.md
new file mode 100644
index 0000000..d280990
--- /dev/null
+++ b/docs/doc_whl_layout.md
@@ -0,0 +1,36 @@
+## rapid-layout Package
+<p>
+ <a href=""><img src="https://img.shields.io/badge/Python-3.6+-aff.svg"></a>
+ <a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
+</p>
+
+### 1. Install package by pypi.
+```bash
+$ pip install rapidocr-layout
+```
+
+### 2. Run by script.
+```python
+import cv2
+from rapid_layout import RapidLayout
+
+layout_engine = RapidLayout()
+
+img = cv2.imread('test_images/ch.png')
+
+layout_res, elapse = layout_engine(img)
+print(layout_res)
+```
+
+### 3. Run by command line.
+```bash
+$ rapid_layout --img_path test_images/ch.png
+```
+
+### 4. Result
+```text
+[
+ {'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'}
+]
+```
diff --git a/python/rapid_structure/README.md b/python/rapid_structure/README.md
index 990b394..b49df8f 100644
--- a/python/rapid_structure/README.md
+++ b/python/rapid_structure/README.md
@@ -1 +1,47 @@
#### Rapid Structure
+- 模型和推理代码均来自于[PP-Structure](https://github.com/PaddlePaddle/PaddleOCR/blob/release%2F2.6/ppstructure/README_ch.md#32)部分。
+- 该部分的功能主要针对文档类图像,包括版面分析和表格识别两类。
+
+#### Rapid Layout
+##### 简介和说明
+- 该部分主要是做文档类图像的版面分析。具体来说,就是分析给定的文档类别图像(论文截图等),定位其中类别和位置,如标题、段落、表格和图片等各个部分。
+- 目前支持三种类别的版面分析模型:中文、英文和表格版面分析模型,具体可参见下面表格:
+
+ | 模型类型 | 模型名称 | 模型大小 | 支持种类 |
+ |:---:|:---:|:---:|:---:|
+ | 表格 | `layout_table.onnx` | 7.06M | `table` |
+ | 英文 | `layout_publaynet.onnx` | 7.06M | `text title list table figure` |
+ | 中文 | `layout_cdla.onnx` | 7.07M | `text title figure figure_caption table table_caption` <br> `header footer reference equation` |
+- 模型下载地址为:[百度网盘](https://pan.baidu.com/s/1PI9fksW6F6kQfJhwUkewWg?pwd=p29g) | [Google Drive](https://drive.google.com/drive/folders/1DAPWSN2zGQ-ED_Pz7RaJGTjfkN2-Mvsf?usp=sharing)
+
+#### 使用方式
+1. pip安装
+ - 由于模型较小,预先将中文版面分析模型(`layout_cdla.onnx`)打包进了whl包内,如果做中文版面分析,可直接安装使用
+ ```bash
+ $ pip install
+ ```
+2. python脚本运行
+ ```python
+ import cv2
+ from rapid_layout import RapidLayout
+
+ layout_engine = RapidLayout()
+
+ img = cv2.imread('test_images/ch.png')
+
+ layout_res, elapse = layout_engine(img)
+ ```
+3. 终端运行
+ ```bash
+ # 假设在rapid_structure目录下
+ $ rapid_layout --img_path test_images/ch.png
+ ```
+
+#### 输出结果
+- 格式为:
+ ```text
+ [
+ {'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'}
+ ]
+ ```
\ No newline at end of file
diff --git a/python/rapid_structure/demo.py b/python/rapid_structure/demo.py
index f9d8475..daa28f9 100644
--- a/python/rapid_structure/demo.py
+++ b/python/rapid_structure/demo.py
@@ -9,7 +9,7 @@ import numpy as np
from rapid_layout import RapidLayout
-def vis_layout(img, layout_res: list):
+def vis_layout(img: np.ndarray, layout_res: list) -> None:
tmp_img = copy.deepcopy(img)
for v in layout_res:
bbox = np.round(v['bbox']).astype(np.int32)
@@ -32,14 +32,14 @@ def vis_layout(img, layout_res: list):
if __name__ == '__main__':
- model_path = 'models/layout_cdla.onnx'
- rapid_layout = RapidLayout(model_path)
+ model_path = 'rapid_layout/models/layout_cdla.onnx'
+ layout_engine = RapidLayout(model_path)
img_path = 'test_images/ch.png'
img = cv2.imread(img_path)
- layout_res, elapse = rapid_layout(img)
+ layout_res, elapse = layout_engine(img)
vis_layout(img, layout_res)
print(layout_res)
diff --git a/python/rapid_structure/models/.gitkeep b/python/rapid_structure/rapid_layout/models/.gitkeep
similarity index 100%
rename from python/rapid_structure/models/.gitkeep
rename to python/rapid_structure/rapid_layout/models/.gitkeep
diff --git a/python/rapid_structure/rapid_layout/rapid_layout.py b/python/rapid_structure/rapid_layout/rapid_layout.py
index f5afcdb..9dca399 100644
--- a/python/rapid_structure/rapid_layout/rapid_layout.py
+++ b/python/rapid_structure/rapid_layout/rapid_layout.py
@@ -22,9 +22,12 @@ root_dir = Path(__file__).resolve().parent
class RapidLayout(object):
- def __init__(self, model_path: str):
+ def __init__(self,
+ model_path: str = None):
config_path = str(root_dir / 'config.yaml')
config = read_yaml(config_path)
+ if model_path is None:
+ model_path = str(root_dir / 'models' / 'layout_cdla.onnx')
config['model_path'] = model_path
session_instance = OrtInferSession(config)
@@ -64,3 +67,26 @@ class RapidLayout(object):
post_preds = self.postprocess_op(ori_im, img, preds)
elapse = time.time() - starttime
return post_preds, elapse
+
+
+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'))
+ 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 __name__ == '__main__':
+ main()
diff --git a/python/rapid_structure/requirements.txt b/python/rapid_structure/requirements.txt
new file mode 100644
index 0000000..63b47e1
--- /dev/null
+++ b/python/rapid_structure/requirements.txt
@@ -0,0 +1,6 @@
+numpy==1.21.6
+onnxruntime==1.10.0
+opencv_python==4.6.0.66
+setuptools==61.2.0
+PyYAML
+pytest
\ No newline at end of file
diff --git a/python/rapid_structure/setup_layout.py b/python/rapid_structure/setup_layout.py
new file mode 100644
index 0000000..94abfb8
--- /dev/null
+++ b/python/rapid_structure/setup_layout.py
@@ -0,0 +1,70 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+from pathlib import Path
+import setuptools
+import subprocess
+
+
+def get_latest_version(package_name):
+ output = subprocess.run(["pip", "index", "versions", package_name],
+ capture_output=True)
+ output = output.stdout.decode('utf-8')
+ if output:
+ output = list(filter(lambda x: len(x) > 0, output.split('\n')))
+ latest_version = output[0].split(' ')[-1][1:-1]
+ return latest_version
+ return None
+
+
+def version_add_one(version, add_loc=-1):
+ if version:
+ version_list = version.split('.')
+ mini_version = str(int(version_list[add_loc]) + 1)
+ version_list[add_loc] = mini_version
+ new_version = '.'.join(version_list)
+ return new_version
+ return '0.0.1'
+
+
+def get_readme():
+ root_dir = Path(__file__).resolve().parent.parent.parent
+ readme_path = str(root_dir / 'docs' / 'doc_whl_layout.md')
+ with open(readme_path, 'r') as f:
+ readme = f.read()
+ return readme
+
+
+MODULE_NAME = 'rapid_layout'
+latest_version = get_latest_version(MODULE_NAME)
+VERSION_NUM = version_add_one(latest_version)
+
+setuptools.setup(
+ name=MODULE_NAME,
+ version=VERSION_NUM,
+ platforms="Any",
+ description="RapidLayout",
+ long_description=get_readme(),
+ long_description_content_type='text/markdown',
+ author="SWHL",
+ author_email="[email protected]",
+ url="https://github.com/RapidAI/RapidOCR",
+ license='Apache-2.0',
+ include_package_data=True,
+ install_requires=["onnxruntime>=1.7.0", "PyYAML>=6.0",
+ "opencv_python>=4.5.1.48", "numpy>=1.21.6"],
+ packages=[MODULE_NAME, f'{MODULE_NAME}.models'],
+ package_data={'': ['layout_cdla.onnx', '*.yaml']},
+ keywords=[
+ 'ppstructure,layout,rapidocr'
+ ],
+ classifiers=[
+ 'Programming Language :: Python :: 3.6',
+ 'Programming Language :: Python :: 3.7',
+ 'Programming Language :: Python :: 3.8',
+ 'Programming Language :: Python :: 3.9',
+ ],
+ entry_points={
+ 'console_scripts': ['rapid_layout=rapid_layout.rapid_layout:main']
+ }
+)
diff --git a/python/rapid_structure/test_images/en.png b/python/rapid_structure/test_images/en.png
deleted file mode 100644
index 49c259d..0000000
Binary files a/python/rapid_structure/test_images/en.png and /dev/null differ
diff --git a/python/rapid_structure/tests/test_layout.py b/python/rapid_structure/tests/test_layout.py
new file mode 100644
index 0000000..47f5f33
--- /dev/null
+++ b/python/rapid_structure/tests/test_layout.py
@@ -0,0 +1,25 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+import sys
+from pathlib import Path
+
+import cv2
+
+# rapid_structure路径
+root_dir = Path(__file__).resolve().parent.parent
+
+sys.path.append(str(root_dir))
+
+from rapid_layout import RapidLayout
+
+
+def test_layout():
+ img_path = str(root_dir / 'test_images' / 'ch.png')
+ img = cv2.imread(img_path)
+
+ layout_engine = RapidLayout()
+
+ layout_res, elapse = layout_engine(img)
+
+ assert len(layout_res) == 13