Skip to content

Commit

Permalink
Update rapidocr_openvino
Browse files Browse the repository at this point in the history
  • Loading branch information
SWHL committed Jul 12, 2023
1 parent bcdd182 commit b55449d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 80 deletions.
27 changes: 25 additions & 2 deletions python/rapidocr_openvino/ch_ppocr_v3_det/text_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,31 @@

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)

Expand Down
61 changes: 14 additions & 47 deletions python/rapidocr_openvino/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,34 @@ 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
cls_thresh: 0.9
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
9 changes: 6 additions & 3 deletions python/rapidocr_openvino/rapid_ocr_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,10 +18,13 @@


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)

Expand Down
71 changes: 43 additions & 28 deletions python/rapidocr_openvino/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -194,37 +194,52 @@ def update_global_params(self, config, global_dict):
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

0 comments on commit b55449d

Please sign in to comment.