Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: update image block handling to use visual count and enhance area type checks
myhloli4dcd31c07cc3
diff --git a/mineru/backend/analyze.py b/mineru/backend/analyze.pyindex c417e23a..d63a9f6f 100644--- a/mineru/backend/analyze.py+++ b/mineru/backend/analyze.py@@ -72,7 +72,7 @@ OCR_DET_BASE_BATCH_SIZE = 8LAYOUT_TITLE_SPLIT_OVERLAP_THRESHOLD = 0.8IMAGE_BLOCK_CONTAINMENT_THRESHOLD = 0.8IMAGE_BLOCK_LAYOUT_COVERAGE_THRESHOLD = 0.9-IMAGE_BLOCK_LAYOUT_MIN_IMAGE_COUNT = 2+IMAGE_BLOCK_LAYOUT_MIN_VISUAL_COUNT = 2BATCH_RATIO = 2TABLE_TEXT_LINE_OVERLAP_THRESHOLD = 0.5TABLE_TEXT_ORIENTATION_MIN_VALID_LINES = 3@@ -99,6 +99,17 @@ MODEL_JSON_VISUAL_BLOCK_TYPES = {BlockType.TABLE,BlockType.EQUATION,}+LOCAL_LAYOUT_IMAGE_BLOCK_BODY_TYPES = {+ BlockType.IMAGE,+ BlockType.CHART,+}+LOCAL_LAYOUT_IMAGE_BLOCK_AREA_TYPES = {+ *LOCAL_LAYOUT_IMAGE_BLOCK_BODY_TYPES,+ BlockType.IMAGE_CAPTION,+ BlockType.CAPTION,+ BlockType.IMAGE_FOOTNOTE,+ BlockType.FOOTNOTE,+}_INLINE_FORMULA_PATTERN = re.compile(r"\\\((.*?)\\\)")VLM_LAYOUT_LABEL_MAP = {@@ -1483,7 +1494,7 @@ def _supplement_missing_image_block_containers(layout_blocks_list: list[list[dict[str, Any]]],containment_threshold: float = IMAGE_BLOCK_CONTAINMENT_THRESHOLD,coverage_threshold: float = IMAGE_BLOCK_LAYOUT_COVERAGE_THRESHOLD,- min_image_count: int = IMAGE_BLOCK_LAYOUT_MIN_IMAGE_COUNT,+ min_visual_count: int = IMAGE_BLOCK_LAYOUT_MIN_VISUAL_COUNT,) -> None:"""用本地 layout 整图框为 xhigh 结果补充缺失的 image_block 容器。"""if len(model_list) != len(layout_blocks_list):@@ -1541,41 +1552,48 @@ def _supplement_missing_image_block_containers():contained_blocks.append((block_index, block, block_bbox))- contained_images = [+ contained_visuals = [(block_index, block)for block_index, block, _ in contained_blocks- if block.get("type") == BlockType.IMAGE+ if block.get("type") in LOCAL_LAYOUT_IMAGE_BLOCK_BODY_TYPES]- if len(contained_images) < min_image_count:+ if len(contained_visuals) < min_visual_count:continuelayout_area = (layout_bbox[2] - layout_bbox[0]) * (layout_bbox[3] - layout_bbox[1])contained_area = sum((block_bbox[2] - block_bbox[0]) * (block_bbox[3] - block_bbox[1])- for _, _, block_bbox in contained_blocks+ for _, block, block_bbox in contained_blocks+ if block.get("type") in LOCAL_LAYOUT_IMAGE_BLOCK_AREA_TYPES)- if contained_area / layout_area <= coverage_threshold:+ coverage_ratio = contained_area / layout_area+ if coverage_ratio < coverage_threshold and not math.isclose(+ coverage_ratio,+ coverage_threshold,+ rel_tol=0.0,+ abs_tol=1e-12,+ ):continue- image_ids = {id(block) for _, block in contained_images}+ contained_block_ids = {id(block) for _, block, _ in contained_blocks}first_block_index = min(block_index for block_index, _, _ in contained_blocks)candidates.append((- -len(contained_images),+ -len(contained_visuals),layout_area,layout_order,first_block_index,layout_block,- image_ids,+ contained_block_ids,))- claimed_image_ids: set[int] = set()+ claimed_block_ids: set[int] = set()selected_containers: list[tuple[int, dict[str, Any]]] = []- for _, _, _, first_block_index, layout_block, image_ids in sorted(candidates):- if claimed_image_ids.intersection(image_ids):+ for _, _, _, first_block_index, layout_block, block_ids in sorted(candidates):+ if claimed_block_ids.intersection(block_ids):continue- claimed_image_ids.update(image_ids)+ claimed_block_ids.update(block_ids)selected_containers.append((first_block_index,diff --git a/tests/unittest/test_pdf_analyze_visual_blocks.py b/tests/unittest/test_pdf_analyze_visual_blocks.pyindex 2dade4af..142f7909 100644--- a/tests/unittest/test_pdf_analyze_visual_blocks.py+++ b/tests/unittest/test_pdf_analyze_visual_blocks.py@@ -228,6 +228,19 @@ def test_image_block_collapses_contained_blocks_before_visual_crop() -> None:"bbox": [0.1, 0.1, 0.3, 0.2],"content": "remove-contained-caption",}+ contained_text = {+ "type": BlockType.TEXT,+ "bbox": [0.3, 0.3, 0.4, 0.4],+ "content": "remove-contained-text",+ }+ contained_table = {+ "type": BlockType.TABLE,+ "bbox": [0.4, 0.4, 0.5, 0.5],+ }+ contained_equation = {+ "type": BlockType.EQUATION,+ "bbox": [0.5, 0.5, 0.6, 0.6],+ }below_threshold_caption = {"type": BlockType.IMAGE_CAPTION,"bbox": [0.0, 0.0, 1.0, 0.81],@@ -248,6 +261,9 @@ def test_image_block_collapses_contained_blocks_before_visual_crop() -> None:image_block,threshold_image,contained_caption,+ contained_text,+ contained_table,+ contained_equation,below_threshold_caption,invalid_caption,external_caption,@@ -365,23 +381,56 @@ def test_xhigh_layout_image_supplements_missing_container_before_crop() -> None:@pytest.mark.parametrize(- ("image_bboxes", "layout_type", "expected_container_count"),+ ("block_types", "block_bboxes", "layout_type", "expected_container_count"),[- (([0.0, 0.0, 0.46, 1.0], [0.54, 0.0, 1.0, 1.0]), BlockType.IMAGE, 1),- (([0.0, 0.0, 0.45, 1.0], [0.55, 0.0, 1.0, 1.0]), BlockType.IMAGE, 0),- (([0.0, 0.0, 0.95, 1.0],), BlockType.IMAGE, 0),- (([0.0, 0.0, 0.46, 1.0], [0.54, 0.0, 1.0, 1.0]), BlockType.CHART, 0),+ (+ (BlockType.IMAGE, BlockType.IMAGE),+ ([0.0, 0.0, 0.46, 1.0], [0.54, 0.0, 1.0, 1.0]),+ BlockType.IMAGE,+ 1,+ ),+ (+ (BlockType.IMAGE, BlockType.IMAGE),+ ([0.0, 0.0, 0.45, 1.0], [0.55, 0.0, 1.0, 1.0]),+ BlockType.IMAGE,+ 1,+ ),+ (+ (BlockType.CHART, BlockType.CHART),+ ([0.0, 0.0, 0.46, 1.0], [0.54, 0.0, 1.0, 1.0]),+ BlockType.IMAGE,+ 1,+ ),+ (+ (BlockType.IMAGE, BlockType.CHART),+ ([0.0, 0.0, 0.46, 1.0], [0.54, 0.0, 1.0, 1.0]),+ BlockType.IMAGE,+ 1,+ ),+ (+ (BlockType.IMAGE,),+ ([0.0, 0.0, 0.95, 1.0],),+ BlockType.IMAGE,+ 0,+ ),+ (+ (BlockType.IMAGE, BlockType.CHART),+ ([0.0, 0.0, 0.46, 1.0], [0.54, 0.0, 1.0, 1.0]),+ BlockType.CHART,+ 0,+ ),],)-def test_xhigh_layout_image_fallback_requires_image_count_and_strict_coverage(- image_bboxes: tuple[list[float], ...],+def test_xhigh_layout_image_fallback_requires_visual_count_and_coverage(+ block_types: tuple[str, ...],+ block_bboxes: tuple[list[float], ...],layout_type: str,expected_container_count: int,) -> None:- """验证 layout 回退要求至少两个 image,且面积占比必须严格大于 0.9。"""+ """验证回退要求至少两个 image/chart,且白名单面积占比大于等于 0.9。"""page_model_list = [- {"type": BlockType.IMAGE, "bbox": bbox}- for bbox in image_bboxes+ {"type": block_type, "bbox": bbox}+ for block_type, bbox in zip(block_types, block_bboxes)]layout_blocks_list = [[{"type": layout_type, "bbox": [0.0, 0.0, 1.0, 1.0], "angle": 0}]]@@ -393,6 +442,53 @@ def test_xhigh_layout_image_fallback_requires_image_count_and_strict_coverage(assert sum(block.get("type") == "image_block" for block in page_model_list) == expected_container_count+def test_xhigh_layout_image_fallback_uses_whitelist_for_area_but_absorbs_all_types() -> None:+ """验证白名单只负责面积判定,回退成功后仍吸收框内所有类型。"""+ assert analyze.LOCAL_LAYOUT_IMAGE_BLOCK_AREA_TYPES == {+ BlockType.IMAGE,+ BlockType.CHART,+ BlockType.IMAGE_CAPTION,+ BlockType.CAPTION,+ BlockType.IMAGE_FOOTNOTE,+ BlockType.FOOTNOTE,+ }+ layout_blocks_list = [[{"type": BlockType.IMAGE, "bbox": [0.0, 0.0, 1.0, 1.0], "angle": 0}]]+ insufficient_page = [+ {"type": BlockType.IMAGE, "bbox": [0.0, 0.0, 0.4, 1.0]},+ {"type": BlockType.CHART, "bbox": [0.4, 0.0, 0.8, 1.0]},+ {"type": BlockType.TEXT, "bbox": [0.8, 0.0, 0.9, 1.0]},+ {"type": BlockType.CHART_CAPTION, "bbox": [0.9, 0.0, 1.0, 1.0]},+ ]++ analyze._supplement_missing_image_block_containers(+ [insufficient_page],+ layout_blocks_list,+ )++ assert all(block.get("type") != "image_block" for block in insufficient_page)++ external_text = {"type": BlockType.TEXT, "bbox": [1.1, 0.0, 1.2, 0.1], "content": "keep-external"}+ qualifying_page = [+ {"type": BlockType.IMAGE, "bbox": [0.0, 0.0, 0.4, 1.0]},+ {"type": BlockType.CHART, "bbox": [0.4, 0.0, 0.8, 1.0]},+ {"type": BlockType.CAPTION, "bbox": [0.8, 0.0, 0.9, 1.0]},+ {"type": BlockType.TEXT, "bbox": [0.1, 0.1, 0.2, 0.2]},+ {"type": BlockType.TABLE, "bbox": [0.2, 0.2, 0.3, 0.3]},+ {"type": BlockType.EQUATION, "bbox": [0.3, 0.3, 0.4, 0.4]},+ external_text,+ ]++ analyze._supplement_missing_image_block_containers(+ [qualifying_page],+ layout_blocks_list,+ )+ analyze._collapse_image_blocks(qualifying_page)++ assert len(qualifying_page) == 2+ assert qualifying_page[0]["type"] == BlockType.IMAGE+ assert qualifying_page[1] is external_text++def test_xhigh_layout_image_fallback_does_not_duplicate_existing_or_overlapping_containers() -> None:"""验证已有 VLM 容器和竞争 layout 框不会重复认领相同 image。"""existing_image_block = {