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
Add MFR input handling and validation for table formulas in PDF processing
myhloli467dc77a4bd6
diff --git a/mineru/backend/analysis/pdf/tables.py b/mineru/backend/analysis/pdf/tables.pyindex 8c725618..7091e35e 100644--- a/mineru/backend/analysis/pdf/tables.py+++ b/mineru/backend/analysis/pdf/tables.py@@ -576,6 +576,46 @@ def _select_table_owner(return max(candidates, key=lambda item: item[0])[1]+def _build_table_external_mfr_inputs(+ images_formula_list: list[list[dict[str, Any]]],+ model_list: list[list[dict[str, Any]]],+ page_sizes: list[tuple[int, int]],+) -> list[list[dict[str, Any]]]:+ """按最终版面筛选 High/Xhigh 表外行内公式,保留原始公式供 OCR 遮罩使用。"""+ mfr_inputs: list[list[dict[str, Any]]] = []+ for formulas, blocks, page_size in zip(images_formula_list, model_list, page_sizes, strict=True):+ table_entries = []+ for block in blocks:+ if block.get("type") != BlockType.TABLE:+ continue+ bbox = block.get("bbox")+ if not isinstance(bbox, (list, tuple)) or len(bbox) != 4:+ continue+ try:+ coordinates = tuple(float(value) for value in bbox)+ except (TypeError, ValueError):+ continue+ if not all(math.isfinite(value) for value in coordinates):+ continue+ if coordinates[2] <= coordinates[0] or coordinates[3] <= coordinates[1]:+ continue+ table_bbox = _bbox_to_pixel_bbox(coordinates, page_size)+ if table_bbox is not None:+ table_entries.append({"table_bbox": table_bbox})++ page_inputs = []+ for formula in formulas:+ if formula.get("label") != "inline_formula":+ continue+ # MFD 框已经处于页面像素空间;表格 angle 不改变其页面上的归属范围。+ formula_bbox = formula.get("bbox")+ if formula_bbox is not None and _select_table_owner(formula_bbox, table_entries) is not None:+ continue+ page_inputs.append({**formula, "bbox": list(formula_bbox) if formula_bbox is not None else None})+ mfr_inputs.append(page_inputs)+ return mfr_inputs++def _collect_medium_table_tasks(model_list: list[list[dict[str, Any]]],inline_formula_list: list[list[dict[str, Any]]],diff --git a/mineru/backend/analysis/pdf/window.py b/mineru/backend/analysis/pdf/window.pyindex b7189a67..54d6b3ac 100644--- a/mineru/backend/analysis/pdf/window.py+++ b/mineru/backend/analysis/pdf/window.py@@ -48,6 +48,7 @@ from .tables import (_apply_medium_table_recognition,_apply_native_txt_table_priority,_apply_table_orientations,+ _build_table_external_mfr_inputs,_fill_flash_ocr_table_contents,_restore_native_high_table_blocks,_split_native_high_table_blocks,@@ -214,14 +215,22 @@ def _process_text_and_formulas(images_formula_list = mfd_resinterline_enable = effort == "medium"- # medium 识别行内和行间公式;high/xhigh 的 txt 路径只识别行内公式。- if mfr_enable and any(mfd_res):- images_formula_list = local_model_context.mfr_model.batch_predict(- mfd_res,- np_images,- batch_size=BATCH_RATIO * MFR_BASE_BATCH_SIZE,- interline_enable=interline_enable,+ # medium 保留未被原生表格消费的公式;high/xhigh 只识别最终表格外的行内公式。+ if mfr_enable:+ mfr_inputs = (+ mfd_res+ if effort == "medium"+ else _build_table_external_mfr_inputs(mfd_res, model_list, [image.size for image in images_pil_list]))+ # 全部被过滤时保持逐页空结果,不能让原始表内公式重新进入正文 sidecar。+ images_formula_list = [[] for _ in mfd_res]+ if any(mfr_inputs):+ images_formula_list = local_model_context.mfr_model.batch_predict(+ mfr_inputs,+ np_images,+ batch_size=BATCH_RATIO * MFR_BASE_BATCH_SIZE,+ interline_enable=interline_enable,+ )inline_formula_list, display_formula_list = _split_formula_results(images_formula_list)if effort == "medium":diff --git a/tests/fixtures/demo1_mfr_table_inputs.json b/tests/fixtures/demo1_mfr_table_inputs.jsonnew file mode 100644index 00000000..30993b39--- /dev/null+++ b/tests/fixtures/demo1_mfr_table_inputs.json@@ -0,0 +1,1765 @@+{+ "source": "demo/pdfs/demo1.pdf",+ "sha256": "f3b3be345bf2df8979f2491ca9466e078e4fd1d6a216611faa8566e4c44d474b",+ "small_backend": "onnx",+ "tiers": {+ "standard": {+ "page_sizes": [+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ]+ ],+ "formulas": [+ [+ {+ "label": "inline_formula",+ "bbox": [+ 570,+ 1350,+ 637,+ 1381+ ],+ "score": 0.5242,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 715,+ 1382,+ 771,+ 1412+ ],+ "score": 0.7102,+ "latex": ""+ }+ ],+ [],+ [+ {+ "label": "display_formula",+ "bbox": [+ 775,+ 434,+ 997,+ 471+ ],+ "score": 0.9328,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 856,+ 520,+ 897,+ 551+ ],+ "score": 0.884,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 929,+ 552,+ 983,+ 584+ ],+ "score": 0.7126,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1150,+ 520,+ 1201,+ 551+ ],+ "score": 0.6521,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 775,+ 1156,+ 1199,+ 1237+ ],+ "score": 0.9619,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 857,+ 1286,+ 898,+ 1316+ ],+ "score": 0.8812,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1208,+ 1286,+ 1252,+ 1315+ ],+ "score": 0.8376,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 963,+ 1349,+ 1004,+ 1380+ ],+ "score": 0.8024,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1208,+ 1384,+ 1261,+ 1415+ ],+ "score": 0.8902,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1260,+ 1418,+ 1300,+ 1448+ ],+ "score": 0.8632,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 895,+ 1482,+ 977,+ 1511+ ],+ "score": 0.8593,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1269,+ 1483,+ 1311,+ 1515+ ],+ "score": 0.8872,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 780,+ 1683,+ 821,+ 1715+ ],+ "score": 0.8717,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1195,+ 1647,+ 1278,+ 1677+ ],+ "score": 0.7465,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1313,+ 1648,+ 1376,+ 1678+ ],+ "score": 0.7079,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1075,+ 1683,+ 1139,+ 1713+ ],+ "score": 0.6192,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 511,+ 1487,+ 586,+ 1519+ ],+ "score": 0.9026,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 597,+ 1522,+ 660,+ 1552+ ],+ "score": 0.8898,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 528,+ 1555,+ 587,+ 1585+ ],+ "score": 0.8968,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 123,+ 1677,+ 564,+ 1756+ ],+ "score": 0.963,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 647,+ 1782,+ 730,+ 1810+ ],+ "score": 0.8024,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 182,+ 1814,+ 323,+ 1848+ ],+ "score": 0.6554,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 792,+ 353,+ 857,+ 382+ ],+ "score": 0.4992,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1306,+ 319,+ 1390,+ 348+ ],+ "score": 0.7514,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 788,+ 779,+ 1109,+ 862+ ],+ "score": 0.9616,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 841,+ 910,+ 871,+ 940+ ],+ "score": 0.4733,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 958,+ 1074,+ 1001,+ 1108+ ],+ "score": 0.8281,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1333,+ 1072,+ 1365,+ 1105+ ],+ "score": 0.8473,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 879,+ 1173,+ 931,+ 1205+ ],+ "score": 0.8784,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1282,+ 1176,+ 1365,+ 1206+ ],+ "score": 0.7242,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 872,+ 1408,+ 934,+ 1438+ ],+ "score": 0.5798,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1199,+ 1408,+ 1259,+ 1438+ ],+ "score": 0.4896,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 787,+ 1523,+ 1109,+ 1602+ ],+ "score": 0.9624,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 789,+ 1664,+ 978,+ 1699+ ],+ "score": 0.9253,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 1086,+ 413,+ 1136,+ 448+ ],+ "score": 0.7654,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 774,+ 546,+ 1165,+ 621+ ],+ "score": 0.9604,+ "latex": ""+ }+ ],+ [],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 296,+ 319,+ 381,+ 349+ ],+ "score": 0.6818,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 323,+ 1383,+ 366,+ 1414+ ],+ "score": 0.5215,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 157,+ 1682,+ 231,+ 1712+ ],+ "score": 0.5854,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 601,+ 1814,+ 716,+ 1848+ ],+ "score": 0.914,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 779,+ 880,+ 832,+ 911+ ],+ "score": 0.8615,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 877,+ 1078,+ 975,+ 1110+ ],+ "score": 0.4867,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 1173,+ 940,+ 1222,+ 973+ ],+ "score": 0.8503,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 814,+ 1078,+ 867,+ 1109+ ],+ "score": 0.8406,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1088,+ 1179,+ 1140,+ 1212+ ],+ "score": 0.8816,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 128,+ 1402,+ 173,+ 1429+ ],+ "score": 0.6116,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 463,+ 750,+ 608,+ 777+ ],+ "score": 0.7096,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 456,+ 778,+ 600,+ 806+ ],+ "score": 0.787,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 167,+ 1025,+ 220,+ 1058+ ],+ "score": 0.8794,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1200,+ 1191,+ 1278,+ 1223+ ],+ "score": 0.8824,+ "latex": ""+ }+ ],+ [],+ [],+ [],+ []+ ],+ "tables": [+ [],+ [],+ [],+ [],+ [+ {+ "type": "table",+ "bbox": [+ 0.115,+ 0.122,+ 0.455,+ 0.896+ ],+ "angle": 270+ }+ ],+ [+ {+ "type": "table",+ "bbox": [+ 0.081,+ 0.153,+ 0.923,+ 0.331+ ],+ "angle": 0+ },+ {+ "type": "table",+ "bbox": [+ 0.081,+ 0.696,+ 0.923,+ 0.875+ ],+ "angle": 0+ }+ ],+ [],+ [+ {+ "type": "table",+ "bbox": [+ 0.082,+ 0.696,+ 0.922,+ 0.875+ ],+ "angle": 0+ }+ ],+ [+ {+ "type": "table",+ "bbox": [+ 0.073,+ 0.164,+ 0.914,+ 0.331+ ],+ "angle": 0+ }+ ],+ [],+ [],+ [],+ []+ ]+ },+ "advanced": {+ "page_sizes": [+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ],+ [+ 1512,+ 2064+ ]+ ],+ "formulas": [+ [+ {+ "label": "inline_formula",+ "bbox": [+ 570,+ 1350,+ 637,+ 1381+ ],+ "score": 0.5242,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 715,+ 1382,+ 771,+ 1412+ ],+ "score": 0.7102,+ "latex": ""+ }+ ],+ [],+ [+ {+ "label": "display_formula",+ "bbox": [+ 775,+ 434,+ 997,+ 471+ ],+ "score": 0.9328,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 856,+ 520,+ 897,+ 551+ ],+ "score": 0.884,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 929,+ 552,+ 983,+ 584+ ],+ "score": 0.7126,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1150,+ 520,+ 1201,+ 551+ ],+ "score": 0.6521,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 775,+ 1156,+ 1199,+ 1237+ ],+ "score": 0.9619,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 857,+ 1286,+ 898,+ 1316+ ],+ "score": 0.8812,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1208,+ 1286,+ 1252,+ 1315+ ],+ "score": 0.8376,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 963,+ 1349,+ 1004,+ 1380+ ],+ "score": 0.8024,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1208,+ 1384,+ 1261,+ 1415+ ],+ "score": 0.8902,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1260,+ 1418,+ 1300,+ 1448+ ],+ "score": 0.8632,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 895,+ 1482,+ 977,+ 1511+ ],+ "score": 0.8593,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1269,+ 1483,+ 1311,+ 1515+ ],+ "score": 0.8872,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 780,+ 1683,+ 821,+ 1715+ ],+ "score": 0.8717,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1195,+ 1647,+ 1278,+ 1677+ ],+ "score": 0.7465,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1313,+ 1648,+ 1376,+ 1678+ ],+ "score": 0.7079,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1075,+ 1683,+ 1139,+ 1713+ ],+ "score": 0.6192,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 511,+ 1487,+ 586,+ 1519+ ],+ "score": 0.9026,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 597,+ 1522,+ 660,+ 1552+ ],+ "score": 0.8898,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 528,+ 1555,+ 587,+ 1585+ ],+ "score": 0.8968,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 123,+ 1677,+ 564,+ 1756+ ],+ "score": 0.963,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 647,+ 1782,+ 730,+ 1810+ ],+ "score": 0.8024,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 182,+ 1814,+ 323,+ 1848+ ],+ "score": 0.6554,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 792,+ 353,+ 857,+ 382+ ],+ "score": 0.4992,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1306,+ 319,+ 1390,+ 348+ ],+ "score": 0.7514,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 788,+ 779,+ 1109,+ 862+ ],+ "score": 0.9616,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 841,+ 910,+ 871,+ 940+ ],+ "score": 0.4733,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 958,+ 1074,+ 1001,+ 1108+ ],+ "score": 0.8281,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1333,+ 1072,+ 1365,+ 1105+ ],+ "score": 0.8473,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 879,+ 1173,+ 931,+ 1205+ ],+ "score": 0.8784,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1282,+ 1176,+ 1365,+ 1206+ ],+ "score": 0.7242,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 872,+ 1408,+ 934,+ 1438+ ],+ "score": 0.5798,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1199,+ 1408,+ 1259,+ 1438+ ],+ "score": 0.4896,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 787,+ 1523,+ 1109,+ 1602+ ],+ "score": 0.9624,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 789,+ 1664,+ 978,+ 1699+ ],+ "score": 0.9253,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 1086,+ 413,+ 1136,+ 448+ ],+ "score": 0.7654,+ "latex": ""+ },+ {+ "label": "display_formula",+ "bbox": [+ 774,+ 546,+ 1165,+ 621+ ],+ "score": 0.9604,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 321,+ 427,+ 363,+ 455+ ],+ "score": 0.4578,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 649,+ 427,+ 687,+ 453+ ],+ "score": 0.4767,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 321,+ 539,+ 363,+ 565+ ],+ "score": 0.463,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 400,+ 896,+ 426+ ],+ "score": 0.5957,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 428,+ 906,+ 455+ ],+ "score": 0.7134,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 456,+ 894,+ 481+ ],+ "score": 0.5089,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 483,+ 911,+ 509+ ],+ "score": 0.6156,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 977,+ 400,+ 1005,+ 425+ ],+ "score": 0.5844,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1087,+ 428,+ 1124,+ 454+ ],+ "score": 0.6899,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 977,+ 427,+ 1015,+ 454+ ],+ "score": 0.6495,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 321,+ 622,+ 363,+ 648+ ],+ "score": 0.4592,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1086,+ 400,+ 1114,+ 426+ ],+ "score": 0.5544,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 977,+ 456,+ 1003,+ 481+ ],+ "score": 0.5128,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1087,+ 511,+ 1129,+ 538+ ],+ "score": 0.5345,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 758,+ 592,+ 799,+ 621+ ],+ "score": 0.4574,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1086,+ 456,+ 1112,+ 482+ ],+ "score": 0.5264,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 511,+ 915,+ 537+ ],+ "score": 0.5702,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1304,+ 510,+ 1348,+ 538+ ],+ "score": 0.4898,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 539,+ 908,+ 565+ ],+ "score": 0.5808,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 977,+ 566,+ 1019,+ 593+ ],+ "score": 0.4771,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 593,+ 908,+ 620+ ],+ "score": 0.5617,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 976,+ 593,+ 1019,+ 620+ ],+ "score": 0.5695,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 867,+ 622,+ 910,+ 648+ ],+ "score": 0.5378,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1086,+ 593,+ 1128,+ 620+ ],+ "score": 0.5631,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1196,+ 593,+ 1236,+ 621+ ],+ "score": 0.5457,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 868,+ 649,+ 908,+ 676+ ],+ "score": 0.5141,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 977,+ 622,+ 1020,+ 649+ ],+ "score": 0.5616,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1086,+ 622,+ 1129,+ 648+ ],+ "score": 0.5002,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 296,+ 319,+ 381,+ 349+ ],+ "score": 0.6818,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 323,+ 1383,+ 366,+ 1414+ ],+ "score": 0.5215,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 157,+ 1682,+ 231,+ 1712+ ],+ "score": 0.5854,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 601,+ 1814,+ 716,+ 1848+ ],+ "score": 0.914,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 779,+ 880,+ 832,+ 911+ ],+ "score": 0.8615,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 877,+ 1078,+ 975,+ 1110+ ],+ "score": 0.4867,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 1173,+ 940,+ 1222,+ 973+ ],+ "score": 0.8503,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 814,+ 1078,+ 867,+ 1109+ ],+ "score": 0.8406,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1088,+ 1179,+ 1140,+ 1212+ ],+ "score": 0.8816,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 128,+ 1402,+ 173,+ 1429+ ],+ "score": 0.6116,+ "latex": ""+ }+ ],+ [+ {+ "label": "inline_formula",+ "bbox": [+ 1142,+ 413,+ 1190,+ 443+ ],+ "score": 0.6318,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 463,+ 750,+ 608,+ 777+ ],+ "score": 0.7096,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 456,+ 778,+ 600,+ 806+ ],+ "score": 0.787,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 167,+ 1025,+ 220,+ 1058+ ],+ "score": 0.8794,+ "latex": ""+ },+ {+ "label": "inline_formula",+ "bbox": [+ 1200,+ 1191,+ 1278,+ 1223+ ],+ "score": 0.8824,+ "latex": ""+ }+ ],+ [],+ [],+ [],+ []+ ],+ "tables": [+ [],+ [],+ [],+ [],+ [+ {+ "type": "table",+ "bbox": [+ 0.115,+ 0.124,+ 0.43,+ 0.891+ ],+ "angle": 270+ }+ ],+ [+ {+ "type": "table",+ "bbox": [+ 0.082,+ 0.152,+ 0.924,+ 0.334+ ],+ "angle": 0+ },+ {+ "type": "table",+ "bbox": [+ 0.082,+ 0.695,+ 0.924,+ 0.878+ ],+ "angle": 0+ }+ ],+ [],+ [+ {+ "type": "table",+ "bbox": [+ 0.082,+ 0.694,+ 0.925,+ 0.878+ ],+ "angle": 0+ }+ ],+ [+ {+ "type": "table",+ "bbox": [+ 0.073,+ 0.165,+ 0.917,+ 0.334+ ],+ "angle": 0+ }+ ],+ [],+ [],+ [],+ []+ ]+ }+ }+}diff --git a/tests/unittest/test_onnx_model_stack.py b/tests/unittest/test_onnx_model_stack.pyindex 3b2632a3..d2fc149c 100644--- a/tests/unittest/test_onnx_model_stack.py+++ b/tests/unittest/test_onnx_model_stack.py@@ -39,12 +39,22 @@ def test_unpublished_source_does_not_invalidate_existing_payload(tmp_path: Path,whole_repo: bool,) -> None:- """未发布来源明确报错,不能删除现有完整标记或误下载旧仓库。"""+ """用固定的单来源仓库验证拒绝不可用来源时,已有模型和完整标记均不受影响。"""monkeypatch.setattr(download.config.model, "base_dir", str(tmp_path))- repo = registry.MINERU_4_MODELS_ONNX+ # 不依赖生产仓库是否已发布到 ModelScope,避免新增来源使负向测试失效。+ repo = download.ModelRepo(+ name="test-huggingface-only",+ repos={"huggingface": "test/huggingface-only"},+ paths={"ocr_det": "OCR/det"},+ )repo.local_dir().mkdir()marker = repo.local_dir() / download.MODEL_COMPLETE_MARKERmarker.touch()+ repo.ocr_det.local_path().mkdir(parents=True)+ resource_marker = repo.ocr_det.local_path() / download.MODEL_COMPLETE_MARKER+ resource_marker.touch()+ payload = repo.ocr_det.local_path() / "model.onnx"+ payload.write_bytes(b"existing model payload")remote = Mock(side_effect=AssertionError("Unexpected network request"))monkeypatch.setattr(download, "hf_snapshot_download", remote)monkeypatch.setattr(download, "ms_snapshot_download", remote)@@ -54,6 +64,8 @@ def test_unpublished_source_does_not_invalidate_existing_payload(else:download.download_model_files(repo, [repo.ocr_det], source="modelscope")assert marker.is_file()+ assert resource_marker.is_file()+ assert payload.read_bytes() == b"existing model payload"remote.assert_not_called()diff --git a/tests/unittest/test_pdf_mfr_table_routing.py b/tests/unittest/test_pdf_mfr_table_routing.pynew file mode 100644index 00000000..88d89f7e--- /dev/null+++ b/tests/unittest/test_pdf_mfr_table_routing.py@@ -0,0 +1,164 @@+"""验证最终表格版面对 MFR 输入的归属约束。"""++from __future__ import annotations++import json+from copy import deepcopy+from pathlib import Path+from types import SimpleNamespace+from typing import Any+from unittest.mock import MagicMock++import pytest+from PIL import Image++from mineru.backend.analysis.pdf import window+from mineru.backend.analysis.pdf.formulas import _build_formula_inputs+from mineru.backend.analysis.pdf.tables import _build_table_external_mfr_inputs+++def _formula(bbox: list[float], label: str = "inline_formula") -> dict[str, Any]:+ """构造与 MFD 输出一致的测试公式。"""+ return {"label": label, "bbox": bbox, "score": 0.9, "latex": ""}++[email protected]("angle", [0, 90, 180, 270])[email protected]("table_bbox", [[0.1, 0.2, 0.9, 0.8], [10, 40, 90, 160]])+def test_final_table_geometry_preserves_external_formulas_and_inputs(angle: int, table_bbox: list[float]) -> None:+ """旋转不改变页面坐标;保留正文、表题、表注及中心位于表外的交叠框。"""+ formulas = [+ [+ _formula([30, 60, 40, 80]),+ _formula([30, 20, 40, 30]),+ _formula([30, 170, 40, 180]),+ _formula([1, 60, 5, 80]),+ _formula([5, 60, 12, 80]),+ _formula([30, 20, 40, 30], "display_formula"),+ ],+ [_formula([30, 60, 40, 80])],+ ]+ blocks = [[{"type": "table", "bbox": table_bbox, "angle": angle}], []]+ original = deepcopy((formulas, blocks))+ result = _build_table_external_mfr_inputs(formulas, blocks, [(100, 200), (100, 200)])+ assert result == [formulas[0][1:5], formulas[1]]+ assert (formulas, blocks) == original+ result[0][0]["bbox"][0] = -1+ assert (formulas, blocks) == original+++ "bbox",+ [+ None,+ 1,+ [],+ [0, 0, 1],+ [0, 0, 0, 1],+ [0.8, 0.8, 0.1, 0.1],+ [0, 0, float("inf"), 1],+ [0, 0, float("nan"), 1],+ [0, 0, "bad", 1],+ ],+)+def test_invalid_table_bbox_does_not_remove_formulas(bbox: Any) -> None:+ """无效表格框不能导致合法公式被排除。"""+ formulas = [[_formula([20, 20, 30, 30])]]+ assert _build_table_external_mfr_inputs(formulas, [[{"type": "table", "bbox": bbox}]], [(100, 100)]) == formulas++[email protected]("effort", ["medium", "high", "xhigh"])[email protected]("parse_mode", ["txt", "ocr"])[email protected]("content", ["", "<table><tr><td>recognized</td></tr></table>"])+def test_window_mfr_routing_and_original_ocr_masks(+ monkeypatch: pytest.MonkeyPatch,+ effort: str,+ parse_mode: str,+ content: str,+) -> None:+ """验证档位路由、空 VLM 结果与原始 OCR 遮罩;Basic 规则清理由已有测试覆盖。"""+ layout = [[_formula([20, 20, 30, 30]), _formula([1, 1, 5, 5]), _formula([40, 40, 50, 50], "display_formula")]]+ blocks = [[{"type": "table", "bbox": [0.1, 0.1, 0.9, 0.9], "content": content}]]+ original = deepcopy(layout)+ mfr = MagicMock(side_effect=lambda inputs, *_args, **_kwargs: deepcopy(inputs))+ enabled = effort == "medium" or parse_mode == "txt"+ context = SimpleNamespace(mfr_model=SimpleNamespace(batch_predict=mfr)) if enabled else SimpleNamespace()+ det = MagicMock(return_value=[[]])+ fill = MagicMock(return_value=blocks)+ monkeypatch.setattr(window, "_ocr_det", det)+ monkeypatch.setattr(window, "_fill_window_block_content_and_lines", fill)+ for name in (+ "_apply_medium_table_recognition",+ "_apply_medium_display_formula_results",+ "_apply_medium_formula_number_ocr",+ "_apply_ocr_rec_results",+ ):+ monkeypatch.setattr(window, name, MagicMock())+ with Image.new("RGB", (100, 100)) as image:+ window._process_text_and_formulas(+ [{"img_pil": image, "scale": 1.0}],+ [MagicMock()],+ blocks,+ parse_mode,+ effort,+ context,+ layout,+ )+ if enabled:+ expected = _build_formula_inputs(layout) if effort == "medium" else [[layout[0][1]]]+ assert mfr.call_args.args[0] == expected+ assert mfr.call_args.kwargs["interline_enable"] is (effort == "medium")+ else:+ mfr.assert_not_called()+ assert det.call_args.args[3] == _build_formula_inputs(layout)+ assert layout == original+ expected_inline = [layout[0][:2]] if effort == "medium" or not enabled else [[layout[0][1]]]+ assert fill.call_args.args[3] == expected_inline++[email protected]("effort", ["high", "xhigh"])[email protected]("final_table", [True, False])+def test_final_layout_controls_filter_and_empty_results_skip_model_loading(+ monkeypatch: pytest.MonkeyPatch,+ effort: str,+ final_table: bool,+) -> None:+ """最终表格框优先于本地框;全空输入不得加载 MFR 或恢复表内 sidecar。"""+ formula = _formula([20, 20, 30, 30])+ # 本地表格与最终表格刻意不一致,确保只根据最终 model_list 决策。+ local_table_bbox = [70, 70, 90, 90] if final_table else [10, 10, 50, 50]+ layout = [[formula, {"label": "table", "bbox": local_table_bbox}], [_formula([10, 10, 20, 20], "display_formula")]]+ blocks = [[{"type": "table", "bbox": [0.1, 0.1, 0.5, 0.5]}] if final_table else [], []]+ mfr = MagicMock(side_effect=lambda inputs, *_args, **_kwargs: deepcopy(inputs))+ context = SimpleNamespace() if final_table else SimpleNamespace(mfr_model=SimpleNamespace(batch_predict=mfr))+ fill = MagicMock(return_value=blocks)+ det = MagicMock(return_value=[[], []])+ monkeypatch.setattr(window, "_ocr_det", det)+ monkeypatch.setattr(window, "_fill_window_block_content_and_lines", fill)+ with Image.new("RGB", (100, 100)) as image:+ window._process_text_and_formulas(+ [{"img_pil": image, "scale": 1.0}] * 2,+ [MagicMock()] * 2,+ blocks,+ "txt",+ effort,+ context,+ layout,+ )+ assert fill.call_args.args[3] == ([[], []] if final_table else [[formula], []])+ assert det.call_args.args[3] == _build_formula_inputs(layout)+ assert mfr.call_count == (0 if final_table else 1)+++def test_demo1_frozen_mfr_inputs_exclude_only_29_table_formulas() -> None:+ """固定真实检测输入,验证第 6/9 页排除 28/1 个且两档剩余 45 个一致。"""+ fixture = json.loads((Path(__file__).parents[1] / "fixtures/demo1_mfr_table_inputs.json").read_text())+ results = {}+ for tier, data in fixture["tiers"].items():+ result = _build_table_external_mfr_inputs(data["formulas"], data["tables"], data["page_sizes"])+ counts = [len(page) for page in result]+ assert counts == [2, 0, 14, 14, 1, 0, 6, 4, 4, 0, 0, 0, 0]+ before = [sum(f["label"] == "inline_formula" for f in page) for page in data["formulas"]]+ expected_removed = [0, 0, 0, 0, 0, 28, 0, 0, 1, 0, 0, 0, 0] if tier == "advanced" else [0] * 13+ assert [a - b for a, b in zip(before, counts, strict=True)] == expected_removed+ results[tier] = result+ assert results["standard"] == results["advanced"]