Skip to content

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

chore: update files

SWHL8ca484987ef8
diff --git a/python/demo.py b/python/demo.py
index 4a84af0..4981635 100644
--- a/python/demo.py
+++ b/python/demo.py
@@ -18,7 +18,7 @@ from rapidocr import RapidOCR, VisRes
# engine = engine = RapidOCR(
# params={"Global.with_onnx": True, "EngineConfig.onnxruntime.use_cuda": True}
# )
-engine = RapidOCR(params={"Global.lang": "en"})
+engine = RapidOCR(params={"Global.lang": "latin", "Global.with_openvino": True})
vis = VisRes()
image_path = "tests/test_files/ch_en_num.jpg"
diff --git a/python/rapidocr/config.yaml b/python/rapidocr/config.yaml
index 8eaef5e..e3f0a01 100644
--- a/python/rapidocr/config.yaml
+++ b/python/rapidocr/config.yaml
@@ -38,8 +38,8 @@ EngineConfig:
use_cuda: false
Det:
- model_path: models/ch_PP-OCRv4_det_infer.onnx
- model_dir: models/ch_PP-OCRv4_det_infer
+ model_path: null
+ model_dir: null
limit_side_len: 736
limit_type: min
@@ -54,8 +54,8 @@ Det:
score_mode: fast
Cls:
- model_path: models/ch_ppocr_mobile_v2.0_cls_infer.onnx
- model_dir: models/ch_ppocr_mobile_v2_cls_infer
+ model_path: null
+ model_dir: null
cls_image_shape: [3, 48, 192]
cls_batch_num: 6
@@ -63,9 +63,9 @@ Cls:
label_list: ['0', '180']
Rec:
- model_path: models/ch_PP-OCRv4_rec_infer.onnx
- model_dir: models/ch_PP-OCRv4_rec_infer
+ model_path: null
+ model_dir: null
- rec_keys_path: models/ppocr_keys_v1.txt
+ rec_keys_path: null
rec_img_shape: [3, 48, 320]
rec_batch_num: 6
diff --git a/python/rapidocr/inference_engine/MODEL_URL.yaml b/python/rapidocr/default_models.yaml
similarity index 100%
rename from python/rapidocr/inference_engine/MODEL_URL.yaml
rename to python/rapidocr/default_models.yaml
diff --git a/python/rapidocr/inference_engine/base.py b/python/rapidocr/inference_engine/base.py
index 82bfe6a..2dd79b3 100644
--- a/python/rapidocr/inference_engine/base.py
+++ b/python/rapidocr/inference_engine/base.py
@@ -13,8 +13,8 @@ from tqdm import tqdm
from ..utils.logger import Logger
-cur_dir = Path(__file__).resolve().parent
-MODEL_URL_PATH = cur_dir / "MODEL_URL.yaml"
+cur_dir = Path(__file__).resolve().parent.parent
+MODEL_URL_PATH = cur_dir / "default_models.yaml"
logger = Logger(logger_name=__name__).get_log()
@@ -76,7 +76,7 @@ def get_engine_name(config: DictConfig) -> str:
class InferSession(abc.ABC):
model_info = OmegaConf.load(MODEL_URL_PATH)
- DEFAULT_MODE_PATH = cur_dir.parent / "models"
+ DEFAULT_MODE_PATH = cur_dir / "models"
logger = Logger(logger_name=__name__).get_log()
@abc.abstractmethod
@@ -88,18 +88,16 @@ class InferSession(abc.ABC):
pass
@staticmethod
- def _verify_model(model_path: Union[str, Path, None]) -> bool:
+ def _verify_model(model_path: Union[str, Path, None]):
if model_path is None:
- return False
+ raise ValueError("model_path is None!")
model_path = Path(model_path)
if not model_path.exists():
- return False
+ raise FileNotFoundError(f"{model_path} does not exists.")
if not model_path.is_file():
- return False
-
- return True
+ raise FileExistsError(f"{model_path} is not a file.")
@abc.abstractmethod
def have_key(self, key: str = "character") -> bool:
@@ -107,7 +105,12 @@ class InferSession(abc.ABC):
@classmethod
def download_file(cls, url: str, save_path: Union[str, Path]):
+ if Path(save_path).exists():
+ cls.logger.info("Model already exists in %s", save_path)
+ return
+
cls.logger.info("Downloading model from %s to %s", url, save_path)
+
response = requests.get(url, stream=True, timeout=60)
status_code = response.status_code
diff --git a/python/rapidocr/inference_engine/onnxruntime.py b/python/rapidocr/inference_engine/onnxruntime.py
index d979133..433c70b 100644
--- a/python/rapidocr/inference_engine/onnxruntime.py
+++ b/python/rapidocr/inference_engine/onnxruntime.py
@@ -32,16 +32,16 @@ class OrtInferSession(InferSession):
self.logger = Logger(logger_name=__name__).get_log()
model_path = config.get("model_path", None)
- if not self._verify_model(model_path):
- self.logger.warning(
- "Model path is invalid, try to download the default model."
- )
+ if model_path is None:
+ # 说明用户没有指定自己模型,使用默认模型
default_model_url = self.get_model_url(
config.engine_name, config.task_type, config.lang
)
model_path = self.DEFAULT_MODE_PATH / Path(default_model_url).name
self.download_file(default_model_url, model_path)
+ self._verify_model(model_path)
+
self.cfg_use_cuda = config.engine_cfg.get("use_cuda", None)
self.cfg_use_dml = config.engine_cfg.get("use_dml", None)
diff --git a/python/rapidocr/inference_engine/openvino.py b/python/rapidocr/inference_engine/openvino.py
index eaa8bf0..c26b976 100644
--- a/python/rapidocr/inference_engine/openvino.py
+++ b/python/rapidocr/inference_engine/openvino.py
@@ -3,6 +3,7 @@
# @Contact: [email protected]
import os
import traceback
+from pathlib import Path
import numpy as np
from omegaconf import DictConfig
@@ -17,8 +18,17 @@ class OpenVINOInferSession(InferSession):
core = Core()
- self._verify_model(config["model_path"])
- model_onnx = core.read_model(config["model_path"])
+ model_path = config.get("model_path", None)
+ if model_path is None:
+ # 说明用户没有指定自己模型,使用默认模型
+ default_model_url = self.get_model_url(
+ config.engine_name, config.task_type, config.lang
+ )
+ model_path = self.DEFAULT_MODE_PATH / Path(default_model_url).name
+ self.download_file(default_model_url, model_path)
+
+ self._verify_model(model_path)
+ model_onnx = core.read_model(model_path)
cpu_nums = os.cpu_count()
infer_num_threads = config.get("inference_num_threads", -1)
diff --git a/python/rapidocr/main.py b/python/rapidocr/main.py
index 0dea8c0..45e440a 100644
--- a/python/rapidocr/main.py
+++ b/python/rapidocr/main.py
@@ -38,8 +38,8 @@ class RapidOCR:
config = ParseParams.load(config_path)
else:
config = ParseParams.load(DEFAULT_CFG_PATH)
- config = ParseParams.update_model_path(config)
- config = ParseParams.update_dict_path(config)
+ # config = ParseParams.update_model_path(config)
+ # config = ParseParams.update_dict_path(config)
if params:
config = ParseParams.update_batch(config, params)