Skip to content

πŸ“„ Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

Add to support visisual result under CLI

SWHL9551d8bb34d1
diff --git a/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml b/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml
index f8b0c95..5ca4f68 100644
--- a/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml
+++ b/.github/workflows/gen_whl_to_pypi_rapidocr_ort.yml
@@ -86,7 +86,7 @@ jobs:
mv rapidocr_onnxruntime rapidocr_onnxruntime_t
mv rapidocr_onnxruntime_t rapidocr_onnxruntime
cd rapidocr_onnxruntime
- echo "from .rapidocr_onnxruntime.main import RapidOCR" > __init__.py
+ echo "from .rapidocr_onnxruntime.main import RapidOCR, VisRes" > __init__.py
cd ..
python -m pip install --upgrade pip
diff --git a/.github/workflows/gen_whl_to_pypi_rapidocr_vino.yml b/.github/workflows/gen_whl_to_pypi_rapidocr_vino.yml
index 54b39e7..5dec3d8 100644
--- a/.github/workflows/gen_whl_to_pypi_rapidocr_vino.yml
+++ b/.github/workflows/gen_whl_to_pypi_rapidocr_vino.yml
@@ -86,7 +86,7 @@ jobs:
mv rapidocr_openvino rapidocr_openvino_t
mv rapidocr_openvino_t rapidocr_openvino
cd rapidocr_openvino
- echo "from .rapidocr_openvino.main import RapidOCR" > __init__.py
+ echo "from .rapidocr_openvino.main import RapidOCR, VisRes" > __init__.py
cd ..
python -m pip install --upgrade pip
diff --git a/python/demo.py b/python/demo.py
index b791ce9..0792344 100644
--- a/python/demo.py
+++ b/python/demo.py
@@ -8,17 +8,17 @@ from rapidocr_onnxruntime import RapidOCR, VisRes
# from rapidocr_openvino import RapidOCR, VisRes
-rapid_ocr = RapidOCR()
+engine = RapidOCR()
vis = VisRes(font_path="resources/fonts/FZYTK.TTF")
image_path = "tests/test_files/ch_en_num.jpg"
with open(image_path, "rb") as f:
img = f.read()
-result, elapse_list = rapid_ocr(img)
+result, elapse_list = engine(img)
print(result)
print(elapse_list)
boxes, txts, scores = list(zip(*result))
-res = vis(img, boxes, txts, scores)
-cv2.imwrite("vis.png", res)
+vis_img = vis(img, boxes, txts, scores)
+cv2.imwrite("vis.png", vis_img)
diff --git a/python/rapidocr_onnxruntime/main.py b/python/rapidocr_onnxruntime/main.py
index 46e06b7..e5b59eb 100644
--- a/python/rapidocr_onnxruntime/main.py
+++ b/python/rapidocr_onnxruntime/main.py
@@ -11,7 +11,14 @@ import numpy as np
from .ch_ppocr_v2_cls import TextClassifier
from .ch_ppocr_v3_det import TextDetector
from .ch_ppocr_v3_rec import TextRecognizer
-from .utils import LoadImage, UpdateParameters, concat_model_path, init_args, read_yaml
+from .utils import (
+ LoadImage,
+ UpdateParameters,
+ VisRes,
+ concat_model_path,
+ init_args,
+ read_yaml,
+)
root_dir = Path(__file__).resolve().parent
@@ -257,9 +264,27 @@ def main():
args.img_path, use_det=use_det, use_cls=use_cls, use_rec=use_rec
)
print(result)
+
if args.print_cost:
print(elapse_list)
+ if args.vis_res and args.vis_font_path:
+ vis = VisRes(font_path=args.vis_font_path)
+ Path(args.vis_save_path).mkdir(parents=True, exist_ok=True)
+
+ save_path = Path(args.vis_save_path) / f"{Path(args.img_path).stem}_vis.png"
+
+ if use_det and not use_cls and not use_rec:
+ boxes, *_ = list(zip(*result))
+ vis_img = vis(args.img_path, boxes, None, None)
+ cv2.imwrite(str(save_path), vis_img)
+ print(f"The vis result has saved in {save_path}")
+ elif use_det and use_rec:
+ boxes, txts, scores = list(zip(*result))
+ vis_img = vis(args.img_path, boxes, txts, scores)
+ cv2.imwrite(str(save_path), vis_img)
+ print(f"The vis result has saved in {save_path}")
+
if __name__ == "__main__":
main()
diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.py
index f4452ce..b762e45 100644
--- a/python/rapidocr_onnxruntime/utils.py
+++ b/python/rapidocr_onnxruntime/utils.py
@@ -265,6 +265,21 @@ def init_args():
rec_group.add_argument("--rec_img_shape", type=list, default=[3, 48, 320])
rec_group.add_argument("--rec_batch_num", type=int, default=6)
+ vis_group = parser.add_argument_group(title="Visual Result")
+ vis_group.add_argument("-vis", "--vis_res", action="store_true", default=False)
+ vis_group.add_argument(
+ "--vis_font_path",
+ type=str,
+ default=None,
+ help="When -vis is True, the font_path must have value.",
+ )
+ vis_group.add_argument(
+ "--vis_save_path",
+ type=str,
+ default=".",
+ help="The directory of saving the vis image.",
+ )
+
args = parser.parse_args()
return args
@@ -352,7 +367,7 @@ class VisRes:
def __init__(
self, font_path: Optional[Union[str, Path]] = None, text_score: float = 0.5
):
- if font_path is None:
+ if font_path is None or not Path(font_path).exists():
raise FileNotFoundError(
f"The {font_path} does not exists! \n"
f"You could download the file in the https://drive.google.com/file/d/1evWVX38EFNwTq_n5gTFgnlv8tdaNcyIA/view?usp=sharing"
diff --git a/python/rapidocr_openvino/main.py b/python/rapidocr_openvino/main.py
index 46e06b7..e5b59eb 100644
--- a/python/rapidocr_openvino/main.py
+++ b/python/rapidocr_openvino/main.py
@@ -11,7 +11,14 @@ import numpy as np
from .ch_ppocr_v2_cls import TextClassifier
from .ch_ppocr_v3_det import TextDetector
from .ch_ppocr_v3_rec import TextRecognizer
-from .utils import LoadImage, UpdateParameters, concat_model_path, init_args, read_yaml
+from .utils import (
+ LoadImage,
+ UpdateParameters,
+ VisRes,
+ concat_model_path,
+ init_args,
+ read_yaml,
+)
root_dir = Path(__file__).resolve().parent
@@ -257,9 +264,27 @@ def main():
args.img_path, use_det=use_det, use_cls=use_cls, use_rec=use_rec
)
print(result)
+
if args.print_cost:
print(elapse_list)
+ if args.vis_res and args.vis_font_path:
+ vis = VisRes(font_path=args.vis_font_path)
+ Path(args.vis_save_path).mkdir(parents=True, exist_ok=True)
+
+ save_path = Path(args.vis_save_path) / f"{Path(args.img_path).stem}_vis.png"
+
+ if use_det and not use_cls and not use_rec:
+ boxes, *_ = list(zip(*result))
+ vis_img = vis(args.img_path, boxes, None, None)
+ cv2.imwrite(str(save_path), vis_img)
+ print(f"The vis result has saved in {save_path}")
+ elif use_det and use_rec:
+ boxes, txts, scores = list(zip(*result))
+ vis_img = vis(args.img_path, boxes, txts, scores)
+ cv2.imwrite(str(save_path), vis_img)
+ print(f"The vis result has saved in {save_path}")
+
if __name__ == "__main__":
main()
diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py
index 32aadbb..0131397 100644
--- a/python/rapidocr_openvino/utils.py
+++ b/python/rapidocr_openvino/utils.py
@@ -194,6 +194,21 @@ def init_args():
rec_group.add_argument("--rec_img_shape", type=list, default=[3, 48, 320])
rec_group.add_argument("--rec_batch_num", type=int, default=6)
+ vis_group = parser.add_argument_group(title="Visual Result")
+ vis_group.add_argument("-vis", "--vis_res", action="store_true", default=False)
+ vis_group.add_argument(
+ "--vis_font_path",
+ type=str,
+ default=None,
+ help="When -vis is True, the font_path must have value.",
+ )
+ vis_group.add_argument(
+ "--vis_save_path",
+ type=str,
+ default=".",
+ help="The directory of saving the vis image.",
+ )
+
args = parser.parse_args()
return args
@@ -281,7 +296,7 @@ class VisRes:
def __init__(
self, font_path: Optional[Union[str, Path]] = None, text_score: float = 0.5
):
- if font_path is None:
+ if font_path is None or not Path(font_path).exists():
raise FileNotFoundError(
f"The {font_path} does not exists! \n"
f"You could download the file in the https://drive.google.com/file/d/1evWVX38EFNwTq_n5gTFgnlv8tdaNcyIA/view?usp=sharing"
diff --git a/python/setup_onnxruntime.py b/python/setup_onnxruntime.py
index c16b0ea..f861b74 100644
--- a/python/setup_onnxruntime.py
+++ b/python/setup_onnxruntime.py
@@ -38,7 +38,7 @@ if len(sys.argv) > 2:
sys.argv = sys.argv[:2]
project_urls = {
- "Documentation": "https://rapidai.github.io/RapidOCRDocs/docs/install_usage/rapidocr/rapidocr_onnxruntime/",
+ "Documentation": "https://rapidai.github.io/RapidOCRDocs/docs/install_usage/rapidocr/usage/",
"Changelog": "https://rapidai.github.io/RapidOCRDocs/docs/changelog/rapidocr/",
}
diff --git a/python/setup_openvino.py b/python/setup_openvino.py
index 8fba832..afa4b17 100644
--- a/python/setup_openvino.py
+++ b/python/setup_openvino.py
@@ -38,7 +38,7 @@ if len(sys.argv) > 2:
sys.argv = sys.argv[:2]
project_urls = {
- "Documentation": "https://rapidai.github.io/RapidOCRDocs/docs/install_usage/rapidocr/rapidocr_openvino/",
+ "Documentation": "https://rapidai.github.io/RapidOCRDocs/docs/install_usage/rapidocr/usage/",
"Changelog": "https://rapidai.github.io/RapidOCRDocs/docs/changelog/rapidocr/",
}