Skip to content

Commit efcffba

Browse files
committed
refactor(ocr): replace **kwargs with explicit params and add type annotations/docstring for ocr()
1 parent 745406a commit efcffba

1 file changed

Lines changed: 58 additions & 4 deletions

File tree

src/onnxocr/onnx_paddleocr.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import argparse
22
import time
33
from pathlib import Path
4+
from typing import Any
5+
6+
from cv2.typing import MatLike
47

58
from onnxocr.logger import get_logger
69
from onnxocr.predict_system import TextSystem
@@ -75,26 +78,77 @@ class ONNXPaddleOcr(TextSystem):
7578
https://onnxruntime.ai/docs/reference/compatibility.html
7679
"""
7780

78-
def __init__(self, **kwargs):
81+
def __init__(
82+
self,
83+
use_gpu: bool = False,
84+
det_model_dir: str | None = None,
85+
rec_model_dir: str | None = None,
86+
cls_model_dir: str | None = None,
87+
rec_char_dict_path: str | None = None,
88+
vis_font_path: str | None = None,
89+
use_angle_cls: bool = False,
90+
det_limit_side_len: float = 960.0,
91+
ocr_model_size: str | None = None,
92+
ocr_model_name: str | None = None,
93+
) -> None:
7994
# 默认参数
8095
parser = init_args()
8196
inference_args_dict = {}
8297
for action in parser._actions:
8398
inference_args_dict[action.dest] = action.default
8499
params = argparse.Namespace(**inference_args_dict)
85100

86-
model_defaults = _build_ppocrv6_defaults(kwargs)
101+
kwargs = {
102+
"use_gpu": use_gpu,
103+
"det_model_dir": det_model_dir,
104+
"rec_model_dir": rec_model_dir,
105+
"cls_model_dir": cls_model_dir,
106+
"rec_char_dict_path": rec_char_dict_path,
107+
"vis_font_path": vis_font_path,
108+
"use_angle_cls": use_angle_cls,
109+
"det_limit_side_len": det_limit_side_len,
110+
"ocr_model_size": ocr_model_size,
111+
"ocr_model_name": ocr_model_name,
112+
}
113+
# 过滤掉 None 值,避免覆盖默认行为
114+
filtered_kwargs = {k: v for k, v in kwargs.items() if v is not None}
115+
116+
model_defaults = _build_ppocrv6_defaults(filtered_kwargs)
87117
params.rec_image_shape = "3, 48, 320"
88118

89119
# 根据传入的参数覆盖更新默认参数
90120
params.__dict__.update(model_defaults)
91-
params.__dict__.update(**kwargs)
121+
params.__dict__.update(**filtered_kwargs)
92122

93123
# 初始化模型
94124
super().__init__(params)
95125
log.info("OCR model initialized: det=True, cls={}, rec=True", self.use_angle_cls)
96126

97-
def ocr(self, img, det=True, rec=True, cls=True) -> list:
127+
def ocr(
128+
self,
129+
img: MatLike | list[MatLike],
130+
det: bool = True,
131+
rec: bool = True,
132+
cls: bool = True
133+
) -> list[Any]:
134+
"""对输入图像进行文字检测、方向分类及文本识别。
135+
136+
Args:
137+
img: 待识别的图像,可以是单张图像 (MatLike) 或图像列表 (List[MatLike])。
138+
det: 是否进行文字检测。若为 True,会先检测出所有文字区域的包围框。
139+
rec: 是否进行文字识别。若为 True,会对文字区域进行文本内容识别。
140+
cls: 是否进行方向角度分类校正。若为 True 且初始化时启用了角度分类模型,会校正文字方向。
141+
142+
Returns:
143+
根据参数组合,返回不同层级的嵌套列表:
144+
1. det=True, rec=True (默认):
145+
返回 [[[box, (text_result, score)], ...]]
146+
其中 box 是四点坐标列表 [[x1, y1], [x2, y2], [x3, y3], [x4, y4]],text_result 是识别的文本,score 是置信度。
147+
2. det=True, rec=False:
148+
返回 [[box, box, ...]],仅包含检测到的所有包围框坐标。
149+
3. det=False, rec=True (或 det=False, rec=False):
150+
返回识别结果列表或分类结果列表。
151+
"""
98152
if cls is True and self.use_angle_cls is False:
99153
log.warning(
100154
"Since the angle classifier is not initialized, the angle classifier will not be used during the forward process"

0 commit comments

Comments
 (0)