Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat: implement specialized OCR for medium/high seal blocks and update model language

myhloli089c64152d60
diff --git a/mineru/backend/hybrid/analyze.py b/mineru/backend/hybrid/analyze.py
index 3bd137eb..1d8ebd2f 100644
--- a/mineru/backend/hybrid/analyze.py
+++ b/mineru/backend/hybrid/analyze.py
@@ -1266,6 +1266,63 @@ def _normalize_medium_content(value: Any) -> str:
return ""
+def _apply_seal_ocr(
+ local_model_context: HybridLocalModelContext,
+ model_list: list[list[dict[str, Any]]],
+ np_images: list[np.ndarray],
+) -> None:
+ """对 medium/high 最终 seal block 执行专用 OCR,并将多段文本按行写回 content。"""
+ if len(model_list) != len(np_images):
+ raise ValueError(
+ "Hybrid seal OCR page count mismatch: "
+ f"model_list={len(model_list)}, images={len(np_images)}"
+ )
+
+ for page_model_list, np_img in zip(model_list, np_images):
+ image_h, image_w = np_img.shape[:2]
+ for block_item in page_model_list:
+ if (
+ block_item.get("type") != BlockType.IMAGE
+ or block_item.get("sub_type") != "seal"
+ ):
+ continue
+
+ seal_bbox = normalize_to_int_bbox(
+ _bbox_to_pixel_bbox(block_item.get("bbox"), (image_w, image_h)),
+ image_size=(image_h, image_w),
+ )
+ if seal_bbox is None:
+ continue
+
+ x0, y0, x1, y1 = seal_bbox
+ seal_crop_rgb = np_img[y0:y1, x0:x1]
+ if seal_crop_rgb.size == 0:
+ continue
+
+ seal_crop_bgr = cv2.cvtColor(seal_crop_rgb, cv2.COLOR_RGB2BGR)
+ seal_ocr_results = local_model_context.seal_model.ocr(
+ seal_crop_bgr,
+ tqdm_enable=True,
+ tqdm_desc="OCR-seal",
+ )
+ seal_ocr_result = seal_ocr_results[0] if seal_ocr_results else []
+
+ seal_texts = []
+ for seal_item in seal_ocr_result or []:
+ if not isinstance(seal_item, (list, tuple)) or len(seal_item) != 2:
+ continue
+ rec_result = seal_item[1]
+ if not isinstance(rec_result, (list, tuple)) or not rec_result:
+ continue
+ seal_text = _normalize_medium_content(rec_result[0])
+ if seal_text:
+ seal_texts.append(seal_text)
+
+ seal_content = "\n".join(seal_texts)
+ if seal_content:
+ block_item["content"] = seal_content
+
+
def _table_bbox_center(bbox: BBox) -> tuple[float, float]:
"""计算 bbox 中心点,用于判断图片或公式应归属哪个表格。"""
return (float(bbox[0]) + float(bbox[2])) / 2.0, (float(bbox[1]) + float(bbox[3])) / 2.0
@@ -2228,6 +2285,9 @@ def doc_analyze(
images_layout_res,
)
+ if effort in {"medium", "high"}:
+ _apply_seal_ocr(hybrid_model, window_model_list, np_images)
+
model_list.extend(window_model_list)
if progress_bar is None:
progress_bar = tqdm(total=page_count, desc="Processing pages")
@@ -2273,9 +2333,9 @@ def doc_analyze(
if __name__ == "__main__":
# pdf_path = "/Users/myhloli/pdf/截断合并/demo1-3.pdf"
- # pdf_path = "/Users/myhloli/pdf/png/table_image3.png" # shubiao.png
- pdf_path = "/Users/myhloli/pdf/demo1.pdf"
+ pdf_path = "/Users/myhloli/pdf/png/seal2.jpg" # shubiao.png
+ # pdf_path = "/Users/myhloli/pdf/demo1.pdf"
pdf_bytes = read_fn(pdf_path)
- middle_json, model_list = doc_analyze(pdf_bytes, effort="low", image_analysis=True)
+ middle_json, model_list = doc_analyze(pdf_bytes, effort="medium")
logger.info(f"middle_json: {middle_json}")
logger.info(f"model_list: {model_list}")
diff --git a/mineru/backend/local_model_runtime.py b/mineru/backend/local_model_runtime.py
index 804bd60f..ae8572ea 100644
--- a/mineru/backend/local_model_runtime.py
+++ b/mineru/backend/local_model_runtime.py
@@ -417,5 +417,5 @@ class HybridLocalModelContext:
"""获取印章识别 OCR 原子模型"""
return self.atom_model_manager.get_atom_model(
atom_model_name=AtomicModel.OCR,
- lang="seal",
+ lang="seal_lite",
)