π Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
Optim inference code
SWHLa8150f28c2bb
diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.pyindex a46b77e..4e01443 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py@@ -20,10 +20,8 @@ from typing import Listimport cv2import numpy as np-try:- from .utils import ClsPostProcess, read_yaml, OrtInferSession-except:- from utils import ClsPostProcess, read_yaml, OrtInferSession+from .utils import ClsPostProcess+from rapidocr_onnxruntime.utils import read_yaml, OrtInferSessionclass TextClassifier():diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.pyindex 1ede3e0..466fee9 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py@@ -11,81 +11,6 @@# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.-from pathlib import Path-import warnings--import yaml-import numpy as np-from onnxruntime import (get_available_providers, get_device,- SessionOptions, InferenceSession,- GraphOptimizationLevel)-root_dir = Path(__file__).resolve().parent.parent---class OrtInferSession():- def __init__(self, config):- sess_opt = SessionOptions()- sess_opt.log_severity_level = 4- sess_opt.enable_cpu_mem_arena = False- sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL-- cuda_ep = 'CUDAExecutionProvider'- cpu_ep = 'CPUExecutionProvider'- cpu_provider_options = {- "arena_extend_strategy": "kSameAsRequested",- }-- EP_list = []- if config['use_cuda'] and get_device() == 'GPU' \- and cuda_ep in get_available_providers():- EP_list = [(cuda_ep, config[cuda_ep])]- EP_list.append((cpu_ep, cpu_provider_options))-- config['model_path'] = str(root_dir / config['model_path'])- self._verify_model(config['model_path'])- self.session = InferenceSession(config['model_path'],- sess_options=sess_opt,- providers=EP_list)-- if config['use_cuda'] and cuda_ep not in self.session.get_providers():- warnings.warn(f'{cuda_ep} is not avaiable for current env, the inference part is automatically shifted to be executed under {cpu_ep}.\n'- 'Please ensure the installed onnxruntime-gpu version matches your cuda and cudnn version, '- 'you can check their relations from the offical web site: '- 'https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html',- RuntimeWarning)-- def __call__(self, input_content: np.ndarray) -> np.ndarray:- input_dict = dict(zip(self.get_input_names(), [input_content]))- try:- return self.session.run(self.get_output_names(), input_dict)- except Exception as e:- raise ONNXRuntimeError('ONNXRuntime inferece failed.') from e-- def get_input_names(self, ):- return [v.name for v in self.session.get_inputs()]-- def get_output_names(self,):- return [v.name for v in self.session.get_outputs()]-- @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 ONNXRuntimeError(Exception):- pass---def read_yaml(yaml_path):- with open(yaml_path, 'rb') as f:- data = yaml.load(f, Loader=yaml.Loader)- return data--class ClsPostProcess():""" Convert between text-label and text-index """@@ -101,4 +26,4 @@ class ClsPostProcess():return decode_outlabel = [(self.label_list[idx], 1.0) for idx in label]- return decode_out, label\ No newline at end of file+ return decode_out, labeldiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.pyindex ceaf36e..8aac787 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/text_detect.py@@ -20,12 +20,8 @@ import timeimport cv2import numpy as np-try:- from .utils import (DBPostProcess, create_operators,- transform, read_yaml, OrtInferSession)-except:- from utils import (DBPostProcess, create_operators,- transform, read_yaml, OrtInferSession)+from .utils import DBPostProcess, create_operators, transform+from rapidocr_onnxruntime.utils import read_yaml, OrtInferSessionclass TextDetector():diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.pyindex 36631f2..b1b489f 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py@@ -17,84 +17,13 @@# @Author: SWHL# @Contact: [email protected]import sys-import warnings-from pathlib import Pathimport cv2import numpy as npimport pyclipperimport six-import yaml-from onnxruntime import (GraphOptimizationLevel, InferenceSession,- SessionOptions, get_available_providers, get_device)from shapely.geometry import Polygon-root_dir = Path(__file__).resolve().parent.parent---class OrtInferSession():- def __init__(self, config):- sess_opt = SessionOptions()- sess_opt.log_severity_level = 4- sess_opt.enable_cpu_mem_arena = False- sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL-- cuda_ep = 'CUDAExecutionProvider'- cpu_ep = 'CPUExecutionProvider'- cpu_provider_options = {- "arena_extend_strategy": "kSameAsRequested",- }-- EP_list = []- if config['use_cuda'] and get_device() == 'GPU' \- and cuda_ep in get_available_providers():- EP_list = [(cuda_ep, config[cuda_ep])]- EP_list.append((cpu_ep, cpu_provider_options))-- config['model_path'] = str(root_dir / config['model_path'])- self._verify_model(config['model_path'])- self.session = InferenceSession(config['model_path'],- sess_options=sess_opt,- providers=EP_list)-- if config['use_cuda'] and cuda_ep not in self.session.get_providers():- warnings.warn(f'{cuda_ep} is not avaiable for current env, the inference part is automatically shifted to be executed under {cpu_ep}.\n'- 'Please ensure the installed onnxruntime-gpu version matches your cuda and cudnn version, '- 'you can check their relations from the offical web site: '- 'https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html',- RuntimeWarning)-- def __call__(self, input_content: np.ndarray) -> np.ndarray:- input_dict = dict(zip(self.get_input_names(), [input_content]))- try:- return self.session.run(self.get_output_names(), input_dict)- except Exception as e:- raise ONNXRuntimeError('ONNXRuntime inferece failed.') from e-- def get_input_names(self,):- return [v.name for v in self.session.get_inputs()]-- def get_output_names(self,):- return [v.name for v in self.session.get_outputs()]-- @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 ONNXRuntimeError(Exception):- pass---def read_yaml(yaml_path):- with open(yaml_path, 'rb') as f:- data = yaml.load(f, Loader=yaml.Loader)- return data-class DecodeImage():""" decode image """diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.pyindex e996420..20a375f 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py@@ -19,10 +19,8 @@ from typing import Listimport cv2import numpy as np-try:- from .utils import CTCLabelDecode, read_yaml, OrtInferSession-except:- from utils import CTCLabelDecode, read_yaml, OrtInferSession+from .utils import CTCLabelDecode+from rapidocr_onnxruntime.utils import read_yaml, OrtInferSessionclass TextRecognizer():diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.pyindex 5259197..a1a53b8 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py@@ -1,88 +1,7 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]-import warnings-from pathlib import Path-import numpy as np-import yaml-from onnxruntime import (GraphOptimizationLevel, InferenceSession,- SessionOptions, get_available_providers, get_device)--root_dir = Path(__file__).resolve().parent.parent---class OrtInferSession():- def __init__(self, config):- sess_opt = SessionOptions()- sess_opt.log_severity_level = 4- sess_opt.enable_cpu_mem_arena = False- sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL-- cuda_ep = 'CUDAExecutionProvider'- cpu_ep = 'CPUExecutionProvider'- cpu_provider_options = {- "arena_extend_strategy": "kSameAsRequested",- }-- EP_list = []- if config['use_cuda'] and get_device() == 'GPU' \- and cuda_ep in get_available_providers():- EP_list = [(cuda_ep, config[cuda_ep])]- EP_list.append((cpu_ep, cpu_provider_options))-- config['model_path'] = str(root_dir / config['model_path'])- self._verify_model(config['model_path'])- self.session = InferenceSession(config['model_path'],- sess_options=sess_opt,- providers=EP_list)-- if config['use_cuda'] and cuda_ep not in self.session.get_providers():- warnings.warn(f'{cuda_ep} is not avaiable for current env, the inference part is automatically shifted to be executed under {cpu_ep}.\n'- 'Please ensure the installed onnxruntime-gpu version matches your cuda and cudnn version, '- 'you can check their relations from the offical web site: '- 'https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html',- RuntimeWarning)-- def __call__(self, input_content: np.ndarray) -> np.ndarray:- input_dict = dict(zip(self.get_input_names(), [input_content]))- try:- return self.session.run(self.get_output_names(), input_dict)- except Exception as e:- raise ONNXRuntimeError('ONNXRuntime inferece failed.') from e-- def get_input_names(self, ):- return [v.name for v in self.session.get_inputs()]-- def get_output_names(self,):- return [v.name for v in self.session.get_outputs()]-- def get_character_list(self, key: str = 'character'):- return self.meta_dict[key].splitlines()-- def have_key(self, key: str = 'character') -> bool:- self.meta_dict = self.session.get_modelmeta().custom_metadata_map- if key in self.meta_dict.keys():- return True- return False-- @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 ONNXRuntimeError(Exception):- pass---def read_yaml(yaml_path):- with open(yaml_path, 'rb') as f:- data = yaml.load(f, Loader=yaml.Loader)- return dataclass CTCLabelDecode():diff --git a/python/rapidocr_onnxruntime/utils.py b/python/rapidocr_onnxruntime/utils.pynew file mode 100644index 0000000..fb54190--- /dev/null+++ b/python/rapidocr_onnxruntime/utils.py@@ -0,0 +1,85 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+import warnings+from pathlib import Path++import numpy as np+import yaml+from onnxruntime import (GraphOptimizationLevel, InferenceSession,+ SessionOptions, get_available_providers, get_device)++root_dir = Path(__file__).resolve().parent+++class OrtInferSession():+ def __init__(self, config):+ sess_opt = SessionOptions()+ sess_opt.log_severity_level = 4+ sess_opt.enable_cpu_mem_arena = False+ sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL++ cuda_ep = 'CUDAExecutionProvider'+ cpu_ep = 'CPUExecutionProvider'+ cpu_provider_options = {+ "arena_extend_strategy": "kSameAsRequested",+ }++ EP_list = []+ if config['use_cuda'] and get_device() == 'GPU' \+ and cuda_ep in get_available_providers():+ EP_list = [(cuda_ep, config[cuda_ep])]+ EP_list.append((cpu_ep, cpu_provider_options))++ config['model_path'] = str(root_dir / config['model_path'])+ self._verify_model(config['model_path'])+ self.session = InferenceSession(config['model_path'],+ sess_options=sess_opt,+ providers=EP_list)++ if config['use_cuda'] and cuda_ep not in self.session.get_providers():+ warnings.warn(f'{cuda_ep} is not avaiable for current env, the inference part is automatically shifted to be executed under {cpu_ep}.\n'+ 'Please ensure the installed onnxruntime-gpu version matches your cuda and cudnn version, '+ 'you can check their relations from the offical web site: '+ 'https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html',+ RuntimeWarning)++ def __call__(self, input_content: np.ndarray) -> np.ndarray:+ input_dict = dict(zip(self.get_input_names(), [input_content]))+ try:+ return self.session.run(self.get_output_names(), input_dict)+ except Exception as e:+ raise ONNXRuntimeError('ONNXRuntime inferece failed.') from e++ def get_input_names(self, ):+ return [v.name for v in self.session.get_inputs()]++ def get_output_names(self,):+ return [v.name for v in self.session.get_outputs()]++ def get_character_list(self, key: str = 'character'):+ return self.meta_dict[key].splitlines()++ def have_key(self, key: str = 'character') -> bool:+ self.meta_dict = self.session.get_modelmeta().custom_metadata_map+ if key in self.meta_dict.keys():+ return True+ return False++ @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 ONNXRuntimeError(Exception):+ pass+++def read_yaml(yaml_path):+ with open(yaml_path, 'rb') as f:+ data = yaml.load(f, Loader=yaml.Loader)+ return datadiff --git a/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.py b/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.pyindex 537e74d..cdbcbd8 100644--- a/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.py+++ b/python/rapidocr_openvino/ch_ppocr_v2_cls/text_cls.py@@ -20,10 +20,8 @@ from typing import Listimport cv2import numpy as np-try:- from .utils import ClsPostProcess, read_yaml, OpenVINOInferSession-except:- from utils import ClsPostProcess, read_yaml, OpenVINOInferSession+from .utils import ClsPostProcess+from rapidocr_openvino.utils import read_yaml, OpenVINOInferSessionclass TextClassifier():diff --git a/python/rapidocr_openvino/ch_ppocr_v2_cls/utils.py b/python/rapidocr_openvino/ch_ppocr_v2_cls/utils.pyindex 77806f9..cb579fa 100644--- a/python/rapidocr_openvino/ch_ppocr_v2_cls/utils.py+++ b/python/rapidocr_openvino/ch_ppocr_v2_cls/utils.py@@ -11,44 +11,6 @@# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.-from pathlib import Path-import numpy as np-import yaml-from openvino.runtime import Core---root_dir = Path(__file__).resolve().parent.parent---class OpenVINOInferSession():- def __init__(self, config):- ie = Core()-- config['model_path'] = str(root_dir / config['model_path'])- self._verify_model(config['model_path'])- model_onnx = ie.read_model(config['model_path'])- compile_model = ie.compile_model(model=model_onnx, device_name='CPU')- self.session = compile_model.create_infer_request()-- def __call__(self, input_content: np.ndarray) -> np.ndarray:- self.session.infer(inputs=[input_content])- return self.session.get_output_tensor().data-- @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.')---def read_yaml(yaml_path):- with open(yaml_path, 'rb') as f:- data = yaml.load(f, Loader=yaml.Loader)- return data--class ClsPostProcess():""" Convert between text-label and text-index """diff --git a/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py b/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.pyindex ef4f813..439f326 100644--- a/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py+++ b/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py@@ -18,12 +18,8 @@ import timeimport cv2import numpy as np-try:- from .utils import (DBPostProcess, create_operators,- transform, OpenVINOInferSession)-except:- from utils import (DBPostProcess, create_operators,- transform, OpenVINOInferSession)+from .utils import DBPostProcess, create_operators, transform+from rapidocr_openvino.utils import read_yaml, OpenVINOInferSessionclass TextDetector():@@ -103,7 +99,6 @@ if __name__ == "__main__":parser.add_argument('--image_path', type=str, default=None)args = parser.parse_args()- from utils import read_yamlconfig = read_yaml(args.config_path)text_detector = TextDetector(config)diff --git a/python/rapidocr_openvino/ch_ppocr_v3_det/utils.py b/python/rapidocr_openvino/ch_ppocr_v3_det/utils.pyindex c0a29c6..781dbb1 100644--- a/python/rapidocr_openvino/ch_ppocr_v3_det/utils.py+++ b/python/rapidocr_openvino/ch_ppocr_v3_det/utils.py@@ -17,47 +17,13 @@# @Author: SWHL# @Contact: [email protected]import sys-from pathlib import Pathimport cv2import numpy as npimport pyclipperimport six-import yaml-from openvino.runtime import Corefrom shapely.geometry import Polygon-root_dir = Path(__file__).resolve().parent.parent---class OpenVINOInferSession():- def __init__(self, config):- ie = Core()-- config['model_path'] = str(root_dir / config['model_path'])- self._verify_model(config['model_path'])- model_onnx = ie.read_model(config['model_path'])- compile_model = ie.compile_model(model=model_onnx, device_name='CPU')- self.session = compile_model.create_infer_request()-- def __call__(self, input_content: np.ndarray) -> np.ndarray:- self.session.infer(inputs=[input_content])- return self.session.get_output_tensor().data-- @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.')---def read_yaml(yaml_path):- with open(yaml_path, 'rb') as f:- data = yaml.load(f, Loader=yaml.Loader)- return data-class DecodeImage():""" decode image """diff --git a/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py b/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.pyindex 04b8151..d51aa25 100644--- a/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py+++ b/python/rapidocr_openvino/ch_ppocr_v3_rec/text_recognize.py@@ -23,10 +23,8 @@ from pathlib import Pathimport cv2import numpy as np-try:- from .utils import CTCLabelDecode, OpenVINOInferSession-except:- from utils import CTCLabelDecode, OpenVINOInferSession+from .utils import CTCLabelDecode+from rapidocr_openvino.utils import read_yaml, OpenVINOInferSessionclass TextRecognizer():@@ -35,7 +33,7 @@ class TextRecognizer():self.rec_batch_num = config['rec_batch_num']dict_path = str(Path(__file__).parent / 'ppocr_keys_v1.txt')- self.character_dict_path = config.get( 'keys_path', dict_path)+ self.character_dict_path = config.get('keys_path', dict_path)self.postprocess_op = CTCLabelDecode(self.character_dict_path)self.infer = OpenVINOInferSession(config)@@ -109,7 +107,6 @@ if __name__ == "__main__":parser.add_argument('--config_path', type=str, default='config.yaml')args = parser.parse_args()- from utils import read_yamlconfig = read_yaml(args.config_path)text_recognizer = TextRecognizer(config)diff --git a/python/rapidocr_openvino/ch_ppocr_v3_rec/utils.py b/python/rapidocr_openvino/ch_ppocr_v3_rec/utils.pyindex 6d70785..f40d906 100644--- a/python/rapidocr_openvino/ch_ppocr_v3_rec/utils.py+++ b/python/rapidocr_openvino/ch_ppocr_v3_rec/utils.py@@ -1,42 +1,7 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]-from pathlib import Pathimport numpy as np-import yaml-from openvino.runtime import Core---root_dir = Path(__file__).resolve().parent.parent---class OpenVINOInferSession():- def __init__(self, config):- ie = Core()-- config['model_path'] = str(root_dir / config['model_path'])- self._verify_model(config['model_path'])- model_onnx = ie.read_model(config['model_path'])- compile_model = ie.compile_model(model=model_onnx, device_name='CPU')- self.session = compile_model.create_infer_request()-- def __call__(self, input_content: np.ndarray) -> np.ndarray:- self.session.infer(inputs=[input_content])- return self.session.get_output_tensor().data-- @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.')---def read_yaml(yaml_path):- with open(yaml_path, 'rb') as f:- data = yaml.load(f, Loader=yaml.Loader)- return dataclass CTCLabelDecode():diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.pynew file mode 100644index 0000000..0db97c5--- /dev/null+++ b/python/rapidocr_openvino/utils.py@@ -0,0 +1,39 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+from pathlib import Path++import numpy as np+import yaml+from openvino.runtime import Core++root_dir = Path(__file__).resolve().parent+++class OpenVINOInferSession():+ def __init__(self, config):+ ie = Core()++ config['model_path'] = str(root_dir / config['model_path'])+ self._verify_model(config['model_path'])+ model_onnx = ie.read_model(config['model_path'])+ compile_model = ie.compile_model(model=model_onnx, device_name='CPU')+ self.session = compile_model.create_infer_request()++ def __call__(self, input_content: np.ndarray) -> np.ndarray:+ self.session.infer(inputs=[input_content])+ return self.session.get_output_tensor().data++ @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.')+++def read_yaml(yaml_path):+ with open(yaml_path, 'rb') as f:+ data = yaml.load(f, Loader=yaml.Loader)+ return data