📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
Add use_cuda parameter
SWHL5bc6fee14bc1
diff --git a/python/config.yaml b/python/config.yamlindex 0c59831..6d177e7 100644--- a/python/config.yaml+++ b/python/config.yaml@@ -8,6 +8,15 @@ Det:class_name: TextDetectormodel_path: resources/models/ch_PP-OCRv2_det_infer.onnx+ use_cuda: false+ # Details of the params: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html+ CUDAExecutionProvider:+ device_id: 0+ arena_extend_strategy: kNextPowerOfTwo+ gpu_mem_limit: 2 * 1024 * 1024 * 1024+ cudnn_conv_algo_search: EXHAUSTIVE+ do_copy_in_default_stream: true+pre_process:DetResizeForTest:limit_side_len: 736@@ -33,6 +42,14 @@ Cls:class_name: TextClassifiermodel_path: resources/models/ch_ppocr_mobile_v2.0_cls_infer.onnx+ use_cuda: false+ CUDAExecutionProvider:+ device_id: 0+ arena_extend_strategy: kNextPowerOfTwo+ gpu_mem_limit: 2 * 1024 * 1024 * 1024+ cudnn_conv_algo_search: EXHAUSTIVE+ do_copy_in_default_stream: true+cls_image_shape: [3, 48, 192]cls_batch_num: 6cls_thresh: 0.9@@ -43,6 +60,14 @@ Rec:class_name: TextRecognizermodel_path: resources/models/ch_ppocr_mobile_v2.0_rec_infer.onnx+ use_cuda: false+ CUDAExecutionProvider:+ device_id: 0+ arena_extend_strategy: kNextPowerOfTwo+ gpu_mem_limit: 2 * 1024 * 1024 * 1024+ cudnn_conv_algo_search: EXHAUSTIVE+ do_copy_in_default_stream: true+rec_img_shape: [3, 32, 320]rec_batch_num: 6keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/config.yaml b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/config.yamlindex e6fa00e..89497ae 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/config.yaml+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/config.yaml@@ -1,5 +1,14 @@model_path: resources/models/ch_ppocr_mobile_v2.0_cls_infer.onnx+use_cuda: false+# Details of the params: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html+CUDAExecutionProvider:+ device_id: 0+ arena_extend_strategy: kNextPowerOfTwo+ gpu_mem_limit: 2 * 1024 * 1024 * 1024+ cudnn_conv_algo_search: EXHAUSTIVE+ do_copy_in_default_stream: true+cls_image_shape: [3, 48, 192]cls_batch_num: 6cls_thresh: 0.9diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.pyindex fc06ac1..38d8549 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/text_cls.py@@ -22,9 +22,9 @@ import numpy as npimport onnxruntime as orttry:- from .utils import ClsPostProcess, read_yaml+ from .utils import ClsPostProcess, read_yaml, OrtInferSessionexcept:- from utils import ClsPostProcess, read_yaml+ from utils import ClsPostProcess, read_yaml, OrtInferSessionclass TextClassifier(object):@@ -34,11 +34,9 @@ class TextClassifier(object):self.cls_thresh = config['cls_thresh']self.postprocess_op = ClsPostProcess(config['label_list'])- sess_opt = ort.SessionOptions()- sess_opt.log_severity_level = 4- sess_opt.enable_cpu_mem_arena = False- self.session = ort.InferenceSession(config['model_path'], sess_opt)- self.input_name = self.session.get_inputs()[0].name+ session_instance = OrtInferSession(config)+ self.session = session_instance.session+ self.input_name = session_instance.get_input_name()def resize_norm_img(self, img):img_c, img_h, img_w = self.cls_image_shape@@ -63,7 +61,6 @@ class TextClassifier(object):padding_im[:, :, :resized_w] = resized_imagereturn padding_im-def __call__(self, img_list: List[np.ndarray]):if isinstance(img_list, np.ndarray):img_list = [img_list]@@ -123,4 +120,4 @@ if __name__ == "__main__":img = cv2.imread(args.image_path)img_list, cls_res, predict_time = text_classifier(img)for ino in range(len(img_list)):- print(f"分类结果:{cls_res[ino]}")+ print(f"cls result:{cls_res[ino]}")diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.pyindex dae9175..55b4816 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py@@ -11,7 +11,47 @@# 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.+import warnings+import yaml+from onnxruntime import (get_available_providers, get_device,+ SessionOptions, InferenceSession)+++class OrtInferSession(object):+ def __init__(self, config):+ sess_opt = SessionOptions()+ sess_opt.log_severity_level = 4+ sess_opt.enable_cpu_mem_arena = False++ cuda_ep = 'CUDAExecutionProvider'+ cpu_ep = 'CPUExecutionProvider'++ EP_list = []+ if config['use_cuda'] and get_device() == 'GPU' \+ and cuda_ep in get_available_providers():+ EP_list = [(cuda_ep, config[cuda_ep])]+ else:+ raise RuntimeError(f'{cuda_ep} is not avaiable for --use_cuda!')++ EP_list.append(cpu_ep)++ 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 get_input_name(self, input_idx=0):+ return self.session.get_inputs()[input_idx].name++ def get_output_name(self, output_idx=0):+ return self.session.get_outputs()[output_idx].namedef read_yaml(yaml_path):diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/config.yaml b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/config.yamlindex 0390513..428ccfc 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/config.yaml+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/config.yaml@@ -1,7 +1,7 @@model_path: G:\ProgramFiles\_self\RapidOCR\python\resources\models\ch_PP-OCRv2_det_infer.onnx-# https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.htmluse_cuda: false+# Details of the params: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.htmlCUDAExecutionProvider:device_id: 0arena_extend_strategy: kNextPowerOfTwodiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/text_detect.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/text_detect.pyindex d656616..7308253 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/text_detect.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/text_detect.py@@ -37,7 +37,6 @@ class TextDetector(object):session_instance = OrtInferSession(config)self.session = session_instance.sessionself.input_name = session_instance.get_input_name()- self.output_name = session_instance.get_output_name()def order_points_clockwise(self, pts):"""@@ -100,7 +99,7 @@ class TextDetector(object):shape_list = np.expand_dims(shape_list, axis=0)starttime = time.time()- preds = self.session.run([self.output_name], {self.input_name: img})+ preds = self.session.run(None, {self.input_name: img})post_result = self.postprocess_op(preds[0], shape_list)@@ -124,4 +123,4 @@ if __name__ == "__main__":dt_boxes, elapse = text_detector(img)src_im = draw_text_det_res(dt_boxes, args.image_path)cv2.imwrite('det_results.jpg', src_im)- print('图像已经保存为det_results.jpg了')+ print('The det_results.jpg has been saved in the current directory.')diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.pyindex b1fa89d..e743908 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_det/utils.py@@ -50,10 +50,12 @@ class OrtInferSession(object):self.session = InferenceSession(config['model_path'],sess_options=sess_opt,providers=EP_list)- # https://github.com/microsoft/onnxruntime/issues/11323- # https://github.com/microsoft/onnxruntime/issues/11996+if config['use_cuda'] and cuda_ep not in self.session.get_providers():- warnings.warn(f'{cuda_ep} is not avaiable for current env',+ 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 get_input_name(self, input_idx=0):diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/config.yaml b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/config.yamlindex 3c3c9df..ffcfc1d 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/config.yaml+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/config.yaml@@ -1,5 +1,14 @@model_path: resources/models/ch_ppocr_server_v2.0_rec_infer.onnx+use_cuda: false+# Details of the params: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html+CUDAExecutionProvider:+ device_id: 0+ arena_extend_strategy: kNextPowerOfTwo+ gpu_mem_limit: 2 * 1024 * 1024 * 1024+ cudnn_conv_algo_search: EXHAUSTIVE+ do_copy_in_default_stream: true+rec_img_shape: [3, 32, 320]rec_batch_num: 6keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/text_recognize.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/text_recognize.pyindex 2753ea7..4fd9265 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/text_recognize.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/text_recognize.py@@ -14,17 +14,15 @@import argparseimport mathimport time-from pathlib import Pathfrom typing import Listimport cv2import numpy as np-import onnxruntimetry:- from .utils import CTCLabelDecode, read_yaml+ from .utils import CTCLabelDecode, read_yaml, OrtInferSessionexcept:- from utils import CTCLabelDecode, read_yaml+ from utils import CTCLabelDecode, read_yaml, OrtInferSessionclass TextRecognizer(object):@@ -34,12 +32,9 @@ class TextRecognizer(object):self.character_dict_path = config['keys_path']self.postprocess_op = CTCLabelDecode(self.character_dict_path)- sess_opt = onnxruntime.SessionOptions()- sess_opt.log_severity_level = 4- sess_opt.enable_cpu_mem_arena = False- self.session = onnxruntime.InferenceSession(config['model_path'],- sess_opt)- self.input_name = self.session.get_inputs()[0].name+ session_instance = OrtInferSession(config)+ self.session = session_instance.session+ self.input_name = session_instance.get_input_name()def resize_norm_img(self, img, max_wh_ratio):img_channel, img_height, img_width = self.rec_image_shapediff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.pyindex c6edeb6..489a04d 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/utils.py@@ -1,8 +1,48 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]+import warnings+import numpy as npimport yaml+from onnxruntime import (get_available_providers, get_device,+ SessionOptions, InferenceSession)+++class OrtInferSession(object):+ def __init__(self, config):+ sess_opt = SessionOptions()+ sess_opt.log_severity_level = 4+ sess_opt.enable_cpu_mem_arena = False++ cuda_ep = 'CUDAExecutionProvider'+ cpu_ep = 'CPUExecutionProvider'++ EP_list = []+ if config['use_cuda'] and get_device() == 'GPU' \+ and cuda_ep in get_available_providers():+ EP_list = [(cuda_ep, config[cuda_ep])]+ else:+ raise RuntimeError(f'{cuda_ep} is not avaiable for --use_cuda!')++ EP_list.append(cpu_ep)++ 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 get_input_name(self, input_idx=0):+ return self.session.get_inputs()[input_idx].name++ def get_output_name(self, output_idx=0):+ return self.session.get_outputs()[output_idx].namedef read_yaml(yaml_path):diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/word_1.jpg b/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/word_1.jpgdeleted file mode 100644index 934cc1c..0000000Binary files a/python/rapidocr_onnxruntime/ch_ppocr_v2_rec/word_1.jpg and /dev/null differdiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yamlindex 4f903d6..2428429 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/config.yaml@@ -1,5 +1,14 @@model_path: resources/models/PPOCRv3/ch_PP-OCRv3_rec_infer.onnx+use_cuda: false+# Details of the params: https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html+CUDAExecutionProvider:+ device_id: 0+ arena_extend_strategy: kNextPowerOfTwo+ gpu_mem_limit: 2 * 1024 * 1024 * 1024+ cudnn_conv_algo_search: EXHAUSTIVE+ do_copy_in_default_stream: true+rec_img_shape: [3, 48, 320]rec_batch_num: 6keys_path: resources/rec_dict/ppocr_keys_v1.txtdiff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.pyindex ca88ce5..b00bf95 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/text_recognize.py@@ -22,9 +22,9 @@ import numpy as npimport onnxruntimetry:- from .utils import CTCLabelDecode, read_yaml+ from .utils import CTCLabelDecode, read_yaml, OrtInferSessionexcept:- from utils import CTCLabelDecode, read_yaml+ from utils import CTCLabelDecode, read_yaml, OrtInferSessionroot_path = Path(__file__).parent@@ -36,12 +36,9 @@ class TextRecognizer(object):self.character_dict_path = config['keys_path']self.postprocess_op = CTCLabelDecode(self.character_dict_path)- sess_opt = onnxruntime.SessionOptions()- sess_opt.log_severity_level = 4- sess_opt.enable_cpu_mem_arena = False- self.session = onnxruntime.InferenceSession(config['model_path'],- sess_opt)- self.input_name = self.session.get_inputs()[0].name+ session_instance = OrtInferSession(config)+ self.session = session_instance.session+ self.input_name = session_instance.get_input_name()def resize_norm_img(self, img, max_wh_ratio):img_channel, img_height, img_width = self.rec_image_shapediff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.pyindex c6edeb6..e50bc2a 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py@@ -1,8 +1,48 @@# -*- encoding: utf-8 -*-# @Author: SWHL# @Contact: [email protected]+import warnings+import numpy as npimport yaml+from onnxruntime import (InferenceSession, SessionOptions,+ get_available_providers, get_device)+++class OrtInferSession(object):+ def __init__(self, config):+ sess_opt = SessionOptions()+ sess_opt.log_severity_level = 4+ sess_opt.enable_cpu_mem_arena = False++ cuda_ep = 'CUDAExecutionProvider'+ cpu_ep = 'CPUExecutionProvider'++ EP_list = []+ if config['use_cuda'] and get_device() == 'GPU' \+ and cuda_ep in get_available_providers():+ EP_list = [(cuda_ep, config[cuda_ep])]+ else:+ raise RuntimeError(f'{cuda_ep} is not avaiable for --use_cuda!')++ EP_list.append(cpu_ep)++ 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 get_input_name(self, input_idx=0):+ return self.session.get_inputs()[input_idx].name++ def get_output_name(self, output_idx=0):+ return self.session.get_outputs()[output_idx].namedef read_yaml(yaml_path):diff --git a/python/test_demo.py b/python/test_demo.pyindex a7697ad..67c10f8 100644--- a/python/test_demo.py+++ b/python/test_demo.py@@ -89,8 +89,7 @@ def visualize(image_path, boxes, rec_res, font_path="fonts/msyh.ttc"):if __name__ == '__main__':- config_path = 'config.yaml'- text_sys = TextSystem(config_path)+ text_sys = TextSystem('config.yaml')image_path = r'test_images/det_images/ch_en_num.jpg'img = cv2.imread(image_path)