Skip to content

Commit d2e9e6b

Browse files
committed
refacter: optimize the parameter update method
1 parent 3be5925 commit d2e9e6b

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

python/rapidocr/main.py

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ def __call__(
9696
unclip_ratio: Optional[float] = None,
9797
) -> Union[TextDetOutput, TextClsOutput, TextRecOutput, RapidOCROutput]:
9898
self.update_params(
99-
use_det,
100-
use_cls,
101-
use_rec,
102-
return_word_box,
103-
return_single_char_box,
104-
text_score,
105-
box_thresh,
106-
unclip_ratio,
99+
use_det=use_det,
100+
use_cls=use_cls,
101+
use_rec=use_rec,
102+
return_word_box=return_word_box,
103+
return_single_char_box=return_single_char_box,
104+
text_score=text_score,
105+
box_thresh=box_thresh,
106+
unclip_ratio=unclip_ratio,
107107
)
108108

109109
ori_img = self.load_img(img_content)
@@ -255,34 +255,31 @@ def calc_word_boxes(
255255
origin_words.append(tuple(origin_words_item))
256256
return tuple(origin_words)
257257

258-
def update_params(
259-
self,
260-
use_det: Optional[bool] = None,
261-
use_cls: Optional[bool] = None,
262-
use_rec: Optional[bool] = None,
263-
return_word_box: Optional[bool] = None,
264-
return_single_char_box: Optional[bool] = None,
265-
text_score: Optional[float] = None,
266-
box_thresh: Optional[float] = None,
267-
unclip_ratio: Optional[float] = None,
268-
):
269-
if use_det is not None:
270-
self.use_det = use_det
271-
if use_cls is not None:
272-
self.use_cls = use_cls
273-
if use_rec is not None:
274-
self.use_rec = use_rec
275-
276-
if return_word_box is not None:
277-
self.return_word_box = return_word_box
278-
if return_single_char_box is not None:
279-
self.return_single_char_box = return_single_char_box
280-
if text_score is not None:
281-
self.text_score = text_score
282-
if box_thresh is not None:
283-
self.text_det.postprocess_op.box_thresh = box_thresh
284-
if unclip_ratio is not None:
285-
self.text_det.postprocess_op.unclip_ratio = unclip_ratio
258+
def update_params(self, **kwargs):
259+
param_map = {
260+
"use_det": ("use_det",),
261+
"use_cls": ("use_cls",),
262+
"use_rec": ("use_rec",),
263+
"return_word_box": ("return_word_box",),
264+
"return_single_char_box": ("return_single_char_box",),
265+
"text_score": ("text_score",),
266+
"box_thresh": ("text_det", "postprocess_op", "box_thresh"),
267+
"unclip_ratio": ("text_det", "postprocess_op", "unclip_ratio"),
268+
}
269+
270+
for key, value in kwargs.items():
271+
if value is None:
272+
continue
273+
274+
path = param_map.get(key)
275+
if not path:
276+
raise ValueError(f"Unknown parameter: {key}")
277+
278+
obj = self
279+
for attr in path[:-1]:
280+
obj = getattr(obj, attr)
281+
282+
setattr(obj, path[-1], value)
286283

287284
def preprocess_img(self, ori_img: np.ndarray) -> Tuple[np.ndarray, Dict[str, Any]]:
288285
op_record = {}

0 commit comments

Comments
 (0)