Skip to content

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

feat: implement formula region masking for OCR detection to improve text recognition accuracy

myhlolid25879fcadfd
diff --git a/mineru/backend/hybrid/hybrid_analyze.py b/mineru/backend/hybrid/hybrid_analyze.py
index 76cbcfee..34cb066f 100644
--- a/mineru/backend/hybrid/hybrid_analyze.py
+++ b/mineru/backend/hybrid/hybrid_analyze.py
@@ -13,12 +13,11 @@ from mineru_vl_utils.structs import BlockType, ContentBlock
from tqdm import tqdm
from mineru.backend.hybrid.hybrid_model_output_to_middle_json import (
- apply_server_side_postprocess,
append_page_model_list_to_middle_json,
+ apply_server_side_postprocess,
finalize_middle_json,
init_middle_json,
)
-from mineru.backend.utils.runtime_utils import exclude_progress_bar_idle_time
from mineru.backend.pipeline.model_init import (
HybridModelSingleton,
run_layout_inference,
@@ -27,20 +26,32 @@ from mineru.backend.pipeline.model_init import (
)
from mineru.backend.pipeline.model_list import AtomicModel
from mineru.backend.utils.formula_number import optimize_hybrid_formula_number_blocks
+from mineru.backend.utils.runtime_utils import exclude_progress_bar_idle_time
from mineru.backend.vlm.vlm_analyze import (
ModelSingleton,
+ _get_model_async,
+ _maybe_enable_serial_execution,
aio_predictor_execution_guard,
predictor_execution_guard,
- _maybe_enable_serial_execution,
- _get_model_async,
)
from mineru.data.data_reader_writer import DataWriter
from mineru.utils.boxbase import calculate_overlap_area_2_minbox_area_ratio
-from mineru.utils.config_reader import get_device, get_processing_window_size
-from mineru.utils.enum_class import ImageType, NotExtractType, BlockType as MineruBlockType
-from mineru.utils.model_utils import crop_img, get_vram, clean_memory
-from mineru.utils.ocr_utils import get_adjusted_mfdetrec_res, get_ocr_result_list, sorted_boxes, merge_det_boxes, \
- update_det_boxes
+from mineru.utils.config_reader import (
+ get_device,
+ get_ocr_det_mask_inline_formula_enable,
+ get_processing_window_size,
+)
+from mineru.utils.enum_class import BlockType as MineruBlockType
+from mineru.utils.enum_class import ImageType, NotExtractType
+from mineru.utils.model_utils import clean_memory, crop_img, get_vram
+from mineru.utils.ocr_utils import (
+ get_adjusted_mfdetrec_res,
+ get_ocr_result_list,
+ mask_formula_regions_for_ocr_det,
+ merge_det_boxes,
+ sorted_boxes,
+ update_det_boxes,
+)
from mineru.utils.pdf_classify import classify
from mineru.utils.pdf_image_tools import (
aio_load_images_from_pdf_bytes_range,
@@ -136,6 +147,8 @@ def ocr_det(
mfd_res,
batch_ratio: int = 1,
):
+ mask_formula_for_ocr_det = get_ocr_det_mask_inline_formula_enable(True)
+
def _set_temp_pixel_bbox(res, pixel_bbox):
res["_normalized_bbox"] = list(res["bbox"])
res["bbox"] = pixel_bbox
@@ -175,9 +188,14 @@ def ocr_det(
page_mfd_res, useful_list
)
bgr_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
+ det_image = (
+ mask_formula_regions_for_ocr_det(bgr_image, adjusted_mfdetrec_res)
+ if mask_formula_for_ocr_det
+ else bgr_image
+ )
ocr_res = run_ocr_inference(
hybrid_pipeline_model.ocr_model.ocr,
- bgr_image,
+ det_image,
mfd_res=adjusted_mfdetrec_res,
rec=False,
)[0]
@@ -221,8 +239,13 @@ def ocr_det(
page_mfd_res, useful_list
)
bgr_image = cv2.cvtColor(new_image, cv2.COLOR_RGB2BGR)
+ det_image = (
+ mask_formula_regions_for_ocr_det(bgr_image, adjusted_mfdetrec_res)
+ if mask_formula_for_ocr_det
+ else bgr_image
+ )
all_cropped_images_info.append((
- bgr_image, useful_list, adjusted_mfdetrec_res, ocr_res_list[-1]
+ bgr_image, det_image, useful_list, adjusted_mfdetrec_res, ocr_res_list[-1]
))
# 按分辨率分组并同时完成padding
@@ -230,7 +253,7 @@ def ocr_det(
resolution_groups = defaultdict(list)
for crop_info in all_cropped_images_info:
- cropped_img = crop_info[0]
+ cropped_img = crop_info[1]
h, w = cropped_img.shape[:2]
# 直接计算目标尺寸并用作分组键
target_h = ((h + RESOLUTION_GROUP_STRIDE - 1) // RESOLUTION_GROUP_STRIDE) * RESOLUTION_GROUP_STRIDE
@@ -239,11 +262,11 @@ def ocr_det(
resolution_groups[group_key].append(crop_info)
# 对每个分辨率组进行批处理
- for (target_h, target_w), group_crops in tqdm(resolution_groups.items(), desc=f"OCR-det"):
+ for (target_h, target_w), group_crops in tqdm(resolution_groups.items(), desc="OCR-det"):
# 对所有图像进行padding到统一尺寸
batch_images = []
for crop_info in group_crops:
- img = crop_info[0]
+ img = crop_info[1]
h, w = img.shape[:2]
# 创建目标尺寸的白色背景
padded_img = np.ones((target_h, target_w, 3), dtype=np.uint8) * 255
@@ -260,7 +283,7 @@ def ocr_det(
# 处理批处理结果
for crop_info, (dt_boxes, _) in zip(group_crops, batch_results):
- bgr_image, useful_list, adjusted_mfdetrec_res, ocr_page_res_list = crop_info
+ bgr_image, _det_image, useful_list, adjusted_mfdetrec_res, ocr_page_res_list = crop_info
if dt_boxes is not None and len(dt_boxes) > 0:
# 处理检测框
@@ -284,28 +307,6 @@ def ocr_det(
ocr_page_res_list.extend(ocr_result_list)
return ocr_res_list
-def mask_image_regions(np_images, model_list):
- # 根据vlm返回的结果,在每一页中将image、table、equation块mask成白色背景图像
- for np_image, vlm_page_results in zip(np_images, model_list):
- img_height, img_width = np_image.shape[:2]
- # 收集需要mask的区域
- mask_regions = []
- for block in vlm_page_results:
- if block['type'] in [BlockType.IMAGE, BlockType.TABLE, BlockType.EQUATION]:
- bbox = block['bbox']
- # 批量转换归一化坐标到像素坐标,并进行边界检查
- x0 = max(0, int(bbox[0] * img_width))
- y0 = max(0, int(bbox[1] * img_height))
- x1 = min(img_width, int(bbox[2] * img_width))
- y1 = min(img_height, int(bbox[3] * img_height))
- # 只添加有效区域
- if x1 > x0 and y1 > y0:
- mask_regions.append((y0, y1, x0, x1))
- # 批量应用mask
- for y0, y1, x0, x1 in mask_regions:
- np_image[y0:y1, x0:x1, :] = 255
- return np_images
-
def normalize_bbox_to_unit(item, page_width, page_height):
"""将像素级bbox归一化为[0, 1]区间"""
diff --git a/mineru/backend/pipeline/batch_analyze.py b/mineru/backend/pipeline/batch_analyze.py
index bc60237d..a0420304 100644
--- a/mineru/backend/pipeline/batch_analyze.py
+++ b/mineru/backend/pipeline/batch_analyze.py
@@ -2,35 +2,38 @@
import base64
import html
import re
+from collections import defaultdict
import cv2
+import numpy as np
from loguru import logger
from tqdm import tqdm
-from collections import defaultdict
-import numpy as np
-from .model_init import (
- AtomModelSingleton,
- run_layout_inference,
- run_mfr_inference,
- run_ocr_inference,
-)
-from .model_list import AtomicModel
+from ...utils.bbox_utils import normalize_to_int_bbox
from ...utils.config_reader import (
get_formula_enable,
get_ocr_det_mask_inline_formula_enable,
get_table_enable,
)
-from ...utils.bbox_utils import normalize_to_int_bbox
-from ...utils.model_utils import crop_img, get_res_list_from_layout_res, clean_vram
-from ...utils.ocr_utils import merge_det_boxes, update_det_boxes, sorted_boxes
+from ...utils.model_utils import clean_vram, crop_img, get_res_list_from_layout_res
from ...utils.ocr_utils import (
+ OcrConfidence,
get_adjusted_mfdetrec_res,
get_ocr_result_list,
- OcrConfidence,
get_rotate_crop_image_for_text_rec,
+ mask_formula_regions_for_ocr_det,
+ merge_det_boxes,
+ sorted_boxes,
+ update_det_boxes,
)
from ...utils.pdf_image_tools import get_crop_np_img
+from .model_init import (
+ AtomModelSingleton,
+ run_layout_inference,
+ run_mfr_inference,
+ run_ocr_inference,
+)
+from .model_list import AtomicModel
LAYOUT_BASE_BATCH_SIZE = 1
MFR_BASE_BATCH_SIZE = 16
@@ -78,24 +81,7 @@ class BatchAnalyze:
bgr_image: np.ndarray,
mask_boxes: list[dict] | None,
) -> np.ndarray:
- if not mask_boxes:
- return bgr_image
-
- masked_image = bgr_image.copy()
- image_h, image_w = masked_image.shape[:2]
- for mask_box in mask_boxes:
- bbox = mask_box.get("bbox")
- if bbox is None:
- continue
-
- int_bbox = normalize_to_int_bbox(bbox, image_size=(image_h, image_w))
- if int_bbox is None:
- continue
-
- x0, y0, x1, y1 = int_bbox
- masked_image[y0:y1, x0:x1] = 255
-
- return masked_image
+ return mask_formula_regions_for_ocr_det(bgr_image, mask_boxes)
def _get_masked_det_image(
self,
diff --git a/mineru/utils/ocr_utils.py b/mineru/utils/ocr_utils.py
index b80081da..a63955f0 100644
--- a/mineru/utils/ocr_utils.py
+++ b/mineru/utils/ocr_utils.py
@@ -1,5 +1,6 @@
# Copyright (c) Opendatalab. All rights reserved.
import copy
+
import cv2
import numpy as np
@@ -14,6 +15,31 @@ LINE_WIDTH_TO_HEIGHT_RATIO_THRESHOLD = 4 # 一般情况下,行宽度超过高
TEXT_REC_ROTATE_RATIO = 1.5
+def mask_formula_regions_for_ocr_det(
+ bgr_image: np.ndarray,
+ mask_boxes: list[dict] | None,
+) -> np.ndarray:
+ """在 OCR det 前将公式区域置白,避免公式干扰相邻文本行检测。"""
+ if not mask_boxes:
+ return bgr_image
+
+ masked_image = bgr_image.copy()
+ image_h, image_w = masked_image.shape[:2]
+ for mask_box in mask_boxes:
+ bbox = mask_box.get("bbox")
+ if bbox is None:
+ continue
+
+ int_bbox = normalize_to_int_bbox(bbox, image_size=(image_h, image_w))
+ if int_bbox is None:
+ continue
+
+ x0, y0, x1, y1 = int_bbox
+ masked_image[y0:y1, x0:x1] = 255
+
+ return masked_image
+
+
def merge_spans_to_line(spans, threshold=0.6):
if len(spans) == 0:
return []