Skip to content

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

feat: enhance block handling by introducing sub_type logic for images and charts

myhlolib8734899b8a9
diff --git a/mineru/backend/hybrid/hybrid_magic_model.py b/mineru/backend/hybrid/hybrid_magic_model.py
index 2ee042e1..ec40d5e0 100644
--- a/mineru/backend/hybrid/hybrid_magic_model.py
+++ b/mineru/backend/hybrid/hybrid_magic_model.py
@@ -87,8 +87,14 @@ class MagicModel:
try:
block_bbox = self.cal_real_bbox(block_info["bbox"])
block_type = block_info["type"]
+ raw_block_type = block_type
block_content = block_info.get("content")
block_angle = block_info.get("angle", 0)
+ block_sub_type = (
+ block_info.get("sub_type")
+ if raw_block_type in ["image", "chart"]
+ else None
+ )
except Exception as e:
# 如果解析失败,可能是因为格式不正确,跳过这个块
logger.warning(f"Invalid block format: {block_info}, error: {e}")
@@ -150,7 +156,7 @@ class MagicModel:
}
if span_type == ContentType.TABLE:
span["html"] = block_content
- elif span_type == ContentType.CHART and block_content:
+ elif raw_block_type in ["image", "chart"] and block_content is not None:
span["content"] = block_content
elif span_type == ContentType.INTERLINE_EQUATION:
span = {
@@ -264,6 +270,8 @@ class MagicModel:
"lines": [line],
"index": index,
}
+ if block_sub_type:
+ block["sub_type"] = block_sub_type
else:
block_spans = []
for span in page_text_inline_formula_spans:
diff --git a/mineru/backend/vlm/vlm_magic_model.py b/mineru/backend/vlm/vlm_magic_model.py
index 1883dcd5..a64c39a0 100644
--- a/mineru/backend/vlm/vlm_magic_model.py
+++ b/mineru/backend/vlm/vlm_magic_model.py
@@ -39,8 +39,14 @@ class MagicModel:
y_1, y_2 = y_2, y_1
block_bbox = (x_1, y_1, x_2, y_2)
block_type = block_info["type"]
+ raw_block_type = block_type
block_content = block_info.get("content")
block_angle = block_info.get("angle", 0)
+ block_sub_type = (
+ block_info.get("sub_type")
+ if raw_block_type in ["image", "chart"]
+ else None
+ )
except Exception as e:
# 如果解析失败,可能是因为格式不正确,跳过这个块
logger.warning(f"Invalid block format: {block_info}, error: {e}")
@@ -101,7 +107,7 @@ class MagicModel:
}
if span_type == ContentType.TABLE:
span["html"] = block_content
- elif span_type == ContentType.CHART and block_content:
+ elif raw_block_type in ["image", "chart"] and block_content is not None:
span["content"] = block_content
elif span_type == ContentType.INTERLINE_EQUATION:
span = {
@@ -199,15 +205,17 @@ class MagicModel:
else:
line = {"bbox": block_bbox, "spans": spans}
- blocks.append(
- {
- "bbox": block_bbox,
- "type": block_type,
- "angle": block_angle,
- "lines": [line],
- "index": index,
- }
- )
+ block = {
+ "bbox": block_bbox,
+ "type": block_type,
+ "angle": block_angle,
+ "lines": [line],
+ "index": index,
+ }
+ if block_sub_type:
+ block["sub_type"] = block_sub_type
+
+ blocks.append(block)
self.image_blocks = []
self.table_blocks = []
diff --git a/mineru/utils/visual_magic_model_utils.py b/mineru/utils/visual_magic_model_utils.py
index 66372688..2b06e267 100644
--- a/mineru/utils/visual_magic_model_utils.py
+++ b/mineru/utils/visual_magic_model_utils.py
@@ -135,6 +135,7 @@ def regroup_visual_blocks(blocks):
body_block = dict(main_block)
body_block["type"] = mapping["body"]
body_block.pop("sub_images", None)
+ body_block.pop("sub_type", None)
captions = []
for caption in sorted(
@@ -160,6 +161,8 @@ def regroup_visual_blocks(blocks):
"blocks": [body_block, *captions, *footnotes],
"index": body_block["index"],
}
+ if visual_type in [BlockType.IMAGE, BlockType.CHART] and main_block.get("sub_type"):
+ two_layer_block["sub_type"] = main_block["sub_type"]
if visual_type == BlockType.IMAGE and main_block.get("sub_images"):
two_layer_block["sub_images"] = main_block["sub_images"]
two_layer_block["blocks"].sort(key=lambda x: x["index"])