Skip to content

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

feat: add use_preprocess_img and use_vertical_padding logic

SWHL3cc33f8d5495
diff --git a/python/rapidocr/config.yaml b/python/rapidocr/config.yaml
index e1200d0..6fc441c 100644
--- a/python/rapidocr/config.yaml
+++ b/python/rapidocr/config.yaml
@@ -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
diff --git a/python/rapidocr/main.py b/python/rapidocr/main.py
index b7db3d1..cdd9a73 100644
--- a/python/rapidocr/main.py
+++ b/python/rapidocr/main.py
@@ -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")
diff --git a/python/tests/test_image_preprocessing.py b/python/tests/test_image_preprocessing.py
new file mode 100644
index 0000000..635e1d8
--- /dev/null
+++ b/python/tests/test_image_preprocessing.py
@@ -0,0 +1,57 @@
+# -*- encoding: utf-8 -*-
+# @Author: SWHL
+# @Contact: [email protected]
+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}