feat: add use_preprocess_img and use_vertical_padding logic

This commit is contained in:
SWHL authored and GitHub committed 2026-07-21 11:50:49 +08:00
1 parent 83866d6a5c
commit 3cc33f8d54
3 files changed
+69 -7

No files matched your search

+1 -1
View File
@@ -9,7 +9,7 @@ Global:
min_side_len: 30
max_side_len: 2000
use_apply_vertical_padding: true
use_vertical_padding: true
min_height: 30
width_height_ratio: 8
+11 -6
View File
@@ -279,21 +279,26 @@ class RapidOCR:
setattr(obj, path[-1], value)
def preprocess_img(self, ori_img: np.ndarray) -> Tuple[np.ndarray, Dict[str, Any]]:
op_record = {}
if not self.cfg.Global.use_preprocess_img:
return ori_img, {"preprocess": {"ratio_h": 1.0, "ratio_w": 1.0}}
img, ratio_h, ratio_w = resize_image_within_bounds(
ori_img, self.min_side_len, self.max_side_len
)
op_record["preprocess"] = {"ratio_h": ratio_h, "ratio_w": ratio_w}
op_record = {"preprocess": {"ratio_h": ratio_h, "ratio_w": ratio_w}}
return img, op_record
def detect_and_crop(
self, img: np.ndarray, op_record: Dict[str, Any]
) -> Tuple[List[np.ndarray], TextDetOutput]:
img, op_record = apply_vertical_padding(
img, op_record, self.width_height_ratio, self.min_height
)
det_res = self.text_det(img)
if self.cfg.Global.use_vertical_padding:
img, op_record = apply_vertical_padding(
img, op_record, self.width_height_ratio, self.min_height
)
else:
op_record["padding_1"] = {"top": 0, "left": 0}
det_res = self.text_det(img)
if det_res.boxes is None:
raise RapidOCRError("The text detection result is empty")
+57
View File
@@ -0,0 +1,57 @@
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
from types import SimpleNamespace
import numpy as np
import rapidocr.main as main_module
from rapidocr import RapidOCR
from rapidocr.ch_ppocr_det import TextDetOutput
def test_preprocess_img_returns_original_when_disabled(monkeypatch):
engine = RapidOCR.__new__(RapidOCR)
engine.cfg = SimpleNamespace(
Global=SimpleNamespace(use_preprocess_img=False)
)
ori_img = np.zeros((20, 40, 3), dtype=np.uint8)
def fail_if_called(*args, **kwargs):
raise AssertionError("resize_image_within_bounds should not be called")
monkeypatch.setattr(main_module, "resize_image_within_bounds", fail_if_called)
img, op_record = engine.preprocess_img(ori_img)
assert img is ori_img
assert op_record == {"preprocess": {"ratio_h": 1.0, "ratio_w": 1.0}}
def test_detect_and_crop_skips_vertical_padding_when_disabled(monkeypatch):
engine = RapidOCR.__new__(RapidOCR)
engine.cfg = SimpleNamespace(
Global=SimpleNamespace(use_vertical_padding=False)
)
img = np.zeros((20, 40, 3), dtype=np.uint8)
boxes = np.array([[[0, 0], [10, 0], [10, 10], [0, 10]]], dtype=np.float32)
detected_images = []
def fail_if_called(*args, **kwargs):
raise AssertionError("apply_vertical_padding should not be called")
def text_det(det_img):
detected_images.append(det_img)
return TextDetOutput(boxes=boxes)
monkeypatch.setattr(main_module, "apply_vertical_padding", fail_if_called)
engine.text_det = text_det
engine.crop_text_regions = lambda crop_img, det_boxes: [crop_img]
op_record = {}
cropped_imgs, det_res = engine.detect_and_crop(img, op_record)
assert detected_images[0] is img
assert cropped_imgs[0] is img
assert det_res.boxes is boxes
assert op_record["padding_1"] == {"top": 0, "left": 0}