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
fix(layout): bound overlap merging and correct nested annotations
myhloli15840d645ef7
diff --git a/mineru/model/layout/pp_doclayout_v2_base.py b/mineru/model/layout/pp_doclayout_v2_base.pyindex c719dc75..2f7cf92d 100644--- a/mineru/model/layout/pp_doclayout_v2_base.py+++ b/mineru/model/layout/pp_doclayout_v2_base.py@@ -13,6 +13,7 @@ import hashlibimport jsonimport mathimport os+from statistics import medianfrom typing import Dict, List, Optional, Sequence, Tuple, Unionimport numpy as np@@ -364,7 +365,7 @@ class PPDocLayoutV2PostProcessor:boxes: List[Dict],cover_threshold: float = 0.8,) -> List[Dict]:- """过滤落在图、表、印章等视觉主体内部的 figure_title。+ """过滤落在最终图、表、印章等视觉主体内部的标题和视觉脚注。这类块通常是图内的 (a)/(b) 标号,不应作为外部 caption 参与后续视觉分组。"""@@ -374,7 +375,7 @@ class PPDocLayoutV2PostProcessor:filtered_boxes = []for box in boxes:- if box.get("label") != "figure_title":+ if box.get("label") not in {"figure_title", "vision_footnote"}:filtered_boxes.append(box)continue@@ -463,28 +464,108 @@ class PPDocLayoutV2PostProcessor:boxes: List[Dict],iou_threshold: float = 0.9,) -> List[Dict]:+ """按置信度选择重复检测,以冻结坐标判断直接重复并保留所有成员的几何。"""if len(boxes) <= 1:return boxessorted_candidates = sorted(enumerate(boxes),- key=lambda item: (-float(item[1].get("score", 0.0)), item[0]),+ key=lambda item: (+ -float(item[1].get("score", 0.0)),+ -cls._calculate_bbox_area(item[1]["bbox"]),+ item[1]["index"],+ item[0],+ ),)suppressed_indexes = set()- kept_indexes = []+ kept_boxes = []for candidate_pos, (current_index, current_box) in enumerate(sorted_candidates):if current_index in suppressed_indexes:continue- kept_indexes.append(current_index)+ kept_box = {**current_box, "bbox": list(current_box["bbox"])}for other_index, other_box in sorted_candidates[candidate_pos + 1 :]:if other_index in suppressed_indexes:continue+ if cls._is_protected_formula_pair(current_box, other_box):+ continue+ # 始终比较原始 current_box,不能让扩大后的并集继续吞并下一段检测。if cls._calculate_iou(current_box["bbox"], other_box["bbox"]) > iou_threshold:suppressed_indexes.add(other_index)+ kept_box["bbox"] = cls._union_bbox(kept_box["bbox"], other_box["bbox"])+ kept_boxes.append((current_index, kept_box))++ return [box for _, box in sorted(kept_boxes, key=lambda item: (item[1]["index"], item[0]))]++ @classmethod+ def _is_protected_formula_pair(cls, left: Dict, right: Dict) -> bool:+ """保护公式与非公式父块、公式编号与其他块,避免通用去重抢先删除语义成员。"""+ if cls._is_formula_number_box(left) != cls._is_formula_number_box(right):+ return True+ return cls._is_formula_box(left) != cls._is_formula_box(right)- kept_indexes.sort()- return [boxes[index] for index in kept_indexes]+ @classmethod+ def _can_merge_layout_pair(cls, left: Dict, right: Dict) -> bool:+ """保留既有视觉跨类别共存规则,公式与父块独立保留,公式之间允许合并。"""+ if cls._is_protected_formula_pair(left, right):+ return False+ labels = {left["label"], right["label"]}+ if labels & cls.VISUAL_BODY_LABELS and len(labels) > 1:+ return "table" in labels and not labels <= cls.VISUAL_BODY_LABELS+ return True++ @classmethod+ def _merge_overlapping_boxes(+ cls,+ boxes: List[Dict],+ *,+ merge_non_formulas: bool,+ overlap_threshold: float = 0.7,+ area_limit: float = 1.5,+ ) -> tuple[List[Dict], dict[int, list[int]]]:+ """以冻结的大框为锚执行有界合并,返回独立存储的子图原始序号。"""+ areas = [cls._calculate_bbox_area(box["bbox"]) for box in boxes]+ anchor_order = sorted(+ range(len(boxes)),+ key=lambda index: (-areas[index], -float(boxes[index].get("score", 0.0)), boxes[index]["index"], index),+ )+ candidate_order = sorted(+ range(len(boxes)),+ key=lambda index: (-float(boxes[index].get("score", 0.0)), -areas[index], boxes[index]["index"], index),+ )+ consumed = set()+ kept_boxes = []+ image_children: dict[int, list[int]] = {}+ for anchor_index in anchor_order:+ if anchor_index in consumed:+ continue+ anchor = boxes[anchor_index]+ kept_box = {**anchor, "bbox": list(anchor["bbox"])}+ consumed.add(anchor_index)+ child_orders = []+ for candidate_index in candidate_order:+ if candidate_index in consumed:+ continue+ candidate = boxes[candidate_index]+ if not merge_non_formulas and not (cls._is_formula_box(anchor) and cls._is_formula_box(candidate)):+ continue+ if not cls._can_merge_layout_pair(anchor, candidate):+ continue+ if cls._calculate_overlap_ratio(anchor["bbox"], candidate["bbox"]) <= overlap_threshold:+ continue+ union_bbox = cls._union_bbox(kept_box["bbox"], candidate["bbox"])+ if cls._calculate_bbox_area(union_bbox) > areas[anchor_index] * area_limit:+ continue+ # 类型和置信度始终属于大框;并集不回流到锚点判断或面积上限。+ kept_box["bbox"] = union_bbox+ consumed.add(candidate_index)+ if anchor["label"] == candidate["label"] == "image" and areas[candidate_index] < areas[anchor_index]:+ child_orders.append(candidate["index"])+ if child_orders:+ image_children[id(kept_box)] = child_orders+ kept_boxes.append((anchor_index, kept_box))+ ordered_boxes = [box for _, box in sorted(kept_boxes, key=lambda item: (item[1]["index"], item[0]))]+ return ordered_boxes, image_children@classmethoddef _filter_nested_text_boxes(cls, boxes: List[Dict]) -> List[Dict]:@@ -517,54 +598,6 @@ class PPDocLayoutV2PostProcessor:parents.append(box)return [box for index, box in enumerate(boxes) if index not in dropped_indexes]- @classmethod- def _merge_nested_formula_boxes(- cls,- boxes: List[Dict],- overlap_threshold: float = 0.7,- ) -> List[Dict]:- if len(boxes) <= 1:- return boxes-- changed = True- while changed:- changed = False- formula_indexes = [index for index, box in enumerate(boxes) if cls._is_formula_box(box)]- for formula_pos, left_index in enumerate(formula_indexes):- for right_index in formula_indexes[formula_pos + 1 :]:- left_box = boxes[left_index]- right_box = boxes[right_index]- if cls._calculate_overlap_ratio(left_box["bbox"], right_box["bbox"]) < overlap_threshold:- continue-- left_area = cls._calculate_bbox_area(left_box["bbox"])- right_area = cls._calculate_bbox_area(right_box["bbox"])- if left_area > right_area:- keep_index, drop_index = left_index, right_index- elif right_area > left_area:- keep_index, drop_index = right_index, left_index- else:- left_score = float(left_box.get("score", 0.0))- right_score = float(right_box.get("score", 0.0))- keep_index, drop_index = (- (left_index, right_index) if left_score >= right_score else (right_index, left_index)- )-- keep_box = boxes[keep_index]- drop_box = boxes[drop_index]- keep_box["bbox"] = cls._union_bbox(keep_box["bbox"], drop_box["bbox"])- keep_box["score"] = round(- max(float(keep_box.get("score", 0.0)), float(drop_box.get("score", 0.0))),- 4,- )- del boxes[drop_index]- changed = True- break- if changed:- break-- return boxes-@classmethoddef _relabel_formula_boxes(cls,@@ -727,95 +760,49 @@ class PPDocLayoutV2PostProcessor:cls,boxes: List[Dict],image_size: Optional[Tuple[int, int]] = None,+ *,+ use_paddlex_filter_boxes: bool = True,) -> List[Dict]:- """统一两种后端的公式保护、边界改标与最终文本框去重。"""+ """统一执行公式保护、分数去重、有界合并、边界清理和子图中位数排序。"""processed_boxes = [{**box, "bbox": list(box["bbox"])} for box in boxes]- processed_boxes = cls._deduplicate_boxes_by_iou(processed_boxes, iou_threshold=0.9)- processed_boxes = cls._merge_nested_formula_boxes(processed_boxes, overlap_threshold=0.7)+ original_inline_ids = {id(box) for box in processed_boxes if cls._is_inline_formula_box(box)}+ # 在任何通用抑制之前修正公式身份;最终再依据实际存活的父块复核。processed_boxes = cls._relabel_formula_boxes(processed_boxes, overlap_threshold=0.7)- processed_boxes = cls._filter_formula_boxes_inside_page_marginals(+ if use_paddlex_filter_boxes:+ processed_boxes = [+ box+ for box in processed_boxes+ if not cls._is_reference_box(box)+ and (+ id(box) in original_inline_ids+ or cls._is_inline_formula_box(box)+ or (box["bbox"][2] - box["bbox"][0] >= 6 and box["bbox"][3] - box["bbox"][1] >= 6)+ )+ ]+ processed_boxes = cls._deduplicate_boxes_by_iou(processed_boxes, iou_threshold=0.9)+ processed_boxes, image_children = cls._merge_overlapping_boxes(processed_boxes,- cover_threshold=0.8,+ merge_non_formulas=use_paddlex_filter_boxes,)processed_boxes = cls._relabel_header_footer_boundary_blocks(processed_boxes,image_size=image_size,)+ processed_boxes = cls._filter_nested_text_boxes(processed_boxes)processed_boxes = cls._filter_internal_visual_caption_boxes(processed_boxes,cover_threshold=0.8,)- processed_boxes = cls._filter_nested_text_boxes(processed_boxes)- return cls._renumber_indices(processed_boxes)-- @classmethod- def _apply_paddlex_filter_boxes(- cls,- boxes: List[Dict],- drop_inline_formula: bool = True,- ) -> List[Dict]:- """沿用既有过滤判定,仅让吞并子图的 image 大框继承最早子图位置。"""- filtered_boxes = [dict(box) for box in boxes if not cls._is_reference_box(box)]- dropped_indexes = set()- image_positions: dict[int, int] = {}-- for i in range(len(filtered_boxes)):- if i in dropped_indexes:- continue- x1, y1, x2, y2 = filtered_boxes[i]["bbox"]- width = float(x2) - float(x1)- height = float(y2) - float(y1)- if (width < 6.0 or height < 6.0) and (drop_inline_formula or not cls._is_inline_formula_box(filtered_boxes[i])):- dropped_indexes.add(i)- continue-- for j in range(i + 1, len(filtered_boxes)):- if i in dropped_indexes or j in dropped_indexes:- continue-- if not drop_inline_formula and (- cls._is_inline_formula_box(filtered_boxes[i]) or cls._is_inline_formula_box(filtered_boxes[j])- ):- continue-- overlap_ratio = cls._calculate_overlap_ratio(- filtered_boxes[i]["bbox"],- filtered_boxes[j]["bbox"],- )- if drop_inline_formula and (- cls._is_inline_formula_box(filtered_boxes[i]) or cls._is_inline_formula_box(filtered_boxes[j])- ):- if overlap_ratio > 0.5:- if cls._is_inline_formula_box(filtered_boxes[i]):- dropped_indexes.add(i)- if cls._is_inline_formula_box(filtered_boxes[j]):- dropped_indexes.add(j)- continue-- if overlap_ratio > 0.7:- box_area_i = cls._calculate_bbox_area(filtered_boxes[i]["bbox"])- box_area_j = cls._calculate_bbox_area(filtered_boxes[j]["bbox"])- labels = {filtered_boxes[i]["label"], filtered_boxes[j]["label"]}- if labels & {"image", "table", "seal", "chart"} and len(labels) > 1:- if "table" not in labels or labels <= {"table", "image", "seal", "chart"}:- continue- if box_area_i >= box_area_j:- keep_index, drop_index = i, j- else:- keep_index, drop_index = j, i- dropped_indexes.add(drop_index)- if labels == {"image"} and box_area_i != box_area_j:- # 不计大图原位置;子图此前吞并过其他图时,继续传递其已继承的位置。- child_position = image_positions.get(drop_index, drop_index)- image_positions[keep_index] = min(image_positions.get(keep_index, child_position), child_position)-- # 判定全部完成后才重排,避免位置调整改变后续配对、吞并及保留框选择。- kept_indexes = sorted(- (index for index in range(len(filtered_boxes)) if index not in dropped_indexes),- key=lambda index: image_positions.get(index, index),+ processed_boxes = cls._filter_formula_boxes_inside_page_marginals(processed_boxes, cover_threshold=0.8)+ processed_boxes = cls._relabel_formula_boxes(processed_boxes, overlap_threshold=0.7)+ # 只调整最终仍为 image 的主体,不把临时来源信息写入公开检测字典。+ processed_boxes.sort(+ key=lambda box: (+ median(image_children[id(box)]) if box["label"] == "image" and id(box) in image_children else box["index"],+ box["index"],+ ))- kept_boxes = [filtered_boxes[index] for index in kept_indexes]- return cls._renumber_indices(kept_boxes)+ return cls._renumber_indices(processed_boxes)# ------------------------------------------------------------------# 可视化diff --git a/mineru/model/layout/pp_doclayout_v2_onnx.py b/mineru/model/layout/pp_doclayout_v2_onnx.pyindex 720fa5f4..d85adff5 100644--- a/mineru/model/layout/pp_doclayout_v2_onnx.py+++ b/mineru/model/layout/pp_doclayout_v2_onnx.py@@ -223,9 +223,9 @@ class PPDocLayoutV2LayoutModelONNX(PPDocLayoutV2PostProcessor):predictions = self._run_session(pixels, sizes)for prediction, image_size in zip(predictions, sizes):layout_res = self._parse_prediction(prediction, image_size)- if use_filter:- layout_res = self._apply_paddlex_filter_boxes(layout_res, drop_inline_formula=False)- results.append(self._apply_layout_post_process(layout_res, image_size=image_size))+ results.append(+ self._apply_layout_post_process(layout_res, image_size=image_size, use_paddlex_filter_boxes=use_filter)+ )pbar.update(len(prepared))return resultsdiff --git a/mineru/model/layout/pp_doclayoutv2.py b/mineru/model/layout/pp_doclayoutv2.pyindex 7d5b3f7a..dac6bbb9 100644--- a/mineru/model/layout/pp_doclayoutv2.py+++ b/mineru/model/layout/pp_doclayoutv2.py@@ -233,9 +233,9 @@ class PPDocLayoutV2LayoutModel(PPDocLayoutV2PostProcessor):predictions = self._post_process_object_detection(outputs, target_sizes)for prediction, image_size in zip(predictions, target_sizes):layout_res = self._parse_prediction(prediction, image_size)- if use_paddlex_filter_boxes:- layout_res = self._apply_paddlex_filter_boxes(layout_res, drop_inline_formula=False)- layout_res = self._apply_layout_post_process(layout_res, image_size=image_size)+ layout_res = self._apply_layout_post_process(+ layout_res, image_size=image_size, use_paddlex_filter_boxes=use_paddlex_filter_boxes+ )results.append(layout_res)pbar.update(len(batch_images))return resultsdiff --git a/tests/fixtures/pp_doclayoutv2/smac_1045h.json b/tests/fixtures/pp_doclayoutv2/smac_1045h.jsonnew file mode 100644index 00000000..055c3a04--- /dev/null+++ b/tests/fixtures/pp_doclayoutv2/smac_1045h.json@@ -0,0 +1,960 @@+{+ "image_size": [+ 2292,+ 1701+ ],+ "backends": {+ "torch": [+ {+ "cls_id": 2,+ "label": "aside_text",+ "score": 0.7702,+ "bbox": [+ 48,+ 762,+ 83,+ 1541+ ],+ "index": 1+ },+ {+ "cls_id": 13,+ "label": "header_image",+ "score": 0.9543,+ "bbox": [+ 174,+ 129,+ 637,+ 217+ ],+ "index": 2+ },+ {+ "cls_id": 12,+ "label": "header",+ "score": 0.9447,+ "bbox": [+ 1313,+ 177,+ 1526,+ 210+ ],+ "index": 3+ },+ {+ "cls_id": 17,+ "label": "paragraph_title",+ "score": 0.7797,+ "bbox": [+ 176,+ 286,+ 448,+ 334+ ],+ "index": 4+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.6647,+ "bbox": [+ 270,+ 374,+ 684,+ 664+ ],+ "index": 5+ },+ {+ "cls_id": 17,+ "label": "paragraph_title",+ "score": 0.9365,+ "bbox": [+ 803,+ 425,+ 949,+ 463+ ],+ "index": 6+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.9505,+ "bbox": [+ 801,+ 493,+ 1251,+ 528+ ],+ "index": 7+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.9428,+ "bbox": [+ 802,+ 533,+ 1124,+ 567+ ],+ "index": 8+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.951,+ "bbox": [+ 802,+ 572,+ 1360,+ 608+ ],+ "index": 9+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.9446,+ "bbox": [+ 802,+ 612,+ 1151,+ 645+ ],+ "index": 10+ },+ {+ "cls_id": 17,+ "label": "paragraph_title",+ "score": 0.4474,+ "bbox": [+ 206,+ 742,+ 488,+ 776+ ],+ "index": 11+ },+ {+ "cls_id": 21,+ "label": "table",+ "score": 0.9862,+ "bbox": [+ 180,+ 768,+ 1522,+ 1212+ ],+ "index": 12+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.4642,+ "bbox": [+ 880,+ 861,+ 921,+ 890+ ],+ "index": 13+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5856,+ "bbox": [+ 887,+ 897,+ 917,+ 927+ ],+ "index": 14+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5089,+ "bbox": [+ 880,+ 947,+ 921,+ 979+ ],+ "index": 15+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5998,+ "bbox": [+ 884,+ 1002,+ 923,+ 1032+ ],+ "index": 16+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.6076,+ "bbox": [+ 885,+ 1039,+ 921,+ 1068+ ],+ "index": 17+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5503,+ "bbox": [+ 886,+ 1071,+ 918,+ 1101+ ],+ "index": 18+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5623,+ "bbox": [+ 888,+ 1108,+ 916,+ 1138+ ],+ "index": 19+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5054,+ "bbox": [+ 887,+ 1143,+ 916,+ 1172+ ],+ "index": 20+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.6253,+ "bbox": [+ 881,+ 1178,+ 915,+ 1209+ ],+ "index": 21+ },+ {+ "cls_id": 21,+ "label": "table",+ "score": 0.952,+ "bbox": [+ 181,+ 1241,+ 1522,+ 1356+ ],+ "index": 22+ },+ {+ "cls_id": 24,+ "label": "vision_footnote",+ "score": 0.8721,+ "bbox": [+ 539,+ 1382,+ 1529,+ 1416+ ],+ "index": 23+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.5808,+ "bbox": [+ 207,+ 1417,+ 350,+ 1451+ ],+ "index": 24+ },+ {+ "cls_id": 7,+ "label": "figure_title",+ "score": 0.7478,+ "bbox": [+ 177,+ 1517,+ 401,+ 1551+ ],+ "index": 25+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7312,+ "bbox": [+ 177,+ 1622,+ 489,+ 1774+ ],+ "index": 26+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.6664,+ "bbox": [+ 499,+ 1567,+ 832,+ 1796+ ],+ "index": 27+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.5133,+ "bbox": [+ 173,+ 1523,+ 1537,+ 2058+ ],+ "index": 28+ },+ {+ "cls_id": 24,+ "label": "vision_footnote",+ "score": 0.5182,+ "bbox": [+ 580,+ 1826,+ 819,+ 1863+ ],+ "index": 29+ },+ {+ "cls_id": 7,+ "label": "figure_title",+ "score": 0.5161,+ "bbox": [+ 891,+ 1516,+ 1117,+ 1552+ ],+ "index": 30+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7146,+ "bbox": [+ 867,+ 1603,+ 1223,+ 1783+ ],+ "index": 31+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7021,+ "bbox": [+ 1238,+ 1610,+ 1539,+ 1789+ ],+ "index": 32+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.6768,+ "bbox": [+ 179,+ 1928,+ 492,+ 2029+ ],+ "index": 33+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7486,+ "bbox": [+ 538,+ 1883,+ 828,+ 2039+ ],+ "index": 34+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.62,+ "bbox": [+ 868,+ 1840,+ 1175,+ 2031+ ],+ "index": 35+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7731,+ "bbox": [+ 1260,+ 1838,+ 1519,+ 2059+ ],+ "index": 36+ },+ {+ "cls_id": 24,+ "label": "vision_footnote",+ "score": 0.6233,+ "bbox": [+ 724,+ 2078,+ 1530,+ 2112+ ],+ "index": 37+ },+ {+ "cls_id": 7,+ "label": "figure_title",+ "score": 0.61,+ "bbox": [+ 724,+ 2078,+ 1530,+ 2112+ ],+ "index": 38+ },+ {+ "cls_id": 16,+ "label": "number",+ "score": 0.8796,+ "bbox": [+ 178,+ 2145,+ 207,+ 2172+ ],+ "index": 39+ }+ ],+ "onnx": [+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.4676,+ "bbox": [+ 501,+ 1620,+ 829,+ 1795+ ],+ "index": 1+ },+ {+ "cls_id": 2,+ "label": "aside_text",+ "score": 0.7695,+ "bbox": [+ 48,+ 762,+ 83,+ 1541+ ],+ "index": 2+ },+ {+ "cls_id": 13,+ "label": "header_image",+ "score": 0.9544,+ "bbox": [+ 174,+ 129,+ 637,+ 217+ ],+ "index": 3+ },+ {+ "cls_id": 12,+ "label": "header",+ "score": 0.9447,+ "bbox": [+ 1313,+ 177,+ 1526,+ 210+ ],+ "index": 4+ },+ {+ "cls_id": 17,+ "label": "paragraph_title",+ "score": 0.7792,+ "bbox": [+ 176,+ 286,+ 448,+ 334+ ],+ "index": 5+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.6613,+ "bbox": [+ 270,+ 374,+ 684,+ 664+ ],+ "index": 6+ },+ {+ "cls_id": 17,+ "label": "paragraph_title",+ "score": 0.9366,+ "bbox": [+ 803,+ 425,+ 949,+ 463+ ],+ "index": 7+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.9505,+ "bbox": [+ 801,+ 493,+ 1251,+ 528+ ],+ "index": 8+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.9427,+ "bbox": [+ 802,+ 533,+ 1124,+ 567+ ],+ "index": 9+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.951,+ "bbox": [+ 802,+ 572,+ 1360,+ 608+ ],+ "index": 10+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.9445,+ "bbox": [+ 802,+ 612,+ 1151,+ 645+ ],+ "index": 11+ },+ {+ "cls_id": 17,+ "label": "paragraph_title",+ "score": 0.4465,+ "bbox": [+ 206,+ 742,+ 488,+ 776+ ],+ "index": 12+ },+ {+ "cls_id": 21,+ "label": "table",+ "score": 0.9862,+ "bbox": [+ 180,+ 768,+ 1522,+ 1212+ ],+ "index": 13+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.4616,+ "bbox": [+ 880,+ 861,+ 921,+ 890+ ],+ "index": 14+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5831,+ "bbox": [+ 887,+ 897,+ 917,+ 927+ ],+ "index": 15+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5061,+ "bbox": [+ 880,+ 947,+ 921,+ 979+ ],+ "index": 16+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.599,+ "bbox": [+ 884,+ 1002,+ 923,+ 1032+ ],+ "index": 17+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.6059,+ "bbox": [+ 885,+ 1039,+ 921,+ 1068+ ],+ "index": 18+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.549,+ "bbox": [+ 886,+ 1071,+ 918,+ 1101+ ],+ "index": 19+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.5604,+ "bbox": [+ 888,+ 1108,+ 916,+ 1138+ ],+ "index": 20+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.503,+ "bbox": [+ 887,+ 1143,+ 916,+ 1172+ ],+ "index": 21+ },+ {+ "cls_id": 15,+ "label": "inline_formula",+ "score": 0.6237,+ "bbox": [+ 881,+ 1178,+ 915,+ 1209+ ],+ "index": 22+ },+ {+ "cls_id": 21,+ "label": "table",+ "score": 0.9521,+ "bbox": [+ 181,+ 1241,+ 1522,+ 1356+ ],+ "index": 23+ },+ {+ "cls_id": 24,+ "label": "vision_footnote",+ "score": 0.8721,+ "bbox": [+ 539,+ 1382,+ 1529,+ 1416+ ],+ "index": 24+ },+ {+ "cls_id": 22,+ "label": "text",+ "score": 0.5823,+ "bbox": [+ 207,+ 1417,+ 350,+ 1451+ ],+ "index": 25+ },+ {+ "cls_id": 7,+ "label": "figure_title",+ "score": 0.7478,+ "bbox": [+ 177,+ 1517,+ 401,+ 1551+ ],+ "index": 26+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7321,+ "bbox": [+ 177,+ 1622,+ 489,+ 1774+ ],+ "index": 27+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.6653,+ "bbox": [+ 499,+ 1567,+ 832,+ 1796+ ],+ "index": 28+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.5123,+ "bbox": [+ 173,+ 1523,+ 1537,+ 2058+ ],+ "index": 29+ },+ {+ "cls_id": 24,+ "label": "vision_footnote",+ "score": 0.5163,+ "bbox": [+ 580,+ 1826,+ 819,+ 1863+ ],+ "index": 30+ },+ {+ "cls_id": 7,+ "label": "figure_title",+ "score": 0.5169,+ "bbox": [+ 891,+ 1516,+ 1117,+ 1552+ ],+ "index": 31+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7158,+ "bbox": [+ 867,+ 1603,+ 1223,+ 1783+ ],+ "index": 32+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7028,+ "bbox": [+ 1238,+ 1610,+ 1539,+ 1789+ ],+ "index": 33+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.6783,+ "bbox": [+ 179,+ 1928,+ 492,+ 2029+ ],+ "index": 34+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7505,+ "bbox": [+ 538,+ 1883,+ 828,+ 2039+ ],+ "index": 35+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.6202,+ "bbox": [+ 868,+ 1840,+ 1175,+ 2031+ ],+ "index": 36+ },+ {+ "cls_id": 14,+ "label": "image",+ "score": 0.7749,+ "bbox": [+ 1260,+ 1838,+ 1519,+ 2059+ ],+ "index": 37+ },+ {+ "cls_id": 24,+ "label": "vision_footnote",+ "score": 0.6236,+ "bbox": [+ 724,+ 2078,+ 1530,+ 2112+ ],+ "index": 38+ },+ {+ "cls_id": 7,+ "label": "figure_title",+ "score": 0.6097,+ "bbox": [+ 724,+ 2078,+ 1530,+ 2112+ ],+ "index": 39+ },+ {+ "cls_id": 16,+ "label": "number",+ "score": 0.8796,+ "bbox": [+ 178,+ 2145,+ 207,+ 2172+ ],+ "index": 40+ }+ ]+ }+}diff --git a/tests/unittest/test_onnx_model_stack.py b/tests/unittest/test_onnx_model_stack.pyindex fdef9ddd..4420f768 100644--- a/tests/unittest/test_onnx_model_stack.py+++ b/tests/unittest/test_onnx_model_stack.py@@ -189,7 +189,7 @@ def test_layout_batch_keeps_independent_pages(monkeypatch: pytest.MonkeyPatch) -monkeypatch.setattr(model, "_run_session", infer)monkeypatch.setattr(model, "_parse_prediction", lambda prediction, size: [prediction])- monkeypatch.setattr(model, "_apply_layout_post_process", lambda result, image_size: result)+ monkeypatch.setattr(model, "_apply_layout_post_process", lambda result, image_size, use_paddlex_filter_boxes: result)assert model.batch_predict([np.full((3, 8, 8), i) for i in (1, 2, 3)], batch_size=2) == [[{"page": 1}],[{"page": 2}],diff --git a/tests/unittest/test_pdf_footnote_layout.py b/tests/unittest/test_pdf_footnote_layout.pyindex 3f685164..95afea7c 100644--- a/tests/unittest/test_pdf_footnote_layout.py+++ b/tests/unittest/test_pdf_footnote_layout.py@@ -30,11 +30,14 @@ FIXTURE = Path(__file__).parents[1] / "fixtures/emnlp614_footnote"("effort", "is_async", "fallback"),[("medium", False, False), ("high", False, False), ("high", True, False), ("high", False, True), ("high", True, True)],)[email protected]("formula_label", ["inline_formula", "display_formula"])def test_real_footnote_survives_recognition_and_native_fill(- monkeypatch: pytest.MonkeyPatch, effort: str, is_async: bool, fallback: bool+ monkeypatch: pytest.MonkeyPatch, effort: str, is_async: bool, fallback: bool, formula_label: str) -> None:"""冻结推理边界但执行真实回填,确保完整脚注、公式和两行内容均只出现一次。"""fixture = json.loads((FIXTURE / "detection.json").read_text())+ # 同一真实切片也覆盖被误标为行间公式的输入,修正后仍必须完整走识别和原生回填。+ fixture["raw_layout"]["torch"][0].update(label=formula_label, cls_id=15 if formula_label == "inline_formula" else 5)source = (FIXTURE / "page7.pdf").read_bytes()formula_bbox = fixture["raw_layout"]["torch"][0]["bbox"]latex = "h_0(d_0) = 5, h_1(d_0) = 15, h_0(d_1) = 10"@@ -46,7 +49,7 @@ def test_real_footnote_survives_recognition_and_native_fill(image = document.render_page(0, scale=fixture["scale"]).pil_imageassert list(image.size) == fixture["render_size"]layout = PPDocLayoutV2PostProcessor._apply_layout_post_process(- PPDocLayoutV2PostProcessor._apply_paddlex_filter_boxes(fixture["raw_layout"]["torch"], drop_inline_formula=False),+ fixture["raw_layout"]["torch"],image_size=image.size[::-1],)blocks = _build_vl_style_layout_blocks([layout], [image])diff --git a/tests/unittest/test_pp_doclayoutv2_postprocess.py b/tests/unittest/test_pp_doclayoutv2_postprocess.pyindex 8f1d2728..e1104889 100644--- a/tests/unittest/test_pp_doclayoutv2_postprocess.py+++ b/tests/unittest/test_pp_doclayoutv2_postprocess.py@@ -20,7 +20,7 @@ from mineru.model.layout.pp_doclayout_v2_base import (PP_DOCLAYOUT_V2_LABEL_TO_ID,PPDocLayoutV2PostProcessor,)-from mineru.types import BlockType+from mineru.types import RAW_FOOTNOTE, BlockType@pytest.fixture(params=["base", "onnx", pytest.param("torch", marks=pytest.mark.torch)])@@ -222,7 +222,7 @@ def test_real_footnote_layout_preserves_formula_recognition(boxes = fixture["raw_layout"][detection_backend]before = deepcopy(boxes)processed = processor._apply_layout_post_process(- processor._apply_paddlex_filter_boxes(boxes, drop_inline_formula=False),+ boxes,image_size=tuple(reversed(fixture["render_size"])),)assert boxes == before@@ -318,10 +318,10 @@ def test_final_filter_removes_text_duplicates_created_by_relabeling(processor: t),],)-def test_image_absorption_uses_earliest_child_position(+def test_image_absorption_uses_median_child_position(processor: type[PPDocLayoutV2PostProcessor], input_order: list[str], expected_order: list[str]) -> None:- """大图无论位于子图之前或之后,都取最早被吞并子图的位置且不改变其他块顺序。"""+ """大图使用全部子图中位数,而非最早子图序号,其他块相对顺序不变。"""boxes = {"parent": _layout_box("image", (0, 0, 100, 100), index=0),"child": _layout_box("image", (10, 10, 40, 40), index=0),@@ -333,21 +333,21 @@ def test_image_absorption_uses_earliest_child_position(source = [dict(boxes[name], index=index) for index, name in enumerate(input_order, start=1)]original = deepcopy(source)- result = processor._apply_paddlex_filter_boxes(source, drop_inline_formula=False)+ result = processor._apply_layout_post_process(source)assert result == [dict(boxes[name], index=index) for index, name in enumerate(expected_order, start=1)]assert source == original-def test_image_absorption_propagates_inherited_child_position(processor: type[PPDocLayoutV2PostProcessor]) -> None:- """中图先吞小图再被大图吞并时,传递小图位置而非中图原先抢占的位置。"""+def test_image_absorption_collects_all_descendant_positions(processor: type[PPDocLayoutV2PostProcessor]) -> None:+ """一次收集所有大小子图的原始序号,不传递子节点聚合后的顺序。"""middle = _layout_box("image", (10, 10, 60, 60), index=1)leading = _layout_box("text", (200, 0, 240, 20), index=2)child = _layout_box("image", (20, 20, 40, 40), index=3)parent = _layout_box("image", (0, 0, 100, 100), index=4)trailing = _layout_box("text", (200, 30, 240, 50), index=5)- result = processor._apply_paddlex_filter_boxes([middle, leading, child, parent, trailing], drop_inline_formula=False)+ result = processor._apply_layout_post_process([middle, leading, child, parent, trailing])assert result == [dict(leading, index=1), dict(parent, index=2), dict(trailing, index=3)]@@ -362,9 +362,7 @@ def test_image_absorption_positions_independent_groups(processor: type[PPDocLayoleft_child = _layout_box("image", (10, 10, 40, 40), index=6)trailing = _layout_box("text", (400, 60, 440, 80), index=7)- result = processor._apply_paddlex_filter_boxes(- [left, right, leading, right_child, between, left_child, trailing], drop_inline_formula=False- )+ result = processor._apply_layout_post_process([left, right, leading, right_child, between, left_child, trailing])assert result == [dict(box, index=index) for index, box in enumerate([leading, right, between, left, trailing], start=1)]@@ -373,26 +371,26 @@ def test_image_absorption_positions_independent_groups(processor: type[PPDocLayodef test_image_position_inheritance_preserves_existing_overlap_rule(processor: type[PPDocLayoutV2PostProcessor], child_x: int, absorbed: bool) -> None:- """继续使用大于 0.7 的既有重叠判定,既不降低门槛也不额外要求八成或完全包含。"""+ """超过七成时有界合并并覆盖越界部分,恰好七成时保留两个图片。"""parent = _layout_box("image", (0, 0, 100, 100), index=1)text = _layout_box("text", (200, 0, 240, 20), index=2)child = _layout_box("image", (child_x, 10, child_x + 40, 50), index=3)- result = processor._apply_paddlex_filter_boxes([parent, text, child], drop_inline_formula=False)+ result = processor._apply_layout_post_process([parent, text, child])- expected = [text, parent] if absorbed else [parent, text, child]+ expected = [text, dict(parent, bbox=[0, 0, child_x + 40, 100])] if absorbed else [parent, text, child]assert result == [dict(box, index=index) for index, box in enumerate(expected, start=1)]def test_equal_area_image_deduplication_does_not_inherit_position(processor: type[PPDocLayoutV2PostProcessor]) -> None:- """等面积图片重复框沿用现有首框选择,不能把去重变成吞并定位。"""+ """等面积重复图片取高分检测自身顺序,不将低分重复框当成子图。"""parent = _layout_box("image", (0, 0, 100, 100), index=1, score=0.8)text = _layout_box("text", (200, 0, 240, 20), index=2)duplicate = _layout_box("image", (0, 0, 100, 100), index=3, score=0.9)- result = processor._apply_paddlex_filter_boxes([parent, text, duplicate], drop_inline_formula=False)+ result = processor._apply_layout_post_process([parent, text, duplicate])- assert result == [parent, text]+ assert result == [dict(text, index=1), dict(duplicate, index=2)]@pytest.mark.parametrize("label", ["text", "abstract", "table", "chart", "seal", "display_formula"])@@ -402,7 +400,7 @@ def test_non_image_absorption_keeps_parent_position(processor: type[PPDocLayoutVtext = _layout_box("text", (200, 0, 240, 20), index=2)child = _layout_box(label, (10, 10, 40, 40), index=3)- result = processor._apply_paddlex_filter_boxes([parent, text, child], drop_inline_formula=False)+ result = processor._apply_layout_post_process([parent, text, child])assert result == [parent, text]@@ -459,13 +457,13 @@ def test_demo2_image_absorption_restores_order_and_common_block_inputs(]original = deepcopy(raw)- processed = processor._apply_layout_post_process(- processor._apply_paddlex_filter_boxes(raw, drop_inline_formula=False), image_size=tuple(fixture["image_size"])- )+ processed = processor._apply_layout_post_process(raw, image_size=tuple(fixture["image_size"]))expected = [dict(raw[source_index - 1], index=index) for index, source_index in enumerate(page["expected_source_indices"], start=1)]+ # 最下方子图越出旧总图一像素;新总图必须包含这条原先被裁掉的边缘。+ expected[1] = dict(expected[1], bbox=[140, 280, 812, 1347])threshold_only = [dict(raw[source_index - 1], index=index)for index, source_index in enumerate(page["threshold_only_source_indices"], start=1)@@ -481,3 +479,226 @@ def test_demo2_image_absorption_restores_order_and_common_block_inputs(blocks = _build_vl_style_layout_blocks([processed], [image])[0]assert [block["type"] for block in blocks[:2]] == [BlockType.TEXT, BlockType.IMAGE]assert blocks[1]["bbox"] == [0.082, 0.127, 0.478, 0.612]++[email protected]("left_label,right_label", [("text", "paragraph_title"), ("image", "text")])+def test_duplicate_uses_high_score_type_and_union(left_label: str, right_label: str) -> None:+ """高 IoU 重复先按分数选类,并保存低分框独有的边缘,不受输入排列影响。"""+ low = _layout_box(left_label, (0, 100, 100, 200), index=1, score=0.6)+ high = _layout_box(right_label, (1, 101, 101, 201), index=2, score=0.95)+ for ordered in permutations([low, high]):+ before = deepcopy(ordered)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process(list(ordered))+ assert result == [dict(high, bbox=[0, 100, 101, 201], index=1)]+ assert ordered == before+++def test_duplicate_equal_scores_prefer_area_then_original_order() -> None:+ """重复检测同分时先保留较大区域,同面积时使用原始序号而非传入排列。"""+ small = _layout_box("text", (1, 101, 99, 199), index=1)+ large = _layout_box("paragraph_title", (0, 100, 100, 200), index=3)+ tied = _layout_box("abstract", (0, 100, 100, 200), index=2)+ for ordered in permutations([small, large, tied]):+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process(list(ordered))+ assert result == [dict(tied, index=1)]+++def test_duplicate_threshold_is_strict_and_union_does_not_chain() -> None:+ """关闭通用合并后,恰好 0.9 不去重,重复框并集也不能继续吸收第三个框。"""+ exact = [+ _layout_box("text", (0, 100, 19, 120), index=1),+ _layout_box("paragraph_title", (1, 100, 20, 120), index=2),+ ]+ assert PPDocLayoutV2PostProcessor._calculate_iou(exact[0]["bbox"], exact[1]["bbox"]) == 0.9+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process(exact, use_paddlex_filter_boxes=False)+ assert result == exact+ chain = [+ _layout_box("text", (0, 100, 100, 200), index=1, score=0.95),+ _layout_box("text", (4, 100, 104, 200), index=2, score=0.9),+ _layout_box("text", (8, 100, 108, 200), index=3, score=0.85),+ ]+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process(chain, use_paddlex_filter_boxes=False)+ assert result == [dict(chain[0], bbox=[0, 100, 104, 200]), dict(chain[2], index=2)]++[email protected]("label", ["text", "display_formula"])+def test_bounded_merge_uses_frozen_anchors_in_all_orders(label: str) -> None:+ """通用和公式合并都不沿重叠链扩张,固定原始序号后输入排列不改变结果。"""+ boxes = [+ _layout_box(label, (offset, 100, offset + 100, 200), index=index, score=1 - index * 0.1)+ for index, offset in enumerate((0, 20, 40, 60), start=1)+ ]+ expected = [dict(boxes[0], bbox=[0, 100, 120, 200]), dict(boxes[2], bbox=[40, 100, 160, 200], index=2)]+ for ordered in permutations(boxes):+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process(list(ordered))+ assert result == expected+++def test_bounded_merge_area_limit_is_fixed_and_rejected_member_survives() -> None:+ """并集恰好 1.5 倍可以保留,再加入导致超限的检测必须独立存活。"""+ anchor = _layout_box("table", (100, 100, 200, 200), index=1, score=0.6)+ left = _layout_box("text", (75, 110, 175, 130), index=2, score=0.99)+ right = _layout_box("paragraph_title", (125, 140, 225, 160), index=3, score=0.98)+ top = _layout_box("text", (110, 75, 130, 175), index=4, score=0.97)+ source = [anchor, left, right, top]+ for ordered in permutations(source):+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process(list(ordered))+ assert result == [dict(anchor, bbox=[75, 100, 225, 200]), dict(top, index=2)]+ assert all(+ any(PPDocLayoutV2PostProcessor._calculate_cover_ratio(box["bbox"], kept["bbox"]) == 1 for kept in result)+ for box in source+ )++[email protected]("outer_label,inner_label", [("table", "text"), ("text", "table"), ("text", "paragraph_title")])[email protected]("bottom", [199, 201])+def test_contained_and_partial_cross_label_merge_keep_large_type(outer_label: str, inner_label: str, bottom: int) -> None:+ """完整包含与微小越界统一保留大框类型和分数,不使用小框高分改写区域语义。"""+ parent = _layout_box(outer_label, (0, 100, 100, 200), index=1, score=0.6)+ child = _layout_box(inner_label, (10, 180, 40, bottom), index=2, score=0.99)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([parent, child])+ assert result == [dict(parent, bbox=[0, 100, 100, max(200, bottom)])]++[email protected]("inner_label", ["text", "table", "chart", "seal"])+def test_visual_cross_label_coexistence_remains(inner_label: str) -> None:+ """非近重复的视觉跨类别组合仍共存,不因有界合并而丢失内部内容。"""+ boxes = [+ _layout_box("image", (0, 100, 100, 200), index=1),+ _layout_box(inner_label, (10, 120, 40, 150), index=2),+ ]+ assert PPDocLayoutV2PostProcessor._apply_layout_post_process(boxes) == boxes++[email protected]("child_indices", [(1, 8, 12, 14), (1, 10, 12)])+def test_image_median_resists_first_outlier_and_does_not_round(child_indices: tuple[int, ...]) -> None:+ """奇偶数量子图均使用原始中位数,异常首位子图不会把底部总图拉到开头。"""+ parent = _layout_box("image", (0, 100, 100, 200), index=2)+ before = _layout_box("text", (200, 100, 240, 120), index=9)+ after = _layout_box("text", (200, 140, 240, 160), index=11)+ children = [+ _layout_box("image", (10 + i * 20, 120, 25 + i * 20, 140), index=index) for i, index in enumerate(child_indices)+ ]+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([parent, before, after, *children])+ assert result == [dict(before, index=1), dict(parent, index=2), dict(after, index=3)]+ # 4.5 不能取整到 4;原始父框序号更早也不能抢到第 4 个位置之前。+ children = [_layout_box("image", (10, 120, 30, 140), index=3), _layout_box("image", (40, 120, 60, 140), index=6)]+ before["index"], after["index"] = 4, 5+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([parent, before, after, *children])+ assert result == [dict(before, index=1), dict(parent, index=2), dict(after, index=3)]+++def test_duplicate_child_only_votes_once_in_image_median() -> None:+ """子图近重复先归一为高分检测,再参与一次中位数投票,不累计被删重复的位置。"""+ parent = _layout_box("image", (0, 100, 100, 200), index=1)+ duplicate = _layout_box("image", (10, 120, 25, 140), index=2, score=0.7)+ winner = dict(duplicate, index=100, score=0.99)+ children = [_layout_box("image", (30 + i * 20, 120, 45 + i * 20, 140), index=index) for i, index in enumerate((4, 8, 12))]+ before = _layout_box("text", (200, 100, 240, 120), index=9)+ after = _layout_box("text", (200, 140, 240, 160), index=11)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([parent, duplicate, winner, *children, before, after])+ assert result == [dict(before, index=1), dict(parent, index=2), dict(after, index=3)]++[email protected]("label", ["figure_title", "vision_footnote"])+def test_visual_annotation_filter_uses_final_body_and_keeps_external_note(label: str) -> None:+ """删除最终图片中的两类注释,保留图外说明;原图被替换成文本后不再充当过滤父框。"""+ body = _layout_box("image", (0, 100, 100, 200), index=1, score=0.6)+ inside = _layout_box(label, (10, 130, 80, 145), index=2)+ outside = _layout_box(label, (10, 210, 80, 225), index=3)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([body, inside, outside])+ assert result == [body, dict(outside, index=2)]+ text = _layout_box("text", (0, 100, 100, 200), index=1, score=0.99)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([body, text, inside], use_paddlex_filter_boxes=False)+ assert result == [text, inside]+++def test_relabelled_visual_body_deduplicates_without_image_reordering() -> None:+ """图和注释一起成为脚注后只留大框,且不再按已吞并子图的中位数重排。"""+ footnote = _layout_box("footnote", (0, 100, 500, 110), index=1)+ parent = _layout_box("image", (0, 120, 100, 220), index=2)+ text = _layout_box("text", (600, 120, 700, 140), index=3)+ child = _layout_box("image", (10, 140, 40, 170), index=4)+ caption = _layout_box("figure_title", (10, 180, 40, 195), index=5)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([footnote, parent, text, child, caption])+ assert result == [footnote, dict(parent, label="footnote", cls_id=10), text]++[email protected]("parent_label", ["text", "footnote"])[email protected]("formula_label", ["display_formula", "inline_formula"])[email protected]("use_filter", [False, True])+def test_near_duplicate_text_formula_pair_preserves_both_semantic_layers(+ parent_label: str, formula_label: str, use_filter: bool+) -> None:+ """短文字配长公式时高 IoU 不触发互相抑制,行间误标也能转为行内并进入识别。"""+ parent = _layout_box(parent_label, (0, 100, 100, 200), index=1, score=0.6)+ formula = _layout_box(formula_label, (1, 101, 99, 199), index=2, score=0.99)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([parent, formula], use_paddlex_filter_boxes=use_filter)+ assert result == [parent, dict(formula, label="inline_formula", cls_id=15)]+ assert len(_build_formula_inputs([result])[0]) == 1+++def test_formula_number_is_not_suppressed_by_formula_or_text() -> None:+ """公式内部的编号不会被提前删除,脚注内编号也保留给后续编号识别和合并。"""+ formula = _layout_box("display_formula", (0, 100, 100, 140), index=1)+ number = _layout_box("formula_number", (80, 110, 95, 130), index=2)+ assert PPDocLayoutV2PostProcessor._apply_layout_post_process([formula, number]) == [formula, number]+ parent = _layout_box("footnote", (0, 90, 110, 150), index=3)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([formula, number, parent])+ assert result == [dict(formula, label="inline_formula", cls_id=15), number, parent]+++def test_small_box_filter_preserves_inline_protection_without_widening_other_labels() -> None:+ """保留原行内公式及新转行内公式的尺寸豁免,不放宽独立公式和其他类别的六像素门槛。"""+ parent = _layout_box("text", (0, 100, 100, 140), index=1)+ nested = _layout_box("display_formula", (20, 110, 60, 115), index=2)+ orphan_inline = _layout_box("inline_formula", (200, 110, 240, 115), index=3)+ tiny_display = _layout_box("display_formula", (300, 110, 340, 115), index=4)+ tiny_text = _layout_box("text", (400, 110, 440, 115), index=5)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([parent, nested, orphan_inline, tiny_display, tiny_text])+ assert result == [+ parent,+ dict(nested, label="inline_formula", cls_id=15),+ dict(orphan_inline, label="display_formula", cls_id=5),+ ]+++ "anchor_label,anchor_bbox,parent_bbox",+ [+ ("header", (800, 150, 900, 180), (100, 80, 700, 160)),+ ("footer", (0, 700, 1000, 730), (100, 750, 700, 850)),+ ],+)+def test_formula_cleanup_runs_after_boundary_relabelling(+ anchor_label: str, anchor_bbox: tuple[int, ...], parent_bbox: tuple[int, ...]+) -> None:+ """新改标的页眉页脚也会清理内部公式,验证清理顺序而非只覆盖原始父框。"""+ anchor = _layout_box(anchor_label, anchor_bbox, index=1)+ parent = _layout_box("text", parent_bbox, index=2)+ formula = _layout_box("display_formula", (200, parent_bbox[1] + 10, 300, parent_bbox[1] + 40), index=3)+ result = PPDocLayoutV2PostProcessor._apply_layout_post_process([anchor, parent, formula], image_size=(1000, 1000))+ assert result == [anchor, dict(parent, label=anchor_label, cls_id=PP_DOCLAYOUT_V2_LABEL_TO_ID[anchor_label])]+ assert _build_formula_inputs([result]) == [[]]++[email protected]("detection_backend", ["torch", "onnx"])+def test_smac_layout_removes_internal_note_and_restores_image_order(+ processor: type[PPDocLayoutV2PostProcessor], detection_backend: str+) -> None:+ """复放 SMAC 实测框,修正图内脚注和 ONNX 抢序,并只扩展子图越界的两条边。"""+ fixture = json.loads((Path(__file__).parents[1] / "fixtures/pp_doclayoutv2/smac_1045h.json").read_text())+ raw = fixture["backends"][detection_backend]+ before = deepcopy(raw)+ source_indices = [*range(1, 25), 28, 37, 39] if detection_backend == "torch" else [*range(2, 26), 29, 38, 40]+ expected = [dict(raw[index - 1], index=order) for order, index in enumerate(source_indices, start=1)]+ expected[24] = dict(expected[24], bbox=[173, 1523, 1539, 2059])+ result = processor._apply_layout_post_process(raw, image_size=tuple(fixture["image_size"]))+ assert result == expected+ assert raw == before+ assert all(set(item) == {"cls_id", "label", "score", "bbox", "index"} for item in result)+ assert len(_build_formula_inputs([result])[0]) == 9+ with Image.new("RGB", tuple(reversed(fixture["image_size"]))) as image:+ blocks = _build_vl_style_layout_blocks([result], [image])[0]+ assert [block["type"] for block in blocks[-4:]] == [BlockType.TEXT, BlockType.IMAGE, RAW_FOOTNOTE, BlockType.PAGE_NUMBER]+ assert blocks[-3]["bbox"] == [0.102, 0.664, 0.905, 0.898]