Skip to content

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

Update rapidocr_openvino

SWHLb55449d3796f
diff --git a/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py b/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py
index 0b11a31..578d08e 100644
--- a/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py
+++ b/python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py
@@ -25,8 +25,31 @@ from .utils import DBPostProcess, create_operators, transform
class TextDetector:
def __init__(self, config):
- self.preprocess_op = create_operators(config["pre_process"])
- self.postprocess_op = DBPostProcess(**config["post_process"])
+ pre_process_list = {
+ "DetResizeForTest": {
+ "limit_side_len": config.get("limit_side_len", 736),
+ "limit_type": config.get("limit_type", "min"),
+ },
+ "NormalizeImage": {
+ "std": [0.229, 0.224, 0.225],
+ "mean": [0.485, 0.456, 0.406],
+ "scale": "1./255.",
+ "order": "hwc",
+ },
+ "ToCHWImage": None,
+ "KeepKeys": {"keep_keys": ["image", "shape"]},
+ }
+ self.preprocess_op = create_operators(pre_process_list)
+
+ post_process = {
+ "thresh": config.get("thresh", 0.3),
+ "box_thresh": config.get("box_thresh", 0.5),
+ "max_candidates": config.get("max_candidates", 1000),
+ "unclip_ratio": config.get("unclip_ratio", 1.6),
+ "use_dilation": config.get("use_dilation", True),
+ "score_mode": config.get("score_mode", "fast"),
+ }
+ self.postprocess_op = DBPostProcess(**post_process)
self.infer = OpenVINOInferSession(config)
diff --git a/python/rapidocr_openvino/config.yaml b/python/rapidocr_openvino/config.yaml
index f169cbb..4607d7d 100644
--- a/python/rapidocr_openvino/config.yaml
+++ b/python/rapidocr_openvino/config.yaml
@@ -7,50 +7,24 @@ Global:
width_height_ratio: 8
Det:
- module_name: ch_ppocr_v3_det
- class_name: TextDetector
- model_path: models/ch_PP-OCRv3_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
- cudnn_conv_algo_search: EXHAUSTIVE
- do_copy_in_default_stream: true
- pre_process:
- DetResizeForTest:
- limit_side_len: 736
- limit_type: min
- NormalizeImage:
- std: [0.229, 0.224, 0.225]
- mean: [0.485, 0.456, 0.406]
- scale: 1./255.
- order: hwc
- ToCHWImage:
- KeepKeys:
- keep_keys: ['image', 'shape']
+ model_path: models/ch_PP-OCRv3_det_infer.onnx
+
+ limit_side_len: 736
+ limit_type: min
- post_process:
- thresh: 0.3
- box_thresh: 0.5
- max_candidates: 1000
- unclip_ratio: 1.6
- use_dilation: true
- score_mode: fast
+ thresh: 0.3
+ box_thresh: 0.5
+ max_candidates: 1000
+ unclip_ratio: 1.6
+ use_dilation: true
+ score_mode: fast
Cls:
- module_name: ch_ppocr_v2_cls
- class_name: TextClassifier
- model_path: models/ch_ppocr_mobile_v2.0_cls_infer.onnx
-
use_cuda: false
- CUDAExecutionProvider:
- device_id: 0
- arena_extend_strategy: kNextPowerOfTwo
- cudnn_conv_algo_search: EXHAUSTIVE
- do_copy_in_default_stream: true
+
+ model_path: models/ch_ppocr_mobile_v2.0_cls_infer.onnx
cls_image_shape: [3, 48, 192]
cls_batch_num: 6
@@ -58,16 +32,9 @@ Cls:
label_list: ['0', '180']
Rec:
- module_name: ch_ppocr_v3_rec
- class_name: TextRecognizer
- model_path: models/ch_PP-OCRv3_rec_infer.onnx
-
use_cuda: false
- CUDAExecutionProvider:
- device_id: 0
- arena_extend_strategy: kNextPowerOfTwo
- cudnn_conv_algo_search: EXHAUSTIVE
- do_copy_in_default_stream: true
+
+ model_path: models/ch_PP-OCRv3_rec_infer.onnx
rec_img_shape: [3, 48, 320]
rec_batch_num: 6
diff --git a/python/rapidocr_openvino/rapid_ocr_api.py b/python/rapidocr_openvino/rapid_ocr_api.py
index 0c351b3..faef39c 100644
--- a/python/rapidocr_openvino/rapid_ocr_api.py
+++ b/python/rapidocr_openvino/rapid_ocr_api.py
@@ -4,7 +4,7 @@
import copy
import importlib
from pathlib import Path
-from typing import Union
+from typing import Optional, Union
import cv2
import numpy as np
@@ -18,10 +18,13 @@ root_dir = Path(__file__).resolve().parent
class RapidOCR:
- def __init__(self, **kwargs):
- config_path = str(root_dir / "config.yaml")
+ def __init__(self, config_path: Optional[str] = None, **kwargs):
+ if config_path is None:
+ config_path = str(root_dir / "config.yaml")
+
if not Path(config_path).exists():
raise FileExistsError(f"{config_path} does not exist!")
+
config = read_yaml(config_path)
config = concat_model_path(config)
diff --git a/python/rapidocr_openvino/utils.py b/python/rapidocr_openvino/utils.py
index 3c699b9..3ee117f 100644
--- a/python/rapidocr_openvino/utils.py
+++ b/python/rapidocr_openvino/utils.py
@@ -4,7 +4,7 @@
import argparse
from io import BytesIO
from pathlib import Path
-from typing import Union
+from typing import Dict, List, Union
import cv2
import numpy as np
@@ -194,37 +194,52 @@ class UpdateParameters:
return config
def update_det_params(self, config, det_dict):
- if det_dict:
- det_dict = {k.split("det_")[1]: v for k, v in det_dict.items()}
- if not det_dict["model_path"]:
- det_dict["model_path"] = str(root_dir / config["model_path"])
- config.update(det_dict)
+ if not det_dict:
+ return config
+
+ det_dict = {k.split("det_")[1]: v for k, v in det_dict.items()}
+ model_path = det_dict.get('model_path', None)
+ if not model_path:
+ det_dict["model_path"] = str(root_dir / config["model_path"])
+
+ config.update(det_dict)
return config
def update_cls_params(self, config, cls_dict):
- if cls_dict:
- need_remove_prefix = ["cls_label_list", "cls_model_path"]
- new_cls_dict = {}
- for k, v in cls_dict.items():
- if k in need_remove_prefix:
- k = k.split("cls_")[1]
- new_cls_dict[k] = v
-
- if not new_cls_dict["model_path"]:
- new_cls_dict["model_path"] = str(root_dir / config["model_path"])
- config.update(new_cls_dict)
+ if not cls_dict:
+ return config
+
+ need_remove_prefix = ["cls_label_list", "cls_model_path", "cls_use_cuda"]
+ new_cls_dict = self.remove_prefix(cls_dict, 'cls_', need_remove_prefix)
+
+ model_path = new_cls_dict.get('model_path', None)
+ if model_path:
+ new_cls_dict["model_path"] = str(root_dir / config["model_path"])
+
+ config.update(new_cls_dict)
return config
def update_rec_params(self, config, rec_dict):
- if rec_dict:
- need_remove_prefix = ["rec_model_path"]
- new_rec_dict = {}
- for k, v in rec_dict.items():
- if k in need_remove_prefix:
- k = k.split("rec_")[1]
- new_rec_dict[k] = v
-
- if not new_rec_dict["model_path"]:
- new_rec_dict["model_path"] = str(root_dir / config["model_path"])
- config.update(new_rec_dict)
+ if not rec_dict:
+ return config
+
+ need_remove_prefix = ["rec_model_path", "rec_use_cuda"]
+ new_rec_dict = self.remove_prefix(rec_dict, 'rec_', need_remove_prefix)
+
+ model_path = new_rec_dict.get('model_path', None)
+ if not model_path:
+ new_rec_dict["model_path"] = str(root_dir / config["model_path"])
+
+ config.update(new_rec_dict)
return config
+
+ @staticmethod
+ def remove_prefix(
+ config: Dict[str, str], prefix: str, remove_params: List[str]
+ ) -> Dict[str, str]:
+ new_rec_dict = {}
+ for k, v in config.items():
+ if k in remove_params:
+ k = k.split(prefix)[1]
+ new_rec_dict[k] = v
+ return new_rec_dict