Skip to content

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

fix(rapidocr): fixed issue #381 #377 (#384)

SWHLd48f31e81586
diff --git a/python/rapidocr/cli.py b/python/rapidocr/cli.py
new file mode 100644
index 0000000..ca9a8a0
--- /dev/null
+++ b/python/rapidocr/cli.py
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+import shutil
+from pathlib import Path
+
+
+root_dir = Path(__file__).resolve().parent
+DEFAULT_CFG_PATH = root_dir / "config.yaml"
+
+
+def generate_cfg(args):
+ if args.save_cfg_file is None:
+ args.save_cfg_file = "./default_rapidocr.yaml"
+
+ shutil.copyfile(DEFAULT_CFG_PATH, args.save_cfg_file)
+ print(f"The config file has saved in {args.save_cfg_file}")
+
+
+def check_install(ocr_engine):
+ img_url = "https://github.com/RapidAI/RapidOCR/blob/a9bb7c1f44b6e00556ada90ac588f020d7637c4b/python/tests/test_files/ch_en_num.jpg?raw=true"
+ result = ocr_engine(img_url)
+
+ if result.txts is None or result.txts[0] != "正品促销":
+ raise ValueError("The installation is incorrect!")
+
+ print("Success! rapidocr is installed correctly!")
diff --git a/python/rapidocr/config.yaml b/python/rapidocr/config.yaml
index 9eb5868..8ab57ca 100644
--- a/python/rapidocr/config.yaml
+++ b/python/rapidocr/config.yaml
@@ -31,7 +31,7 @@ EngineConfig:
openvino:
inference_num_threads: -1
- paddlepaddle:
+ paddle:
cpu_math_library_num_threads: -1
use_cuda: false
gpu_id: 0
diff --git a/python/rapidocr/default_models.yaml b/python/rapidocr/default_models.yaml
index cfb4a0a..bb7b834 100644
--- a/python/rapidocr/default_models.yaml
+++ b/python/rapidocr/default_models.yaml
@@ -72,7 +72,7 @@ openvino:
cls:
ch_ppocr_mobile_v2.0_cls_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/cls/ch_ppocr_mobile_v2.0_cls_infer.onnx
-paddlepaddle:
+paddle:
PP-OCRv4:
det:
ch_PP-OCRv4_det_infer: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/det/ch_PP-OCRv4_det_infer
diff --git a/python/rapidocr/inference_engine/base.py b/python/rapidocr/inference_engine/base.py
index bb0a071..4326128 100644
--- a/python/rapidocr/inference_engine/base.py
+++ b/python/rapidocr/inference_engine/base.py
@@ -4,7 +4,7 @@
import abc
from enum import Enum
from pathlib import Path
-from typing import Union
+from typing import List, Union
import numpy as np
from omegaconf import DictConfig, OmegaConf
@@ -22,10 +22,21 @@ logger = Logger(logger_name=__name__).get_log()
class Engine(Enum):
ONNXRUNTIME = "onnxruntime"
OPENVINO = "openvino"
- PADDLE = "paddlepaddle"
+ PADDLE = "paddle"
TORCH = "torch"
+def get_installed_engine() -> List[str]:
+ installed_engine = []
+ for v in Engine.__members__:
+ engine_name = Engine[v].value
+ if import_package(engine_name) is None:
+ continue
+
+ installed_engine.append(engine_name)
+ return installed_engine
+
+
def get_engine(engine_name: str):
logger.info("Using engine_name: %s", engine_name)
@@ -49,7 +60,7 @@ def get_engine(engine_name: str):
if not import_package("paddle"):
raise ImportError("paddleopaddle is not installed")
- from .paddlepaddle import PaddleInferSession
+ from .paddle import PaddleInferSession
return PaddleInferSession
diff --git a/python/rapidocr/inference_engine/paddlepaddle.py b/python/rapidocr/inference_engine/paddle.py
similarity index 100%
rename from python/rapidocr/inference_engine/paddlepaddle.py
rename to python/rapidocr/inference_engine/paddle.py
diff --git a/python/rapidocr/main.py b/python/rapidocr/main.py
index b57ff20..7a06e57 100644
--- a/python/rapidocr/main.py
+++ b/python/rapidocr/main.py
@@ -3,7 +3,6 @@
# @Contact: [email protected]
import argparse
import copy
-import shutil
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union
@@ -14,6 +13,7 @@ from .cal_rec_boxes import CalRecBoxes
from .ch_ppocr_cls import TextClassifier, TextClsOutput
from .ch_ppocr_det import TextDetector, TextDetOutput
from .ch_ppocr_rec import TextRecInput, TextRecognizer, TextRecOutput
+from .cli import check_install, generate_cfg
from .inference_engine.base import get_engine_name
from .utils import (
LoadImage,
@@ -320,28 +320,6 @@ class RapidOCR:
ocr_res.scores = tuple(filter_scores)
return ocr_res
- def export_config(self, save_path: Union[Path, str]) -> None:
- with open(save_path, "w", encoding="utf-8") as f:
- ParseParams.save(self.config, f)
-
-
-def generate_cfg(args):
- if args.save_cfg_file is None:
- args.save_cfg_file = "./default_rapidocr.yaml"
-
- shutil.copyfile(DEFAULT_CFG_PATH, args.save_cfg_file)
- print(f"The config file has saved in {args.save_cfg_file}")
-
-
-def check_install():
- img_url = "https://github.com/RapidAI/RapidOCR/blob/a9bb7c1f44b6e00556ada90ac588f020d7637c4b/python/tests/test_files/ch_en_num.jpg?raw=true"
- result = RapidOCR()(img_url)
-
- if result.txts is None or result.txts[0] != "正品促销":
- raise ValueError("The installation is incorrect!")
-
- print("Success! rapidocr is installed correctly!")
-
def parse_args(arg_list: Optional[List[str]] = None):
parser = argparse.ArgumentParser()
@@ -374,16 +352,16 @@ def main(arg_list: Optional[List[str]] = None):
generate_cfg(args)
return
- if args.command == "check":
- check_install()
- return
-
params = {
"Global.text_score": args.text_score,
"Global.return_word_box": args.return_word_box,
}
ocr_engine = RapidOCR(params=params)
+ if args.command == "check":
+ check_install(ocr_engine)
+ return
+
if args.img_path is None:
raise ValueError("Please input the image path")
diff --git a/python/requirements.txt b/python/requirements.txt
index 2e7ef61..da23ea3 100644
--- a/python/requirements.txt
+++ b/python/requirements.txt
@@ -5,6 +5,6 @@ six>=1.15.0
Shapely>=1.7.1,!=2.0.4 # python3.12 2.0.4 bug
PyYAML
Pillow
-onnxruntime>=1.7.0
tqdm
-omegaconf
\ No newline at end of file
+omegaconf
+requests
diff --git a/python/setup.py b/python/setup.py
index fd70ed8..8ef232c 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -73,8 +73,9 @@ setuptools.setup(
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
+ "Programming Language :: Python :: 3.x",
],
- python_requires=">=3.6,<3.13",
+ python_requires=">=3.6,<4",
entry_points={
"console_scripts": [f"{MODULE_NAME}={MODULE_NAME}.main:main"],
},
diff --git a/python/tests/test_main.py b/python/tests/test_main.py
index 0e0a2f1..47b922e 100644
--- a/python/tests/test_main.py
+++ b/python/tests/test_main.py
@@ -94,6 +94,26 @@ def test_cli(capsys, cmd, gt):
assert gt in output
[email protected]("cmd", [f"config --save_cfg_file {tests_dir}/config.yaml"])
+def test_cli_config(capsys, cmd):
+ main(shlex.split(cmd))
+ output = capsys.readouterr().out.strip()
+
+ assert "The config file has saved in" in output
+
+ cfg_yaml_path = tests_dir / "config.yaml"
+ assert cfg_yaml_path.exists()
+ cfg_yaml_path.unlink()
+
+
[email protected]("cmd", ["check"])
+def test_cli_check(capsys, cmd):
+ main(shlex.split(cmd))
+ output = capsys.readouterr().out.strip()
+
+ assert "Success! rapidocr is installed correctly!" in output
+
+
@pytest.mark.parametrize(
"cmd,img_name",
[