Skip to content

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

feat: adapt PR #451

SWHL41d5824e1378
diff --git a/docs/README_zh.md b/docs/README_zh.md
index d05d45d..b334203 100644
--- a/docs/README_zh.md
+++ b/docs/README_zh.md
@@ -32,7 +32,7 @@
### 简介
-💖目前,我们自豪地推出了运行速度最为迅猛、兼容性最为广泛的多平台多语言OCR工具,它完全开源免费,并支持离线环境下的快速部署。其核心亮点在于采用ONNXRuntime作为推理引擎,相比传统的PaddlePaddle推理引擎,速度实现了4至5倍的提升,同时彻底解决了内存泄露问题,确保了高效稳定的运行。
+💖目前,我们自豪地推出了运行速度最为迅猛、兼容性最为广泛的多平台多语言OCR工具,它完全开源免费,并支持离线环境下的快速部署。
🦜 **支持语言概览:** 默认支持中文与英文识别,对于其他语言的识别需求,我们提供了便捷的自助转换方案。具体转换指南,请参见[这里](https://rapidai.github.io/RapidOCRDocs/blog/2022/09/28/%E6%94%AF%E6%8C%81%E8%AF%86%E5%88%AB%E8%AF%AD%E8%A8%80/)。
diff --git a/python/demo.py b/python/demo.py
index 35730fc..cc8808c 100644
--- a/python/demo.py
+++ b/python/demo.py
@@ -1,15 +1,9 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
-from rapidocr import EngineType, ModelType, OCRVersion, RapidOCR
+from rapidocr import RapidOCR
-engine = RapidOCR(
- params={
- "Rec.ocr_version": OCRVersion.PPOCRV5,
- "Rec.engine_type": EngineType.PADDLE,
- "Rec.model_type": ModelType.MOBILE,
- }
-)
+engine = RapidOCR()
img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"
result = engine(img_url)
diff --git a/python/rapidocr/config.yaml b/python/rapidocr/config.yaml
index 29065df..756e9f9 100644
--- a/python/rapidocr/config.yaml
+++ b/python/rapidocr/config.yaml
@@ -19,9 +19,28 @@ EngineConfig:
intra_op_num_threads: -1
inter_op_num_threads: -1
enable_cpu_mem_arena: false
+
+ cpu_ep_cfg:
+ arena_extend_strategy: "kSameAsRequested"
+
use_cuda: false
+ cuda_ep_cfg:
+ device_id: 0
+ arena_extend_strategy: "kNextPowerOfTwo"
+ cudnn_conv_algo_search: "EXHAUSTIVE"
+ do_copy_in_default_stream: true
+
use_dml: false
+ dm_ep_cfg: null
+
use_cann: false
+ cann_ep_cfg:
+ device_id: 0
+ arena_extend_strategy: "kNextPowerOfTwo"
+ npu_mem_limit: 20 * 1024 * 1024 * 1024
+ op_select_impl_mode: "high_performance"
+ optypelist_for_implmode: "Gelu"
+ enable_cann_graph: true
openvino:
inference_num_threads: -1
diff --git a/python/rapidocr/inference_engine/onnxruntime/main.py b/python/rapidocr/inference_engine/onnxruntime/main.py
index 4627bcb..ee4bf5a 100644
--- a/python/rapidocr/inference_engine/onnxruntime/main.py
+++ b/python/rapidocr/inference_engine/onnxruntime/main.py
@@ -19,6 +19,18 @@ class OrtInferSession(InferSession):
def __init__(self, cfg: Dict[str, Any]):
self.logger = Logger(logger_name=__name__).get_log()
+ # support custom session (PR #451)
+ session = cfg.get("session", None)
+ if session is not None:
+ if not isinstance(session, InferenceSession):
+ raise TypeError(
+ f"Expected session to be an InferenceSession, got {type(session)}"
+ )
+
+ self.logger.debug("Using the provided InferenceSession for inference.")
+ self.session = session
+ return
+
model_path = cfg.get("model_path", None)
if model_path is None:
# 说明用户没有指定自己模型,使用默认模型
@@ -45,11 +57,8 @@ class OrtInferSession(InferSession):
self._verify_model(model_path)
sess_opt = self._init_sess_opts(cfg.engine_cfg)
- provider_cfg = ProviderConfig(
- cfg_use_cuda=cfg.engine_cfg.get("use_cuda", False),
- cfg_use_dml=cfg.engine_cfg.get("use_dml", False),
- cfg_use_cann=cfg.engine_cfg.get("use_cann", False),
- )
+
+ provider_cfg = ProviderConfig(engine_cfg=cfg.engine_cfg)
self.session = InferenceSession(
model_path,
sess_options=sess_opt,
diff --git a/python/rapidocr/inference_engine/onnxruntime/provider_config.py b/python/rapidocr/inference_engine/onnxruntime/provider_config.py
index a8ca3de..0e58704 100644
--- a/python/rapidocr/inference_engine/onnxruntime/provider_config.py
+++ b/python/rapidocr/inference_engine/onnxruntime/provider_config.py
@@ -3,7 +3,7 @@
# @Contact: [email protected]
import platform
from enum import Enum
-from typing import Any, Dict, List
+from typing import Any, Dict, List, Sequence
from onnxruntime import get_available_providers, get_device
@@ -18,15 +18,17 @@ class EP(Enum):
class ProviderConfig:
- def __init__(self, cfg_use_cuda: bool, cfg_use_dml: bool, cfg_use_cann: bool):
+ def __init__(self, engine_cfg: Dict[str, Any]):
self.logger = Logger(logger_name=__name__).get_log()
self.had_providers: List[str] = get_available_providers()
self.default_provider = self.had_providers[0]
- self.cfg_use_cuda = cfg_use_cuda
- self.cfg_use_dml = cfg_use_dml
- self.cfg_use_cann = cfg_use_cann
+ self.cfg_use_cuda = engine_cfg.get("use_cuda", False)
+ self.cfg_use_dml = engine_cfg.get("use_dml", False)
+ self.cfg_use_cann = engine_cfg.get("use_cann", False)
+
+ self.cfg = engine_cfg
def get_ep_list(self):
results = [(EP.CPU_EP.value, self.cpu_ep_cfg())]
@@ -47,34 +49,23 @@ class ProviderConfig:
return results
def cpu_ep_cfg(self) -> Dict[str, Any]:
- return {
- "arena_extend_strategy": "kSameAsRequested",
- }
+ return self.cfg.cpu_ep_cfg
def cuda_ep_cfg(self) -> Dict[str, Any]:
- return {
- "device_id": 0,
- "arena_extend_strategy": "kNextPowerOfTwo",
- "cudnn_conv_algo_search": "EXHAUSTIVE",
- "do_copy_in_default_stream": True,
- }
+ return self.cfg.cuda_ep_cfg
def dml_ep_cfg(self) -> Dict[str, Any]:
+ if self.cfg.dm_ep_cfg is not None:
+ return self.cfg.dm_ep_cfg
+
if self.use_cuda:
return self.cuda_ep_cfg()
return self.cpu_ep_cfg()
def cann_ep_cfg(self) -> Dict[str, Any]:
- return {
- "device_id": 0,
- "arena_extend_strategy": "kNextPowerOfTwo",
- "npu_mem_limit": 20 * 1024 * 1024 * 1024,
- "op_select_impl_mode": "high_performance",
- "optypelist_for_implmode": "Gelu",
- "enable_cann_graph": True,
- }
+ return self.cfg.cann_ep_cfg
- def verify_providers(self, session_providers: List[str]):
+ def verify_providers(self, session_providers: Sequence[str]):
if not session_providers:
raise ValueError("Session Providers is empty")
@@ -89,7 +80,7 @@ class ProviderConfig:
for ep, check_func in providers_to_check.items():
if check_func() and first_provider != ep.value:
self.logger.warning(
- f"{ep.value} is not available for current env, the inference part is automatically shifted to be executed under {first_provider}. "
+ f"{ep.value} is available, but the inference part is automatically shifted to be executed under {first_provider}. "
)
self.logger.warning(f"The available lists are {session_providers}")
@@ -104,26 +95,16 @@ class ProviderConfig:
self.logger.warning(
f"{CUDA_EP} is not in available providers ({self.had_providers}). Use {self.default_provider} inference by default."
)
- self.logger.info("!!! Recommend to use rapidocr_paddle for inference on GPU.")
- self.logger.info(
- "(For reference only) If you want to use GPU acceleration, you must do:"
- )
- self.logger.info(
- "First, uninstall all onnxruntime packages in current environment."
- )
- self.logger.info(
- "Second, install onnxruntime-gpu by `pip install onnxruntime-gpu`."
- )
- self.logger.info(
- "Note the onnxruntime-gpu version must match your cuda and cudnn version."
- )
- self.logger.info(
- "You can refer this link: https://onnxruntime.ai/docs/execution-providers/CUDA-EP.html"
- )
- self.logger.info(
- "Third, ensure %s is in available providers list. e.g. ['CUDAExecutionProvider', 'CPUExecutionProvider']",
- CUDA_EP,
- )
+ install_instructions = [
+ f"If you want to use {CUDA_EP} acceleration, you must do:"
+ "(For reference only) If you want to use GPU acceleration, you must do:",
+ "First, uninstall all onnxruntime packages in current environment.",
+ "Second, install onnxruntime-gpu by `pip install onnxruntime-gpu`.",
+ "Note the onnxruntime-gpu version must match your cuda and cudnn version.",
+ "You can refer this link: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html",
+ f"Third, ensure {CUDA_EP} is in available providers list. e.g. ['CUDAExecutionProvider', 'CPUExecutionProvider']",
+ ]
+ self.print_log(install_instructions)
return False
def is_dml_available(self) -> bool:
@@ -152,22 +133,16 @@ class ProviderConfig:
return True
self.logger.warning(
- "%s is not in available providers (%s). Use %s inference by default.",
- DML_EP,
- self.had_providers,
- self.default_provider,
- )
- self.logger.info("If you want to use DirectML acceleration, you must do:")
- self.logger.info(
- "First, uninstall all onnxruntime packages in current environment."
- )
- self.logger.info(
- "Second, install onnxruntime-directml by `pip install onnxruntime-directml`"
- )
- self.logger.info(
- "Third, ensure %s is in available providers list. e.g. ['DmlExecutionProvider', 'CPUExecutionProvider']",
- DML_EP,
- )
+ f"{DML_EP} is not in available providers ({self.had_providers}). Use {self.default_provider} inference by default."
+ )
+ install_instructions = [
+ f"{DML_EP} is not in available providers ({self.had_providers}). Use {self.default_provider} inference by default.",
+ "If you want to use DirectML acceleration, you must do:",
+ "First, uninstall all onnxruntime packages in current environment.",
+ "Second, install onnxruntime-directml by `pip install onnxruntime-directml`",
+ f"Third, ensure {DML_EP} is in available providers list. e.g. ['DmlExecutionProvider', 'CPUExecutionProvider']",
+ ]
+ self.print_log(install_instructions)
return False
def is_cann_available(self) -> bool:
@@ -179,23 +154,18 @@ class ProviderConfig:
return True
self.logger.warning(
- "%s is not in available providers (%s). Use %s inference by default.",
- CANN_EP,
- self.had_providers,
- self.default_provider,
- )
- self.logger.info("If you want to use CANN acceleration, you must do:")
- self.logger.info(
- "First, ensure you have installed Huawei Ascend software stack."
- )
- self.logger.info(
- "Second, install onnxruntime with CANN support by following the instructions at:"
- )
- self.logger.info(
- "\thttps://onnxruntime.ai/docs/execution-providers/CANN-ExecutionProvider.html"
- )
- self.logger.info(
- "Third, ensure %s is in available providers list. e.g. ['CANNExecutionProvider', 'CPUExecutionProvider']",
- CANN_EP,
- )
+ f"{CANN_EP} is not in available providers ({self.had_providers}). Use {self.default_provider} inference by default."
+ )
+ install_instructions = [
+ "If you want to use CANN acceleration, you must do:",
+ "First, ensure you have installed Huawei Ascend software stack.",
+ "Second, install onnxruntime with CANN support by following the instructions at:",
+ "\thttps://onnxruntime.ai/docs/execution-providers/community-maintained/CANN-ExecutionProvider.html",
+ f"Third, ensure {CANN_EP} is in available providers list. e.g. ['CANNExecutionProvider', 'CPUExecutionProvider']",
+ ]
+ self.print_log(install_instructions)
return False
+
+ def print_log(self, log_list: List[str]):
+ for log_info in log_list:
+ self.logger.info(log_info)
diff --git a/python/tests/test_main.py b/python/tests/test_main.py
index 6bd3e5f..17be855 100644
--- a/python/tests/test_main.py
+++ b/python/tests/test_main.py
@@ -201,6 +201,7 @@ def test_en_lang():
assert result.txts[0] == "3"
[email protected](sys.platform.startswith("darwin"), reason="does not run on macOS")
def test_engine_openvino():
engine = get_engine(
params={