Skip to content

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

refactor: optimize model_root_dir logic

SWHLd48c4cf3121c
diff --git a/python/rapidocr/inference_engine/mnn/main.py b/python/rapidocr/inference_engine/mnn/main.py
index b1d3e8c..249acef 100644
--- a/python/rapidocr/inference_engine/mnn/main.py
+++ b/python/rapidocr/inference_engine/mnn/main.py
@@ -19,7 +19,12 @@ class MNNInferSession(InferSession):
model_path = cfg.get("model_path", None)
if model_path is None:
# Check model_root_dir only if model_path is None
- model_root_dir = Path(cfg.get("model_root_dir"))
+ model_root_dir = cfg.get("model_root_dir", None)
+ if model_root_dir is None:
+ raise ValueError(
+ "Either model_path or model_root_dir must be provided in the configuration."
+ )
+ model_root_dir = Path(model_root_dir)
if not model_root_dir.exists():
raise FileNotFoundError(
f"model_root_dir {model_root_dir} does not exist"
diff --git a/python/rapidocr/inference_engine/onnxruntime/main.py b/python/rapidocr/inference_engine/onnxruntime/main.py
index 7222a47..7fdfabd 100644
--- a/python/rapidocr/inference_engine/onnxruntime/main.py
+++ b/python/rapidocr/inference_engine/onnxruntime/main.py
@@ -32,7 +32,12 @@ class OrtInferSession(InferSession):
model_path = cfg.get("model_path", None)
if model_path is None:
# Check model_root_dir only if model_path is None
- model_root_dir = Path(cfg.get("model_root_dir"))
+ model_root_dir = cfg.get("model_root_dir", None)
+ if model_root_dir is None:
+ raise ValueError(
+ "Either model_path or model_root_dir must be provided in the configuration."
+ )
+ model_root_dir = Path(model_root_dir)
if not model_root_dir.exists():
raise FileNotFoundError(
f"model_root_dir {model_root_dir} does not exist"
diff --git a/python/rapidocr/inference_engine/openvino/main.py b/python/rapidocr/inference_engine/openvino/main.py
index c0039d9..5ec6246 100644
--- a/python/rapidocr/inference_engine/openvino/main.py
+++ b/python/rapidocr/inference_engine/openvino/main.py
@@ -26,7 +26,12 @@ class OpenVINOInferSession(InferSession):
model_path = cfg.get("model_path", None)
if model_path is None:
# Check model_root_dir only if model_path is None
- model_root_dir = Path(cfg.get("model_root_dir"))
+ model_root_dir = cfg.get("model_root_dir", None)
+ if model_root_dir is None:
+ raise ValueError(
+ "Either model_path or model_root_dir must be provided in the configuration."
+ )
+ model_root_dir = Path(model_root_dir)
if not model_root_dir.exists():
raise FileNotFoundError(
f"model_root_dir {model_root_dir} does not exist"
diff --git a/python/rapidocr/inference_engine/tensorrt/main.py b/python/rapidocr/inference_engine/tensorrt/main.py
index 10c7637..2019e99 100644
--- a/python/rapidocr/inference_engine/tensorrt/main.py
+++ b/python/rapidocr/inference_engine/tensorrt/main.py
@@ -284,7 +284,18 @@ class TRTInferSession(InferSession):
if cache_dir is None:
# Check model_root_dir only if cache_dir is None
if self.model_root_dir is None:
- self.model_root_dir = Path(cfg.get("model_root_dir"))
+ model_root_dir = cfg.get("model_root_dir", None)
+ if model_root_dir is None:
+ raise ValueError(
+ "Either model_path or model_root_dir must be provided in the configuration."
+ )
+ model_root_dir = Path(model_root_dir)
+ if not model_root_dir.exists():
+ raise FileNotFoundError(
+ f"model_root_dir {model_root_dir} does not exist"
+ )
+
+ self.model_root_dir = model_root_dir
if not self.model_root_dir.exists():
raise FileNotFoundError(
f"model_root_dir {self.model_root_dir} does not exist"