Skip to content

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

Add visualize paramter of rapid-table module.

SWHL0bddb01f9ae6
diff --git a/.github/workflows/gen_whl_to_pypi_rapid_layout.yml b/.github/workflows/gen_whl_to_pypi_rapid_layout.yml
index f4cfb49..9028515 100644
--- a/.github/workflows/gen_whl_to_pypi_rapid_layout.yml
+++ b/.github/workflows/gen_whl_to_pypi_rapid_layout.yml
@@ -73,7 +73,7 @@ jobs:
wget https://github.com/RapidAI/RapidOCR/releases/download/v1.1.0/rapid_layout_models.zip
unzip rapid_layout_models.zip
mv rapid_layout_models/*.onnx rapid_layout/models/
- python setup_layout.py bdist_wheel
+ python setup_layout.py bdist_wheel ${{ github.event.head_commit.message }}
# - name: Publish distribution 📦 to Test PyPI
# uses: pypa/[email protected]
diff --git a/docs/doc_whl_rapid_layout.md b/docs/doc_whl_rapid_layout.md
index 506f17a..29f40cf 100644
--- a/docs/doc_whl_rapid_layout.md
+++ b/docs/doc_whl_rapid_layout.md
@@ -7,7 +7,7 @@
### 1. Install package by pypi.
```bash
-$ pip install rapidocr-layout
+$ pip install rapid-layout
```
### 2. Run by script.
diff --git a/docs/doc_whl_rapid_table.md b/docs/doc_whl_rapid_table.md
index db95c31..6534b24 100644
--- a/docs/doc_whl_rapid_table.md
+++ b/docs/doc_whl_rapid_table.md
@@ -6,24 +6,28 @@
### 1. Install package by pypi.
```bash
-$ pip install rapidocr-table
+$ pip install rapid-table
```
### 2. Run by script.
+- RapidTable has the default `model_path` value, you can set the different value of `model_path` to use different models, e.g. `table_engine = RapidTable(model_path='ch_ppstructure_mobile_v2_SLANet.onnx')`
+- See details, for [README_Table](https://github.com/RapidAI/RapidOCR/blob/a87133a6fc3b8ee696e246028e529f7b2319d138/python/rapid_structure/docs/README_Table.md) .
+- 📌 `table.jpg` source: [link](https://github.com/RapidAI/RapidOCR/blob/a87133a6fc3b8ee696e246028e529f7b2319d138/python/rapid_structure/test_images/table.jpg)
+
```python
import cv2
from rapid_table import RapidTable
-rapid_table = RapidTable()
+table_engine = RapidTable()
-img = cv2.imread('test_images/table.jpg')
-table_html_str, _ = rapid_table(img)
+img = cv2.imread('table.jpg')
+table_html_str, _ = table_engine(img)
print(table_html_str)
```
### 3. Run by command line.
```bash
-$ rapid_table --img_path test_images/table.jpg
+$ rapid_table -v -img table.jpg
```
### 4. Result.
diff --git a/python/rapid_structure/docs/README_Layout.md b/python/rapid_structure/docs/README_Layout.md
index 0af0662..008c429 100644
--- a/python/rapid_structure/docs/README_Layout.md
+++ b/python/rapid_structure/docs/README_Layout.md
@@ -40,22 +40,22 @@
3. 终端运行
- 用法:
- ```bash
- $ rapid_layout -h
- usage: rapid_layout [-h] [-v] -img IMG_PATH [-m MODEL_PATH]
+ ```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.
- ```
+ 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
- ```
+ ```bash
+ $ rapid_layout -v -img test_images/layout.png
+ ```
4. 结果
- 返回结果
diff --git a/python/rapid_structure/docs/README_Table.md b/python/rapid_structure/docs/README_Table.md
index 1e1f97f..8a3b0e9 100644
--- a/python/rapid_structure/docs/README_Table.md
+++ b/python/rapid_structure/docs/README_Table.md
@@ -28,17 +28,33 @@
import cv2
from rapid_table import RapidTable
- rapid_table = RapidTable()
+ # RapidTable类提供model_path参数,可以自行指定上述2个模型,默认是en_ppstructure_mobile_v2_SLANet.onnx
+ # table_engine = RapidTable(model_path='ch_ppstructure_mobile_v2_SLANet.onnx')
+ table_engine = RapidTable()
img = cv2.imread('test_images/table.jpg')
- table_html_str, _ = rapid_table(img)
+ table_html_str, _ = table_engine(img)
print(table_html_str)
```
3. 终端运行
- ```bash
- # 假设在rapid_structure目录下
- $ rapid_table --img_path test_images/table.jpg
- ```
+ - 用法:
+ ```bash
+ $ rapid_table -h
+ usage: rapid_table [-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_table -v -img test_images/table.jpg
+ ```
+
4. 结果
- 返回结果
```html
diff --git a/python/rapid_structure/rapid_layout/rapid_layout.py b/python/rapid_structure/rapid_layout/rapid_layout.py
index 9c6319e..41fc71f 100644
--- a/python/rapid_structure/rapid_layout/rapid_layout.py
+++ b/python/rapid_structure/rapid_layout/rapid_layout.py
@@ -83,10 +83,10 @@ def main():
help='The model path used for inference.')
args = parser.parse_args()
- layout_engine = RapidLayout(args.model_path)
+ rapid_layout = RapidLayout(args.model_path)
img = cv2.imread(args.img_path)
- layout_res, elapse = layout_engine(img)
+ layout_res, elapse = rapid_layout(img)
print(layout_res)
if args.vis:
diff --git a/python/rapid_structure/rapid_table/rapid_table.py b/python/rapid_structure/rapid_table/rapid_table.py
index 895cf41..27027e6 100644
--- a/python/rapid_structure/rapid_table/rapid_table.py
+++ b/python/rapid_structure/rapid_table/rapid_table.py
@@ -1,10 +1,12 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
-import time
+import argparse
import copy
+import time
from pathlib import Path
+import cv2
import numpy as np
from rapidocr_onnxruntime import RapidOCR
@@ -54,24 +56,40 @@ class RapidTable():
return dt_boxes, rec_res
-def main():
- import argparse
- import cv2
+def vis_table(table_res: str, save_path: str) -> None:
+ style_res = '''<style>td {border-left: 1px solid;border-bottom:1px solid;}
+ table, th {border-top:1px solid;font-size: 10px;
+ border-collapse: collapse;border-right: 1px solid;}
+ </style>'''
+ prefix_table, suffix_table = table_res.split('<body>')
+ new_table_res = f'{prefix_table}{style_res}<body>{suffix_table}'
+ with open(save_path, 'w', encoding='utf-8') as f:
+ f.write(new_table_res)
+ print(f'The infer result has saved in {save_path}')
+
+def main() -> None:
parser = argparse.ArgumentParser()
- parser.add_argument('--img_path', type=str, required=True)
- parser.add_argument('--model_path', type=str,
- default=str(root_dir / 'models' / 'en_ppstructure_mobile_v2_SLANet.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' / 'en_ppstructure_mobile_v2_SLANet.onnx'),
+ help='The model path used for inference.')
args = parser.parse_args()
rapid_table = RapidTable(args.model_path)
img = cv2.imread(args.img_path)
-
table_html_str, elapse = rapid_table(img)
-
print(table_html_str)
+ if args.vis:
+ img_path = Path(args.img_path)
+ save_path = img_path.resolve().parent / f'vis_{img_path.stem}.html'
+ vis_table(table_html_str, str(save_path))
+
if __name__ == '__main__':
main()
diff --git a/python/rapid_structure/setup_layout.py b/python/rapid_structure/setup_layout.py
index cb117cf..c6e1ca2 100644
--- a/python/rapid_structure/setup_layout.py
+++ b/python/rapid_structure/setup_layout.py
@@ -1,7 +1,9 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
+import re
import subprocess
+import sys
from pathlib import Path
import setuptools
@@ -12,8 +14,7 @@ def get_latest_version(package_name):
capture_output=True)
output = output.stdout.decode('utf-8')
if output:
- output = list(filter(lambda x: len(x) > 0, output.split('\n')))
- return output[0].strip().split(' ')[-1][1:-1]
+ return extract_version(output)
return None
@@ -35,10 +36,26 @@ def get_readme():
return readme
+def extract_version(message: str) -> str:
+ pattern = r'\d+\.(?:\d+\.)*\d+'
+ matched_versions = re.findall(pattern, message)
+ if matched_versions:
+ return matched_versions[0]
+ return ''
+
+
MODULE_NAME = 'rapid_layout'
latest_version = get_latest_version(MODULE_NAME)
VERSION_NUM = version_add_one(latest_version)
+# 优先提取commit message中的语义化版本号,如无,则自动加1
+if len(sys.argv) > 2:
+ match_str = ' '.join(sys.argv[2:])
+ matched_versions = extract_version(match_str)
+ if matched_versions:
+ VERSION_NUM = matched_versions
+sys.argv = sys.argv[:2]
+
setuptools.setup(
name=MODULE_NAME,
version=VERSION_NUM,
diff --git a/python/rapid_structure/setup_table.py b/python/rapid_structure/setup_table.py
index 34eb257..50fa369 100644
--- a/python/rapid_structure/setup_table.py
+++ b/python/rapid_structure/setup_table.py
@@ -75,7 +75,7 @@ setuptools.setup(
f'{MODULE_NAME}.models',
f'{MODULE_NAME}.table_matcher',
f'{MODULE_NAME}.table_structure'],
- package_data={'': ['*.onnx']},
+ package_data={'': ['en_ppstructure_mobile_v2_SLANet.onnx']},
keywords=[
'ppstructure,table,rapidocr,rapid_table'
],