📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
chore: update files
SWHL844b2e6cbff8
diff --git a/python/rapidocr/ch_ppocr_cls/main.py b/python/rapidocr/ch_ppocr_cls/main.pyindex 7b5b207..851239d 100644--- a/python/rapidocr/ch_ppocr_cls/main.py+++ b/python/rapidocr/ch_ppocr_cls/main.py@@ -19,7 +19,7 @@ from typing import Any, Dict, List, Unionimport cv2import numpy as np-from rapidocr.utils import OrtInferSession+from rapidocr.inference_engine import OrtInferSessionfrom .utils import ClsPostProcess, TextClsOutputdiff --git a/python/rapidocr/ch_ppocr_det/main.py b/python/rapidocr/ch_ppocr_det/main.pyindex a6cce33..63e633d 100644--- a/python/rapidocr/ch_ppocr_det/main.py+++ b/python/rapidocr/ch_ppocr_det/main.py@@ -19,7 +19,7 @@ from typing import Any, Dict, Listimport numpy as np-from rapidocr.utils import OrtInferSession+from rapidocr.inference_engine import OrtInferSessionfrom .utils import DBPostProcess, DetPreProcess, TextDetOutputdiff --git a/python/rapidocr/ch_ppocr_rec/main.py b/python/rapidocr/ch_ppocr_rec/main.pyindex 44ea52b..26e7a81 100644--- a/python/rapidocr/ch_ppocr_rec/main.py+++ b/python/rapidocr/ch_ppocr_rec/main.py@@ -18,7 +18,7 @@ from typing import Any, Dictimport cv2import numpy as np-from rapidocr.utils import OrtInferSession+from rapidocr.inference_engine import OrtInferSessionfrom .utils import CTCLabelDecode, TextRecInput, TextRecOutputdiff --git a/python/rapidocr/inference_engine/__init__.py b/python/rapidocr/inference_engine/__init__.pyindex 0ecdd4f..9b75723 100644--- a/python/rapidocr/inference_engine/__init__.py+++ b/python/rapidocr/inference_engine/__init__.py@@ -1,3 +1,4 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]+from .onnxruntime import OrtInferSessiondiff --git a/python/rapidocr/inference_engine/base.py b/python/rapidocr/inference_engine/base.pynew file mode 100644index 0000000..1f0f526--- /dev/null+++ b/python/rapidocr/inference_engine/base.py@@ -0,0 +1,30 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+import abc+from pathlib import Path+from typing import Union++import numpy as np+++class InferSession(abc.ABC):+ @abc.abstractmethod+ def __init__(self, config):+ pass++ @abc.abstractmethod+ def __call__(self, input_content: np.ndarray) -> np.ndarray:+ pass++ @staticmethod+ def _verify_model(model_path: Union[str, Path, None]):+ if model_path is None:+ raise ValueError("model_path is None!")++ model_path = Path(model_path)+ if not model_path.exists():+ raise FileNotFoundError(f"{model_path} does not exists.")++ if not model_path.is_file():+ raise FileExistsError(f"{model_path} is not a file.")diff --git a/python/rapidocr/inference_engine/onnxruntime.py b/python/rapidocr/inference_engine/onnxruntime.pyindex 09f688d..45967a0 100644--- a/python/rapidocr/inference_engine/onnxruntime.py+++ b/python/rapidocr/inference_engine/onnxruntime.py@@ -5,8 +5,7 @@ import osimport platformimport tracebackfrom enum import Enum-from pathlib import Path-from typing import Any, Dict, List, Tuple, Union+from typing import Any, Dict, List, Tupleimport numpy as npfrom onnxruntime import (@@ -18,6 +17,7 @@ from onnxruntime import ()from ..utils.logger import get_logger+from .base import InferSessionclass EP(Enum):@@ -26,7 +26,7 @@ class EP(Enum):DIRECTML_EP = "DmlExecutionProvider"-class OrtInferSession:+class OrtInferSession(InferSession):def __init__(self, config: Dict[str, Any]):self.logger = get_logger("OrtInferSession")@@ -214,18 +214,6 @@ class OrtInferSession:return Truereturn False- @staticmethod- def _verify_model(model_path: Union[str, Path, None]):- if model_path is None:- raise ValueError("model_path is None!")-- model_path = Path(model_path)- if not model_path.exists():- raise FileNotFoundError(f"{model_path} does not exists.")-- if not model_path.is_file():- raise FileExistsError(f"{model_path} is not a file.")-class ONNXRuntimeError(Exception):passdiff --git a/python/rapidocr/inference_engine/torch.py b/python/rapidocr/inference_engine/torch.pyindex 0ecdd4f..d59202a 100644--- a/python/rapidocr/inference_engine/torch.py+++ b/python/rapidocr/inference_engine/torch.py@@ -1,3 +1,65 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]+from pathlib import Path+from typing import Dict, Optional, Union++import numpy as np+import torch+import yaml++root_dir = Path(__file__).resolve().parent.parent+DEFAULT_CFG_PATH = root_dir / "arch_config.yaml"+++def read_yaml(yaml_path: Union[str, Path]) -> Dict[str, Dict]:+ with open(yaml_path, "rb") as f:+ data = yaml.load(f, Loader=yaml.Loader)+ return data+++from rapidocr_torch.modeling.architectures.base_model import BaseModel++from ..utils.logger import get_logger+++class TorchInferSession:+ def __init__(self, config, mode: Optional[str] = None) -> None:+ all_arch_config = read_yaml(DEFAULT_CFG_PATH)++ self.logger = get_logger("TorchInferSession")+ self.mode = mode+ model_path = Path(config["model_path"])+ self._verify_model(model_path)+ file_name = model_path.stem+ if file_name not in all_arch_config:+ raise ValueError(f"architecture {file_name} is not in config.yaml")+ arch_config = all_arch_config[file_name]+ self.predictor = BaseModel(arch_config)+ self.predictor.load_state_dict(torch.load(model_path, weights_only=True))+ self.predictor.eval()+ self.use_gpu = False+ if config["use_cuda"]:+ self.predictor.cuda()+ self.use_gpu = True++ def __call__(self, img: np.ndarray):+ with torch.no_grad():+ inp = torch.from_numpy(img)+ if self.use_gpu:+ inp = inp.cuda()+ # 适配跟onnx对齐取值逻辑+ outputs = self.predictor(inp).unsqueeze(0)+ return outputs.cpu().numpy()++ @staticmethod+ def _verify_model(model_path):+ model_path = Path(model_path)+ if not model_path.exists():+ raise FileNotFoundError(f"{model_path} does not exists.")+ if not model_path.is_file():+ raise FileExistsError(f"{model_path} is not a file.")+++class TorchInferError(Exception):+ passdiff --git a/python/rapidocr/main.py b/python/rapidocr/main.pyindex 43f605b..c39c694 100644--- a/python/rapidocr/main.py+++ b/python/rapidocr/main.py@@ -44,8 +44,8 @@ class RapidOCR:updater = UpdateParameters()config = updater(config, **kwargs)- det_lang, rec_lang = parse_lang(config.Global.lang)# 根据选定的语言加载对应的模型+ det_lang, rec_lang = parse_lang(config.Global.lang)self.print_verbose = config.Global.print_verboseself.text_score = config.Global.text_scorediff --git a/python/rapidocr/utils/__init__.py b/python/rapidocr/utils/__init__.pyindex dea0484..7535b42 100644--- a/python/rapidocr/utils/__init__.py+++ b/python/rapidocr/utils/__init__.py@@ -1,7 +1,6 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]-from ..inference_engine.onnxruntime import OrtInferSessionfrom .load_image import LoadImage, LoadImageErrorfrom .logger import get_loggerfrom .parse_parameters import UpdateParameters, init_args, parse_lang, update_model_path