Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix(ocr): skip degenerate text crops before recognition

myhloli0dbefb400eaf
diff --git a/mineru/model/ocr/image.py b/mineru/model/ocr/image.py
index 7b5cf03d..0f3e64e5 100644
--- a/mineru/model/ocr/image.py
+++ b/mineru/model/ocr/image.py
@@ -145,6 +145,11 @@ def resize_text_recognition_image(
def get_rotate_crop_image_for_text_rec(img: np.ndarray, points: np.ndarray) -> np.ndarray | None:
"""裁剪四边形文本区域并应用文字识别方向规则。"""
+ # 对边长度截断不足一个像素的退化框没有可识别裁图,避免送入共享 warp 的零尺寸目标。
+ crop_width = max(np.linalg.norm(points[0] - points[1]), np.linalg.norm(points[2] - points[3]))
+ crop_height = max(np.linalg.norm(points[0] - points[3]), np.linalg.norm(points[1] - points[2]))
+ if crop_width < 1 or crop_height < 1:
+ return None
return rotate_vertical_crop_if_needed(get_rotate_crop_image(img, points))
diff --git a/mineru/model/ocr/pytorch_paddle.py b/mineru/model/ocr/pytorch_paddle.py
index 5d214c4f..836be732 100644
--- a/mineru/model/ocr/pytorch_paddle.py
+++ b/mineru/model/ocr/pytorch_paddle.py
@@ -228,7 +228,7 @@ class PytorchPaddleOCR(TextSystem):
def __call__(
self, img: np.ndarray | None, mfd_res: list[dict[str, Any]] | None = None
) -> tuple[list[np.ndarray] | None, list[tuple[str, float]] | None]:
- """执行检测与识别,跳过无效印章裁片时同步移除检测框,避免文字错配。"""
+ """执行检测与识别,跳过无效裁片时同步移除检测框,避免文字错配。"""
if img is None:
logger.debug("no valid image provided")
return None, None
@@ -262,10 +262,17 @@ class PytorchPaddleOCR(TextSystem):
# Standard text OCR rotates tall crops before recognition.
# Seal OCR keeps its dedicated poly-crop path above.
+ crop_boxes = []
for bno in range(len(dt_boxes)):
tmp_box = copy.deepcopy(dt_boxes[bno])
img_crop = get_rotate_crop_image_for_text_rec(ori_im, tmp_box)
+ if img_crop is None:
+ continue
img_crop_list.append(img_crop)
+ crop_boxes.append(dt_boxes[bno])
+ dt_boxes = crop_boxes
+ if not img_crop_list:
+ return [], []
rec_res, elapse = self.text_recognizer(img_crop_list)
# logger.debug("rec_res num : {}, elapsed : {}".format(len(rec_res), elapse))
diff --git a/mineru/model/ocr/results.py b/mineru/model/ocr/results.py
index 512ecffe..fc4045d7 100644
--- a/mineru/model/ocr/results.py
+++ b/mineru/model/ocr/results.py
@@ -60,11 +60,16 @@ def get_ocr_result_list(
else:
p1, p2, p3, p4 = box_ocr_res
text, score = "", 1
+ # 公式遮罩拆框可能产生零宽或窄残片;沿用既有过滤条件,先过滤再裁图。
+ if (p3[0] - p1[0]) < OcrConfidence.min_width:
+ continue
if ocr_enable:
if ori_im is None:
ori_im = bgr_image.copy()
tmp_box = copy.deepcopy(np.array([p1, p2, p3, p4]).astype("float32"))
img_crop = get_rotate_crop_image_for_text_rec(ori_im, tmp_box)
+ if img_crop is None:
+ continue
need_ocr_rec = True
poly = [p1, p2, p3, p4]
diff --git a/tests/unittest/test_ocr_text_crop_degenerate.py b/tests/unittest/test_ocr_text_crop_degenerate.py
new file mode 100644
index 00000000..c5a0e5cc
--- /dev/null
+++ b/tests/unittest/test_ocr_text_crop_degenerate.py
@@ -0,0 +1,63 @@
+"""正文 OCR 在公式遮罩拆框之后的零尺寸裁图回归。"""
+
+from __future__ import annotations
+
+from unittest.mock import Mock
+
+import numpy as np
+import pytest
+
+from mineru.model.ocr.geometry import update_det_boxes
+from mineru.model.ocr.image import get_rotate_crop_image_for_text_rec
+from mineru.model.ocr.results import get_ocr_result_list
+
+
+ "points",
+ (
+ [[4, 4], [4, 4], [4, 12], [4, 12]],
+ [[4, 4], [16, 4], [16, 4], [4, 4]],
+ [[4, 4], [16, 6], [16, 6.5], [4, 4.5]],
+ ),
+)
+def test_zero_pixel_text_crop_is_discarded(points: list[list[float]]) -> None:
+ """零宽、零高及倾斜亚像素高度不能进入透视采样或后续 OCR rec。"""
+ image = np.zeros((24, 24, 3), dtype=np.uint8)
+ assert get_rotate_crop_image_for_text_rec(image, np.asarray(points, dtype=np.float32)) is None
+ assert get_ocr_result_list([points], [0, 0, 0, 0, 24, 24, 24, 24], True, image) == []
+
+
+def test_formula_split_zero_width_fragment_preserves_valid_text() -> None:
+ """公式遮罩留下闭区间端点时丢弃零宽残片,合法正文的坐标和裁图保持原样。"""
+ image = np.arange(24 * 24 * 3, dtype=np.uint8).reshape(24, 24, 3)
+ boxes = update_det_boxes(
+ [[[4, 4], [20, 4], [20, 12], [4, 12]]],
+ [{"bbox": [5, 4, 10, 12]}],
+ )
+ assert boxes[0][0][0] == boxes[0][2][0] == 4
+ output = get_ocr_result_list(boxes, [0, 0, 0, 0, 24, 24, 24, 24], True, image)
+ assert len(output) == 1 and output[0]["bbox"] == (11, 4, 20, 12)
+ assert output[0]["text"] == "" and output[0]["score"] == 1.0
+ np.testing.assert_array_equal(output[0]["np_img"], image[4:12, 11:20])
+
+
[email protected]("mask_right", (10, 20))
+def test_torch_ocr_discards_invalid_crops_without_text_misalignment(mask_right: int) -> None:
+ """Torch 整体 OCR 同步过滤零宽框和裁图,全部被过滤时不调用识别器。"""
+ from mineru.model.ocr.pytorch_paddle import PytorchPaddleOCR
+
+ model = object.__new__(PytorchPaddleOCR)
+ model.is_seal = False
+ model.enable_merge_det_boxes = False
+ model.drop_score = 0.5
+ model.text_detector = Mock(return_value=(np.asarray([[[4, 4], [20, 4], [20, 12], [4, 12]]]), 0.0))
+ model.text_recognizer = Mock(return_value=([("valid", 0.99)], 0.0))
+ image = np.zeros((24, 24, 3), dtype=np.uint8)
+ boxes, texts = model(image, mfd_res=[{"bbox": [5, 4, mask_right, 12]}])
+ if mask_right == 20:
+ assert boxes == texts == []
+ model.text_recognizer.assert_not_called()
+ else:
+ assert len(boxes) == 1 and texts == [("valid", 0.99)]
+ np.testing.assert_array_equal(boxes[0], [[11, 4], [20, 4], [20, 12], [11, 12]])
+ assert len(model.text_recognizer.call_args.args[0]) == 1