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: implement OCR for medium formula numbers and enhance vision footnote handling
myhloli343307ec747c
diff --git a/mineru/backend/hybrid/hybrid_analyze.py b/mineru/backend/hybrid/hybrid_analyze.pyindex d7d42ab4..dc373f6f 100644--- a/mineru/backend/hybrid/hybrid_analyze.py+++ b/mineru/backend/hybrid/hybrid_analyze.py@@ -124,8 +124,7 @@ HYBRID_MEDIUM_LAYOUT_LABEL_MAP = {**HYBRID_VLM_LAYOUT_LABEL_MAP,"doc_title": MineruBlockType.DOC_TITLE,"paragraph_title": MineruBlockType.PARAGRAPH_TITLE,- "figure_title": MineruBlockType.IMAGE_CAPTION,- "vision_footnote": MineruBlockType.PAGE_FOOTNOTE,+ "vision_footnote": MineruBlockType.FOOTNOTE,}HYBRID_VLM_OCR_DET_TEXT_TYPES = {@@ -181,13 +180,8 @@ def _is_hybrid_medium_ocr_det_candidate(block: dict[str, Any]) -> bool:MineruBlockType.DOC_TITLE,MineruBlockType.PARAGRAPH_TITLE,MineruBlockType.INDEX,- MineruBlockType.ABSTRACT,MineruBlockType.ASIDE_TEXT,- MineruBlockType.PHONETIC,- MineruBlockType.LIST,- MineruBlockType.CHART_CAPTION,- MineruBlockType.CHART_FOOTNOTE,- MineruBlockType.CODE_FOOTNOTE,+ MineruBlockType.FOOTNOTE,}@@ -707,7 +701,9 @@ def _medium_layout_item_to_hybrid_item(elif block_type == MineruBlockType.EQUATION:hybrid_item["content"] = _normalize_medium_content(layout_item.get("latex", ""))else:- content = _normalize_medium_content(layout_item.get("text", ""))+ content = _normalize_medium_content(layout_item.get("text", "")) or _normalize_medium_content(+ layout_item.get("content", "")+ )if content:hybrid_item["content"] = content@@ -771,6 +767,65 @@ def _run_medium_formula_recognition(return mfd_res+def _apply_medium_formula_number_ocr(+ local_context: HybridLocalModelContext,+ images_layout_res: list[list[dict[str, Any]]],+ np_images: list[np.ndarray],+) -> None:+ """对 medium formula_number 裁剪图执行 OCR-rec,并把编号文本回填到原始 layout 项。"""+ need_rec_items: list[dict[str, Any]] = []+ formula_number_crops: list[np.ndarray] = []+ for layout_res, np_img in zip(images_layout_res, np_images):+ image_h, image_w = np_img.shape[:2]+ bgr_image = cv2.cvtColor(np_img, cv2.COLOR_RGB2BGR)+ for layout_item in layout_res:+ if layout_item.get("label") != "formula_number":+ continue++ formula_number_bbox = normalize_to_int_bbox(layout_item.get("bbox"), image_size=(image_h, image_w))+ if formula_number_bbox is None:+ continue++ x0, y0, x1, y1 = formula_number_bbox+ if x1 <= x0 or y1 <= y0:+ continue++ # 使用 OCR rec 的标准旋转裁剪逻辑,保证 medium 编号裁图与正文 OCR-rec 输入一致。+ formula_number_crops.append(+ get_rotate_crop_image_for_text_rec(+ bgr_image,+ _medium_bbox_to_quad(formula_number_bbox).copy(),+ )+ )+ need_rec_items.append(layout_item)++ if not formula_number_crops:+ return++ ocr_result_list = run_ocr_inference(+ local_context.ocr_model.ocr,+ formula_number_crops,+ det=False,+ tqdm_enable=True,+ tqdm_desc="Hybrid-medium formula-number OCR-rec",+ )[0]+ if len(ocr_result_list) != len(need_rec_items):+ raise ValueError(+ "Hybrid medium formula number OCR rec result count mismatch: "+ f"ocr_result_list={len(ocr_result_list)}, need_rec_items={len(need_rec_items)}"+ )++ for layout_item, ocr_result in zip(need_rec_items, ocr_result_list):+ if not ocr_result or len(ocr_result) < 2:+ continue+ ocr_text, ocr_score = ocr_result+ normalized_text = _normalize_medium_content(ocr_text)+ layout_item["score"] = float(f"{float(ocr_score or 0.0):.3f}")+ if normalized_text:+ layout_item["text"] = normalized_text+ layout_item["content"] = normalized_text++def _medium_bbox_center(bbox: list[float] | tuple[float, ...]) -> tuple[float, float]:"""计算 bbox 中心点,供表格内联对象匹配所属表格。"""return (float(bbox[0]) + float(bbox[2])) / 2.0, (float(bbox[1]) + float(bbox[3])) / 2.0@@ -1048,6 +1103,7 @@ def _collect_medium_table_ocr_rec_inputs(atom_model_manager: Any,table_items: list[dict[str, Any]],batch_ratio: int,+ enable_ocr_det_batch: bool,) -> dict[str | None, list[dict[str, Any]]]:"""对表格裁剪图执行 OCR det,并按语言收集 OCR rec 输入。"""rec_img_lang_group: dict[str | None, list[dict[str, Any]]] = defaultdict(list)@@ -1062,6 +1118,24 @@ def _collect_medium_table_ocr_rec_inputs(if not det_images:return rec_img_lang_group+ if not enable_ocr_det_batch:+ for table_det_item in tqdm(+ table_det_items,+ total=len(table_det_items),+ desc="Hybrid-medium table OCR-det",+ ):+ dt_boxes = run_ocr_inference(+ det_ocr_engine.ocr,+ table_det_item["det_image"],+ rec=False,+ )[0]+ _append_medium_table_ocr_det_result(+ table_det_item,+ dt_boxes,+ rec_img_lang_group,+ )+ return rec_img_lang_group+det_batch_size = max(1, min(len(det_images), batch_ratio * OCR_DET_BASE_BATCH_SIZE))batch_results = run_ocr_inference(det_ocr_engine.text_detector.batch_predict,@@ -1170,9 +1244,15 @@ def _apply_medium_table_ocr_rec(atom_model_manager: Any,table_items: list[dict[str, Any]],batch_ratio: int,+ enable_ocr_det_batch: bool,) -> None:"""执行 medium 表格 OCR det/rec,并把识别结果按 table_id 写回 table_items。"""- rec_img_lang_group = _collect_medium_table_ocr_rec_inputs(atom_model_manager, table_items, batch_ratio)+ rec_img_lang_group = _collect_medium_table_ocr_rec_inputs(+ atom_model_manager,+ table_items,+ batch_ratio,+ enable_ocr_det_batch,+ )for lang, rec_img_list in rec_img_lang_group.items():if not rec_img_list:continue@@ -1296,7 +1376,12 @@ def _apply_medium_table_recognition(atom_model_manager = local_context.atom_model_manager_apply_medium_table_orientation(atom_model_manager, table_items, batch_ratio)_apply_medium_table_classification(atom_model_manager, table_items)- _apply_medium_table_ocr_rec(atom_model_manager, table_items, batch_ratio)+ _apply_medium_table_ocr_rec(+ atom_model_manager,+ table_items,+ batch_ratio,+ local_context.enable_ocr_det_batch,+ )_append_medium_table_inline_objects_to_ocr_result(table_items)_apply_medium_wireless_table_recognition(atom_model_manager, table_items)wired_table_items = _select_medium_wired_table_items(table_items)@@ -1387,6 +1472,7 @@ def _extract_with_local_layout(batch_ratio,)_apply_medium_seal_ocr(local_context, images_layout_res, np_images)+ _apply_medium_formula_number_ocr(local_context, images_layout_res, np_images)_prune_medium_empty_ocr_text_blocks(images_layout_res, _ocr_enable)medium_model_list = _build_medium_hybrid_model_list(images_layout_res, images_pil_list)ocr_res_list = _ocr_det(diff --git a/tests/unittest/test_backend_block_object_regressions.py b/tests/unittest/test_backend_block_object_regressions.pyindex 57644504..3ee3c9df 100644--- a/tests/unittest/test_backend_block_object_regressions.py+++ b/tests/unittest/test_backend_block_object_regressions.py@@ -4,6 +4,7 @@ from types import SimpleNamespacefrom PIL import Image+from mineru.backend.hybrid import hybrid_analyzefrom mineru.backend.hybrid.model_output_to_middle_json import blocks_to_page_info as hybrid_blocks_to_page_infofrom mineru.types import BlockType@@ -40,3 +41,31 @@ def test_hybrid_block_objects_sort_and_group_without_dict_access() -> None:visual_block = page_info.preproc_blocks[0]assert visual_block.type == BlockType.IMAGEassert {block.type for block in visual_block.blocks} == {BlockType.IMAGE_BODY, BlockType.IMAGE_CAPTION}+++def test_hybrid_medium_vision_footnote_groups_as_image_footnote() -> None:+ """medium 的 vision_footnote 应作为视觉脚注挂到相邻 image 下,而不是进入 page footnote。"""+ page_image = Image.new("RGB", (200, 200), "white")+ model_list = hybrid_analyze._build_medium_hybrid_model_list(+ [+ [+ {"label": "image", "bbox": [20, 20, 120, 90], "score": 0.9},+ {"label": "vision_footnote", "bbox": [24, 94, 120, 120], "text": "Figure note", "score": 0.9},+ ]+ ],+ [page_image],+ )++ page_info = hybrid_blocks_to_page_info(+ model_list[0],+ _image_dict(),+ _fake_pdf_page(),+ 0,+ _ocr_enable=True,+ _vlm_ocr_enable=False,+ )++ assert len(page_info.preproc_blocks) == 1+ visual_block = page_info.preproc_blocks[0]+ assert visual_block.type == BlockType.IMAGE+ assert {block.type for block in visual_block.blocks} == {BlockType.IMAGE_BODY, BlockType.IMAGE_FOOTNOTE}