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: refactor OCR result processing and add PDF page retrieval for window analysis
myhloli61d71ea25f4a
diff --git a/mineru/backend/hybrid/analyze.py b/mineru/backend/hybrid/analyze.pyindex 7d7d2528..e87d4af8 100644--- a/mineru/backend/hybrid/analyze.py+++ b/mineru/backend/hybrid/analyze.py@@ -435,7 +435,6 @@ def _normalize_batch_ocr_det_boxes(dt_boxes: Any, adjusted_mfdetrec_res: list[Andef _append_ocr_det_result(- local_context: Any,crop: _OcrDetCrop,ocr_res: Any,need_rec_img: bool,@@ -448,7 +447,6 @@ def _append_ocr_det_result(crop.useful_list,need_rec_img,crop.bgr_image,- local_context.lang,)crop.page_ocr_res_list.extend(ocr_result_list)@@ -478,7 +476,7 @@ def _ocr_det(dt_boxes_final = _normalize_batch_ocr_det_boxes(dt_boxes, crop.adjusted_mfdetrec_res)if dt_boxes_final:ocr_res = [box.tolist() if hasattr(box, "tolist") else box for box in dt_boxes_final]- _append_ocr_det_result(local_model_context, crop, ocr_res, need_rec_img)+ _append_ocr_det_result(crop, ocr_res, need_rec_img)return ocr_res_list@@ -878,7 +876,7 @@ def doc_analyze(else:raise ValueError(f"Unsupported parse mode: {parse_mode}")- _process_text_and_formulas(+ window_model_list = _process_text_and_formulas(images_pil_list,window_model_list,parse_mode,diff --git a/mineru/backend/hybrid/hybrid_analyze.py b/mineru/backend/hybrid/hybrid_analyze.pyindex a1e7478d..06753797 100644--- a/mineru/backend/hybrid/hybrid_analyze.py+++ b/mineru/backend/hybrid/hybrid_analyze.py@@ -40,7 +40,7 @@ from ...utils.ocr_utils import (sorted_boxes,update_det_boxes,)-from ...utils.pdf_document import PDFDocument+from ...utils.pdf_document import PDFDocument, PDFPagefrom ...utils.pdf_image_tools import aio_load_images_from_pdf_bytes_range, load_images_from_pdf_bytes_rangefrom ...utils.bbox_utils import normalize_to_int_bboxfrom ...utils.pdf_image_tools import get_crop_np_img@@ -319,6 +319,11 @@ def _build_processing_windows(page_count: int, configured_window_size: int) -> l]+def _get_window_pdf_pages(pdf_doc: PDFDocument, window: _ProcessingWindow) -> list[PDFPage]:+ """按窗口闭区间获取轻量 PDFPage 代理列表,供窗口内后续处理复用。"""+ return [pdf_doc[page_idx] for page_idx in range(window.start, window.end + 1)]++def _log_processing_window_plan(page_count: int, configured_window_size: int, total_windows: int) -> None:"""输出 Hybrid 分段处理计划日志,避免同步和异步入口文案漂移。"""logger.info(@@ -2225,6 +2230,7 @@ def doc_analyze(last_append_end_time = Nonetry:for window in windows:+ pdf_pages = _get_window_pdf_pages(pdf_doc, window)images_list = load_images_from_pdf_bytes_range(pdf_bytes=pdf_bytes,start_page_id=window.start,@@ -2233,6 +2239,8 @@ def doc_analyze()try:images_pil_list = [image_dict["img_pil"] for image_dict in images_list]+ if len(pdf_pages) != len(images_pil_list):+ raise ValueError("Hybrid processing window PDF page count does not match image count")_log_processing_window(window, page_count, len(images_pil_list))if plan.extractor == "medium_local":window_model_list, local_context = _extract_with_local_layout(@@ -2376,6 +2384,7 @@ async def aio_doc_analyze(last_append_end_time = Nonetry:for window in windows:+ pdf_pages = _get_window_pdf_pages(pdf_doc, window)images_list = await aio_load_images_from_pdf_bytes_range(pdf_bytes,start_page_id=window.start,@@ -2384,6 +2393,8 @@ async def aio_doc_analyze()try:images_pil_list = [image_dict["img_pil"] for image_dict in images_list]+ if len(pdf_pages) != len(images_pil_list):+ raise ValueError("Hybrid processing window PDF page count does not match image count")_log_processing_window(window, page_count, len(images_pil_list))if plan.extractor == "medium_local":window_model_list, local_context = await asyncio.to_thread(diff --git a/mineru/utils/ocr_utils.py b/mineru/utils/ocr_utils.pyindex 4e1f0c0c..94d8c6a3 100644--- a/mineru/utils/ocr_utils.py+++ b/mineru/utils/ocr_utils.py@@ -367,7 +367,6 @@ def get_ocr_result_list(useful_list: list[int],ocr_enable: bool,bgr_image: np.ndarray,- lang: str,) -> list[dict[str, Any]]:paste_x, paste_y, xmin, ymin, xmax, ymax, new_width, new_height = useful_listocr_result_list = []@@ -430,8 +429,6 @@ def get_ocr_result_list(}if need_ocr_rec:ocr_item["np_img"] = img_crop- ocr_item["lang"] = lang- ocr_item["_need_ocr_rec"] = Trueocr_result_list.append(ocr_item)return ocr_result_list