Skip to content

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

feat: add bbox calculation utility and enhance visual gap handling in block processing

myhloli666d58d1f575
diff --git a/mineru/backend/utils/visual_magic_model_utils.py b/mineru/backend/utils/visual_magic_model_utils.py
index eedac7bd..ef95a70c 100644
--- a/mineru/backend/utils/visual_magic_model_utils.py
+++ b/mineru/backend/utils/visual_magic_model_utils.py
@@ -82,9 +82,17 @@ def _block_type(block: BlockLike) -> str:
return str(_get_block_field(block, "type", "") or "")
+def _bbox_for_calculation(bbox: BBox) -> BBox:
+ """返回仅用于计算的 bbox 副本,归一化坐标临时放大一千倍。"""
+ x0, y0, x1, y1 = bbox
+ if all(value <= 1 for value in (x0, y0, x1, y1)):
+ return x0 * 1000, y0 * 1000, x1 * 1000, y1 * 1000
+ return x0, y0, x1, y1
+
+
def _block_bbox(block: BlockLike) -> BBox:
"""读取参与视觉关系判断的 block bbox。"""
- return cast(BBox, _get_block_field(block, "bbox"))
+ return _bbox_for_calculation(cast(BBox, _get_block_field(block, "bbox")))
def _block_line_items(block: BlockLike) -> list[Any]:
@@ -540,7 +548,14 @@ def find_best_visual_parent(
if len(closest_index_candidates) == 1:
return closest_index_candidates[0]
- edge_distances = [(main_block, bbox_distance(child_block.bbox, main_block.bbox)) for main_block in closest_index_candidates]
+ child_bbox = _bbox_for_calculation(child_block.bbox)
+ edge_distances = [
+ (
+ main_block,
+ bbox_distance(child_bbox, _bbox_for_calculation(main_block.bbox)),
+ )
+ for main_block in closest_index_candidates
+ ]
edge_values = [edge_distance for _, edge_distance in edge_distances]
if max(edge_values) - min(edge_values) > 2:
return min(
@@ -563,7 +578,10 @@ def find_best_visual_parent(
return min(
closest_index_candidates,
key=lambda main_block: (
- bbox_center_distance(child_block.bbox, main_block.bbox),
+ bbox_center_distance(
+ child_bbox,
+ _bbox_for_calculation(main_block.bbox),
+ ),
main_block.index,
),
)
@@ -644,8 +662,8 @@ def is_block_outside_visual_gap(between_block: Block, child_block: Block, main_b
def vertical_gap_between_blocks(first_block: Block, second_block: Block) -> tuple[float, float] | None:
"""计算两个块上下分离时的垂直间隔;发生纵向重叠时保持严格阻断。"""
- first_bbox = first_block.bbox
- second_bbox = second_block.bbox
+ first_bbox = _bbox_for_calculation(first_block.bbox)
+ second_bbox = _bbox_for_calculation(second_block.bbox)
if first_bbox[3] <= second_bbox[1]:
return first_bbox[3], second_bbox[1]
if second_bbox[3] <= first_bbox[1]:
@@ -655,6 +673,7 @@ def vertical_gap_between_blocks(first_block: Block, second_block: Block) -> tupl
def is_bbox_intersecting_vertical_gap(bbox: BBox, vertical_gap: tuple[float, float]) -> bool:
"""判断 bbox 是否与视觉父子块之间的垂直间隔相交。"""
+ bbox = _bbox_for_calculation(bbox)
gap_top, gap_bottom = vertical_gap
return bbox[1] < gap_bottom and bbox[3] > gap_top
@@ -666,6 +685,8 @@ def is_bbox_overlapping_visual_relation_block(bbox: BBox, child_bbox: BBox, main
def are_bboxes_overlapping(first_bbox: BBox, second_bbox: BBox) -> bool:
"""判断两个 bbox 是否存在二维相交。"""
+ first_bbox = _bbox_for_calculation(first_bbox)
+ second_bbox = _bbox_for_calculation(second_bbox)
return not (
first_bbox[2] <= second_bbox[0]
or first_bbox[0] >= second_bbox[2]
diff --git a/tests/unittest/test_visual_magic_model_utils.py b/tests/unittest/test_visual_magic_model_utils.py
index 042d44f4..7d8a6fac 100644
--- a/tests/unittest/test_visual_magic_model_utils.py
+++ b/tests/unittest/test_visual_magic_model_utils.py
@@ -2,8 +2,11 @@ from __future__ import annotations
from mineru.backend.utils.visual_magic_model_utils import (
VISUAL_MAIN_TYPES,
+ _bbox_for_calculation,
fallback_inline_caption_fragments,
fallback_leading_table_continuation_captions,
+ find_best_visual_parent,
+ is_block_outside_visual_gap,
)
from mineru.types import Block, BlockType, Line
@@ -13,6 +16,115 @@ def _line_metadata(count: int) -> list[dict[str, list[float]]]:
return [{"bbox": [0.0, float(index), 1.0, float(index + 1)]} for index in range(count)]
+def test_bbox_for_calculation_scales_only_all_unit_range_coordinates() -> None:
+ """验证仅四个坐标均不大于一时生成放大后的计算副本。"""
+ normalized_bbox = (0.123, 0.221, 0.445, 0.556)
+
+ assert _bbox_for_calculation(normalized_bbox) == (123.0, 221.0, 445.0, 556.0)
+ assert normalized_bbox == (0.123, 0.221, 0.445, 0.556)
+ assert _bbox_for_calculation((123, 221, 445, 556)) == (123, 221, 445, 556)
+ assert _bbox_for_calculation((0.123, 0.221, 0.445, 1.0)) == (123.0, 221.0, 445.0, 1000.0)
+ assert _bbox_for_calculation((0.123, 0.221, 0.445, 1.001)) == (0.123, 0.221, 0.445, 1.001)
+
+
+def test_normalized_dict_bbox_matches_scaled_caption_geometry_without_rewrite() -> None:
+ """验证 dict 归一化框与千倍坐标使用相同标题几何且不回写 bbox。"""
+ companion = {
+ "index": 1,
+ "type": BlockType.TEXT,
+ "bbox": [0.02, 0.125, 0.1, 0.145],
+ "content": "unrelated text",
+ "_lines": _line_metadata(1),
+ }
+ normalized_blocks = [
+ {
+ "index": 0,
+ "type": BlockType.CAPTION,
+ "bbox": [0.7, 0.1, 0.95, 0.12],
+ "content": "Table 1",
+ "_lines": _line_metadata(1),
+ },
+ companion,
+ {
+ "index": 2,
+ "type": BlockType.TABLE_BODY,
+ "bbox": [0.7, 0.15, 0.95, 0.8],
+ "content": "",
+ },
+ ]
+ original_bboxes = [list(block["bbox"]) for block in normalized_blocks]
+ scaled_blocks = [
+ {
+ **block,
+ "bbox": [value * 1000 for value in block["bbox"]],
+ }
+ for block in normalized_blocks
+ ]
+
+ fallback_inline_caption_fragments(normalized_blocks, VISUAL_MAIN_TYPES)
+ fallback_inline_caption_fragments(scaled_blocks, VISUAL_MAIN_TYPES)
+
+ assert companion["type"] == BlockType.TEXT
+ assert scaled_blocks[1]["type"] == BlockType.TEXT
+ assert [block["bbox"] for block in normalized_blocks] == original_bboxes
+
+
+def test_normalized_block_bbox_matches_scaled_visual_parent_without_rewrite() -> None:
+ """验证 Block 归一化框与千倍坐标选择同一视觉父块且保留原框。"""
+ previous_table = Block(index=0, type=BlockType.TABLE_BODY, bbox=(0.1, 0.1, 0.4, 0.4))
+ caption = Block(index=1, type=BlockType.CAPTION, bbox=(0.1, 0.42, 0.4, 0.44))
+ following_table = Block(index=2, type=BlockType.TABLE_BODY, bbox=(0.1, 0.8, 0.4, 0.95))
+ normalized_blocks = [previous_table, caption, following_table]
+ original_bboxes = [block.bbox for block in normalized_blocks]
+ scaled_blocks = [
+ Block(
+ index=block.index,
+ type=block.type,
+ bbox=tuple(value * 1000 for value in block.bbox),
+ )
+ for block in normalized_blocks
+ ]
+
+ normalized_parent = find_best_visual_parent(
+ caption,
+ [previous_table, following_table],
+ normalized_blocks,
+ {block.index: position for position, block in enumerate(normalized_blocks)},
+ )
+ scaled_parent = find_best_visual_parent(
+ scaled_blocks[1],
+ [scaled_blocks[0], scaled_blocks[2]],
+ scaled_blocks,
+ {block.index: position for position, block in enumerate(scaled_blocks)},
+ )
+
+ assert normalized_parent is previous_table
+ assert scaled_parent is scaled_blocks[0]
+ assert [block.bbox for block in normalized_blocks] == original_bboxes
+
+
+def test_normalized_block_bbox_matches_scaled_visual_gap_geometry() -> None:
+ """验证归一化框在视觉间隔、相交和重叠判断中与千倍坐标一致。"""
+ child = Block(index=0, type=BlockType.CAPTION, bbox=(0.1, 0.1, 0.4, 0.2))
+ inside_gap = Block(index=1, type=BlockType.TEXT, bbox=(0.8, 0.3, 0.9, 0.4))
+ outside_gap = Block(index=2, type=BlockType.TEXT, bbox=(0.8, 0.8, 0.9, 0.9))
+ main = Block(index=3, type=BlockType.TABLE_BODY, bbox=(0.1, 0.6, 0.4, 0.7))
+ normalized_blocks = [child, inside_gap, outside_gap, main]
+ scaled_blocks = [
+ Block(
+ index=block.index,
+ type=block.type,
+ bbox=tuple(value * 1000 for value in block.bbox),
+ )
+ for block in normalized_blocks
+ ]
+
+ assert is_block_outside_visual_gap(inside_gap, child, main) is False
+ assert is_block_outside_visual_gap(outside_gap, child, main) is True
+ assert is_block_outside_visual_gap(scaled_blocks[1], scaled_blocks[0], scaled_blocks[3]) is False
+ assert is_block_outside_visual_gap(scaled_blocks[2], scaled_blocks[0], scaled_blocks[3]) is True
+
+
def test_dict_inline_caption_fragment_uses_common_fields() -> None:
"""验证 dict 同行标题片段可原地改成通用 caption。"""
companion = {