Skip to content

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

feat(inference): add get_character_list abstract method to InferSession

SWHLe549f7eacc22
diff --git a/python/rapidocr/inference_engine/base.py b/python/rapidocr/inference_engine/base.py
index 6bf6ca7..6edaa89 100644
--- a/python/rapidocr/inference_engine/base.py
+++ b/python/rapidocr/inference_engine/base.py
@@ -5,7 +5,7 @@ import abc
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
-from typing import Dict, Union
+from typing import Dict, List, Union
import numpy as np
from omegaconf import OmegaConf
@@ -109,6 +109,10 @@ class InferSession(abc.ABC):
def have_key(self, key: str = "character") -> bool:
pass
+ @abc.abstractmethod
+ def get_character_list(self, key: str = "character") -> List[str]:
+ pass
+
@classmethod
def get_model_url(cls, file_info: FileInfo) -> Dict[str, str]:
engine_type = file_info.engine_type.value
diff --git a/python/rapidocr/inference_engine/mnn/main.py b/python/rapidocr/inference_engine/mnn/main.py
index 09934d4..baef27d 100644
--- a/python/rapidocr/inference_engine/mnn/main.py
+++ b/python/rapidocr/inference_engine/mnn/main.py
@@ -3,6 +3,7 @@
# @Contact: [email protected]
import traceback
from pathlib import Path
+from typing import List
import MNN
import numpy as np
@@ -74,6 +75,9 @@ class MNNInferSession(InferSession):
def have_key(self, key: str = "character") -> bool:
return False
+ def get_character_list(self, key: str = "character") -> List[str]:
+ return []
+
class MNNError(Exception):
pass
diff --git a/python/rapidocr/inference_engine/paddle/main.py b/python/rapidocr/inference_engine/paddle/main.py
index e2a04f3..e6f8b71 100644
--- a/python/rapidocr/inference_engine/paddle/main.py
+++ b/python/rapidocr/inference_engine/paddle/main.py
@@ -2,7 +2,7 @@
# @Author: SWHL
# @Contact: [email protected]
from pathlib import Path
-from typing import Optional, Tuple
+from typing import List, Optional, Tuple
import numpy as np
from omegaconf.errors import ConfigKeyError
@@ -142,6 +142,9 @@ class PaddleInferSession(InferSession):
def have_key(self, key: str = "character") -> bool:
return False
+ def get_character_list(self, key: str = "character") -> List[str]:
+ return []
+
class PaddleInferError(Exception):
pass
diff --git a/python/rapidocr/inference_engine/pytorch/main.py b/python/rapidocr/inference_engine/pytorch/main.py
index 8dc7a6b..6da3dd5 100644
--- a/python/rapidocr/inference_engine/pytorch/main.py
+++ b/python/rapidocr/inference_engine/pytorch/main.py
@@ -1,6 +1,8 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: [email protected]
+from typing import List
+
import numpy as np
import torch
@@ -24,6 +26,9 @@ class TorchInferSession(InferSession):
def have_key(self, key: str = "character") -> bool:
return False
+ def get_character_list(self, key: str = "character") -> List[str]:
+ return []
+
class TorchInferError(Exception):
pass
diff --git a/python/rapidocr/inference_engine/tensorrt/main.py b/python/rapidocr/inference_engine/tensorrt/main.py
index 6b636a7..121264e 100644
--- a/python/rapidocr/inference_engine/tensorrt/main.py
+++ b/python/rapidocr/inference_engine/tensorrt/main.py
@@ -24,13 +24,14 @@ Example:
>>> with TRTInferSession(config) as session:
... output = session(input_array)
"""
+
import traceback
from pathlib import Path
-from typing import Any, Dict, Optional
+from typing import Any, Dict, List, Optional
import numpy as np
-from cuda.bindings import runtime as cudart
import tensorrt as trt
+from cuda.bindings import runtime as cudart
from ...utils.download_file import DownloadFile, DownloadFileInput
from ...utils.log import logger
@@ -101,7 +102,9 @@ class TRTInferSession(InferSession):
self._requires_square_input = self._check_multi_model(cfg)
if self._requires_square_input:
self._max_square_size = self._get_max_profile_size()
- logger.debug(f"MULTI det model: requires square input, max_size={self._max_square_size}")
+ logger.debug(
+ f"MULTI det model: requires square input, max_size={self._max_square_size}"
+ )
else:
self._requires_square_input = False
self._max_square_size = 2048
@@ -317,8 +320,9 @@ class TRTInferSession(InferSession):
"""
try:
from ...utils.typings import LangDet, TaskType
- is_det = getattr(cfg, 'task_type', None) == TaskType.DET
- is_multi = getattr(cfg, 'lang_type', None) == LangDet.MULTI
+
+ is_det = getattr(cfg, "task_type", None) == TaskType.DET
+ is_multi = getattr(cfg, "lang_type", None) == LangDet.MULTI
return is_det and is_multi
except Exception:
return False
@@ -580,6 +584,9 @@ class TRTInferSession(InferSession):
"""
return False
+ def get_character_list(self, key: str = "character") -> List[str]:
+ return []
+
@classmethod
def get_dict_key_url(cls, file_info: FileInfo) -> Optional[str]:
"""Get dictionary URL by falling back to Paddle/ONNX model config.