Skip to content

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

feat: refactor bounding box normalization to use utility function for improved consistency

myhloli695cee451d4a
diff --git a/mineru/backend/pipeline/batch_analyze.py b/mineru/backend/pipeline/batch_analyze.py
index e0b92c55..3d4af93f 100644
--- a/mineru/backend/pipeline/batch_analyze.py
+++ b/mineru/backend/pipeline/batch_analyze.py
@@ -15,6 +15,7 @@ from ...model.table.rec.unet_table.utils_table_recover import (
sorted_ocr_boxes as sorted_table_boxes,
)
from ...utils.config_reader import get_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.ocr_utils import get_adjusted_mfdetrec_res, get_ocr_result_list, OcrConfidence, get_rotate_crop_image
@@ -37,32 +38,8 @@ class BatchAnalyze:
self.enable_ocr_det_batch = enable_ocr_det_batch
@staticmethod
- def _normalize_bbox(box) -> list[float] | None:
- arr = np.asarray(box, dtype=np.float32)
- if arr.size == 0:
- return None
- if arr.ndim == 2 and arr.shape[-1] == 2:
- xs = arr[:, 0]
- ys = arr[:, 1]
- else:
- flat = arr.reshape(-1)
- if flat.size == 4:
- return [
- float(flat[0]),
- float(flat[1]),
- float(flat[2]),
- float(flat[3]),
- ]
- if flat.size < 8:
- return None
- xs = flat[0::2]
- ys = flat[1::2]
- return [
- float(np.min(xs)),
- float(np.min(ys)),
- float(np.max(xs)),
- float(np.max(ys)),
- ]
+ def _normalize_bbox(box) -> list[int] | None:
+ return normalize_to_int_bbox(box)
@staticmethod
def _bbox_center(bbox) -> tuple[float, float]:
@@ -76,9 +53,9 @@ class BatchAnalyze:
)
@staticmethod
- def _rotate_local_bbox(local_bbox: list[float], table_width: float, table_height: float, rotate_label: str) -> list[float]:
+ def _rotate_local_bbox(local_bbox: list[float], table_width: float, table_height: float, rotate_label: str) -> list[int]:
if rotate_label not in {"90", "270"}:
- return local_bbox
+ return normalize_to_int_bbox(local_bbox)
corners = np.asarray(
[
@@ -99,12 +76,7 @@ class BatchAnalyze:
[[y, table_width - x] for x, y in corners],
dtype=np.float32,
)
- return [
- float(np.min(rotated[:, 0])),
- float(np.min(rotated[:, 1])),
- float(np.max(rotated[:, 0])),
- float(np.max(rotated[:, 1])),
- ]
+ return normalize_to_int_bbox(rotated)
def _page_bbox_to_table_bbox(self, item_bbox: list[float], table_bbox: list[float], rotate_label: str) -> list[float]:
table_width = float(table_bbox[2] - table_bbox[0])
@@ -120,12 +92,10 @@ class BatchAnalyze:
@staticmethod
def _crop_image_to_data_uri(page_np_img: np.ndarray, bbox: list[float]) -> str:
h, w = page_np_img.shape[:2]
- x0 = max(int(np.floor(bbox[0])), 0)
- y0 = max(int(np.floor(bbox[1])), 0)
- x1 = min(int(np.ceil(bbox[2])), w)
- y1 = min(int(np.ceil(bbox[3])), h)
- if x0 >= x1 or y0 >= y1:
+ int_bbox = normalize_to_int_bbox(bbox, image_size=(h, w))
+ if int_bbox is None:
return ""
+ x0, y0, x1, y1 = int_bbox
crop = page_np_img[y0:y1, x0:x1]
if crop.size == 0:
@@ -344,10 +314,11 @@ class BatchAnalyze:
for table_res in table_res_list:
def get_crop_table_img(scale):
- crop_xmin, crop_ymin, crop_xmax, crop_ymax = [
- int(v) for v in table_res["bbox"]
- ]
- bbox = (int(crop_xmin / scale), int(crop_ymin / scale), int(crop_xmax / scale), int(crop_ymax / scale))
+ bbox = normalize_to_int_bbox(
+ [float(v) / float(scale) for v in table_res["bbox"]]
+ )
+ if bbox is None:
+ return np_img[0:0, 0:0]
return get_crop_np_img(bbox, np_img, scale=scale)
wireless_table_img = get_crop_table_img(scale = 1)
diff --git a/mineru/utils/model_utils.py b/mineru/utils/model_utils.py
index 01467e18..876acadc 100644
--- a/mineru/utils/model_utils.py
+++ b/mineru/utils/model_utils.py
@@ -1,3 +1,4 @@
+import math
import os
import time
import gc
@@ -40,7 +41,7 @@ def _get_bbox(item):
def _get_int_bbox(item):
xmin, ymin, xmax, ymax = _get_bbox(item)
- return int(xmin), int(ymin), int(xmax), int(ymax)
+ return math.floor(xmin), math.floor(ymin), math.ceil(xmax), math.ceil(ymax)
def _set_bbox(item, bbox):
diff --git a/mineru/utils/ocr_utils.py b/mineru/utils/ocr_utils.py
index 58767583..eb332312 100644
--- a/mineru/utils/ocr_utils.py
+++ b/mineru/utils/ocr_utils.py
@@ -3,6 +3,8 @@ import copy
import cv2
import numpy as np
+from .bbox_utils import normalize_to_int_bbox
+
class OcrConfidence:
min_confidence = 0.5
@@ -386,12 +388,9 @@ def get_ocr_result_list(
p3 = [p3[0] - paste_x + xmin, p3[1] - paste_y + ymin]
p4 = [p4[0] - paste_x + xmin, p4[1] - paste_y + ymin]
- bbox = [
- float(round(p1[0], 4)),
- float(round(p1[1], 4)),
- float(round(p3[0], 4)),
- float(round(p3[1], 4)),
- ]
+ bbox = normalize_to_int_bbox([p1, p2, p3, p4])
+ if bbox is None:
+ continue
ocr_item = {
"label": "ocr_text",
diff --git a/mineru/utils/pdf_image_tools.py b/mineru/utils/pdf_image_tools.py
index 20a8cad6..a1438344 100644
--- a/mineru/utils/pdf_image_tools.py
+++ b/mineru/utils/pdf_image_tools.py
@@ -11,6 +11,7 @@ from PIL import Image, ImageOps
from mineru.data.data_reader_writer import FileBasedDataWriter
from mineru.utils.check_sys_env import is_windows_environment
+from mineru.utils.bbox_utils import normalize_to_int_bbox
from mineru.utils.os_env_config import get_load_images_timeout, get_load_images_threads
from mineru.utils.pdf_reader import image_to_b64str, image_to_bytes, page_to_image
from mineru.utils.enum_class import ImageType
@@ -246,13 +247,10 @@ def cut_image(
def get_crop_img(bbox: tuple, pil_img, scale=2):
- scale_bbox = (
- int(bbox[0] * scale),
- int(bbox[1] * scale),
- int(bbox[2] * scale),
- int(bbox[3] * scale),
- )
- return pil_img.crop(scale_bbox)
+ scale_bbox = normalize_to_int_bbox([float(v) * scale for v in bbox])
+ if scale_bbox is None:
+ return pil_img.crop((0, 0, 0, 0))
+ return pil_img.crop(tuple(scale_bbox))
def get_crop_np_img(bbox: tuple, input_img, scale=2):
@@ -263,12 +261,13 @@ def get_crop_np_img(bbox: tuple, input_img, scale=2):
else:
raise ValueError("Input must be a pillow object or a numpy array.")
- scale_bbox = (
- int(bbox[0] * scale),
- int(bbox[1] * scale),
- int(bbox[2] * scale),
- int(bbox[3] * scale),
+ height, width = np_img.shape[:2]
+ scale_bbox = normalize_to_int_bbox(
+ [float(v) * scale for v in bbox],
+ image_size=(height, width),
)
+ if scale_bbox is None:
+ return np_img[0:0, 0:0]
return np_img[scale_bbox[1] : scale_bbox[3], scale_bbox[0] : scale_bbox[2]]