Skip to content

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

debug: trace flash image parsing stages

jinzhenj251a53295fcb
diff --git a/mineru/backend/flash/pdf_extractor.py b/mineru/backend/flash/pdf_extractor.py
index 2652e423..9bbb352a 100644
--- a/mineru/backend/flash/pdf_extractor.py
+++ b/mineru/backend/flash/pdf_extractor.py
@@ -2,18 +2,39 @@
from __future__ import annotations
+import logging
+import time
+
from ...utils.pdf_document import PDFDocument
+logger = logging.getLogger(__name__)
+
def extract_pages_text(filepath: str, start_page: int = 0, end_page: int | None = None) -> list[str]:
"""Extract plain text from each PDF page, preserving empty pages."""
pages: list[str] = []
+ open_started_at = time.perf_counter()
+ logger.debug("Flash PDFium document open started")
with PDFDocument(filepath) as pdf_doc:
page_count = pdf_doc.page_count
+ logger.debug(
+ "Flash PDFium document open completed pages=%d elapsed_ms=%d",
+ page_count,
+ round((time.perf_counter() - open_started_at) * 1000),
+ )
end = page_count if end_page is None else min(end_page, page_count)
# TODO: jzj here has issue
for page_idx in range(start_page, end):
- pages.append(pdf_doc.get_page_text(page_idx))
+ page_started_at = time.perf_counter()
+ logger.debug("Flash PDFium text extraction started page_index=%d", page_idx)
+ text = pdf_doc.get_page_text(page_idx)
+ pages.append(text)
+ logger.debug(
+ "Flash PDFium text extraction completed page_index=%d text_chars=%d elapsed_ms=%d",
+ page_idx,
+ len(text),
+ round((time.perf_counter() - page_started_at) * 1000),
+ )
return pages
diff --git a/mineru/doclib/services/parse_svc.py b/mineru/doclib/services/parse_svc.py
index 3e39349e..f1c29973 100644
--- a/mineru/doclib/services/parse_svc.py
+++ b/mineru/doclib/services/parse_svc.py
@@ -918,7 +918,18 @@ class ParseService:
server_dim = "local(flash)" if tier == "flash" else "unknown"
try:
if tier == "flash":
+ logger.debug(
+ "Local flash parse started task_id=%s path=%s page_range=%s",
+ task["id"],
+ file_row["path"],
+ page_range,
+ )
result = await self._parse_via_local(file_row, tier, page_range)
+ logger.debug(
+ "Local flash parse completed task_id=%s elapsed_ms=%d",
+ task["id"],
+ _now_ms() - execute_start_ms,
+ )
else:
result, via = await self._parse_via_api(file_row, tier, page_range, privacy)
server_dim = await self._telemetry_server_dim(via=via, tier=tier)
diff --git a/mineru/parser/pdf.py b/mineru/parser/pdf.py
index 2690118f..b20c742a 100644
--- a/mineru/parser/pdf.py
+++ b/mineru/parser/pdf.py
@@ -2,7 +2,9 @@
from __future__ import annotations
import asyncio
+import logging
import os
+import time
from abc import abstractmethod
from dataclasses import dataclass
from pathlib import Path
@@ -22,6 +24,8 @@ from ..utils.backend_options import (
from ..utils.image_payload import ImagePayloadCache
from .base import DocumentParser, ParseResult
+logger = logging.getLogger(__name__)
+
@dataclass
class _PreparedPdfInput:
@@ -149,7 +153,23 @@ class PdfBaseParser(DocumentParser):
suffix = guess_suffix_by_path(path)
if suffix in IMAGE_EXTENSIONS:
+ conversion_started_at = time.perf_counter()
+ input_size = len(pdf_bytes)
+ logger.debug(
+ "Image input conversion started filename=%s suffix=%s input_bytes=%d",
+ path.name,
+ suffix,
+ input_size,
+ )
pdf_bytes = PDFDocument.from_image(pdf_bytes).bytes
+ logger.debug(
+ "Image input conversion completed filename=%s suffix=%s input_bytes=%d pdf_bytes=%d elapsed_ms=%d",
+ path.name,
+ suffix,
+ input_size,
+ len(pdf_bytes),
+ round((time.perf_counter() - conversion_started_at) * 1000),
+ )
pdf_bytes, retained_page_indices, broken_page_indices = self._maybe_adjust_pdf_bytes(
pdf_bytes,
@@ -336,6 +356,12 @@ class PdfFlashParser(PdfBaseParser):
from ..types import Block, Line, PageInfo, Span
from ..utils.page_index import resolve_output_page_idx
+ extraction_started_at = time.perf_counter()
+ logger.debug(
+ "Flash PDF extraction started pdf_bytes=%d mapped_pages=%s",
+ len(pdf_bytes),
+ len(page_index_map) if page_index_map is not None else "all",
+ )
filepath = self._pdf_bytes_to_tempfile(pdf_bytes)
try:
pages_text = extract_pages_text(filepath)
@@ -344,6 +370,11 @@ class PdfFlashParser(PdfBaseParser):
os.unlink(filepath)
except OSError:
pass
+ logger.debug(
+ "Flash PDF extraction completed pages=%d elapsed_ms=%d",
+ len(pages_text),
+ round((time.perf_counter() - extraction_started_at) * 1000),
+ )
pages: list[PageInfo] = []
block_idx = 0
diff --git a/mineru/utils/pdf_document.py b/mineru/utils/pdf_document.py
index d8db7d94..074850ef 100644
--- a/mineru/utils/pdf_document.py
+++ b/mineru/utils/pdf_document.py
@@ -2,7 +2,9 @@
from __future__ import annotations
import asyncio
+import logging
import os
+import time
from contextlib import contextmanager
from io import BytesIO
from typing import Any, Iterator, Literal, TypeAlias, cast
@@ -21,6 +23,8 @@ from .pdf_image_tools import get_crop_img, load_images_from_pdf_bytes_range
from .pdf_reader import image_to_bytes
from .pdfium_guard import _pdfium_lock, safe_rewrite_pdf_bytes_with_pdfium
+logger = logging.getLogger(__name__)
+
POINTS_PER_INCH: int = 72
DEFAULT_RENDER_DPI: int = 200
DEFAULT_RENDER_SCALE: float = DEFAULT_RENDER_DPI / POINTS_PER_INCH
@@ -110,18 +114,54 @@ class PDFDocument:
render_scale: float = DEFAULT_RENDER_SCALE,
render_max_edge: int = DEFAULT_RENDER_MAX_EDGE,
) -> "PDFDocument":
+ open_started_at = time.perf_counter()
+ logger.debug("Pillow image open started input_bytes=%d", len(image_bytes))
image = Image.open(BytesIO(image_bytes))
+ logger.debug(
+ "Pillow image open completed format=%s mode=%s size=%sx%s elapsed_ms=%d",
+ image.format,
+ image.mode,
+ image.width,
+ image.height,
+ round((time.perf_counter() - open_started_at) * 1000),
+ )
+
# 根据 EXIF 信息自动转正(处理手机拍摄的带 Orientation 标记的图片)
+ transpose_started_at = time.perf_counter()
+ logger.debug("Pillow EXIF transpose started")
image = ImageOps.exif_transpose(image) or image
+ logger.debug(
+ "Pillow EXIF transpose completed mode=%s size=%sx%s elapsed_ms=%d",
+ image.mode,
+ image.width,
+ image.height,
+ round((time.perf_counter() - transpose_started_at) * 1000),
+ )
# 只在必要时转换
if image.mode != "RGB":
+ source_mode = image.mode
+ conversion_started_at = time.perf_counter()
+ logger.debug("Pillow RGB conversion started source_mode=%s", source_mode)
image = image.convert("RGB")
+ logger.debug(
+ "Pillow RGB conversion completed source_mode=%s elapsed_ms=%d",
+ source_mode,
+ round((time.perf_counter() - conversion_started_at) * 1000),
+ )
render_dpi = max(1, int(round(render_scale * POINTS_PER_INCH)))
with BytesIO() as pdf_buffer:
# 第一张图保存为 PDF,其余追加
+ save_started_at = time.perf_counter()
+ logger.debug(
+ "Pillow PDF encoding started mode=%s size=%sx%s dpi=%d",
+ image.mode,
+ image.width,
+ image.height,
+ render_dpi,
+ )
image.save(
pdf_buffer,
format="PDF",
@@ -130,6 +170,11 @@ class PDFDocument:
subsampling=0,
)
pdf_bytes = pdf_buffer.getvalue()
+ logger.debug(
+ "Pillow PDF encoding completed output_bytes=%d elapsed_ms=%d",
+ len(pdf_bytes),
+ round((time.perf_counter() - save_started_at) * 1000),
+ )
return PDFDocument(
pdf_bytes,