fix(knowdge-base): 手册/设计类 PDF 不抑制图片,并对保留图片跑 OCR,把 图中文写进md再同步

This commit is contained in:
sophie committed 2026-07-10 10:18:08 +08:00
1 parent 64205b72bd
commit 0939aeb0d8
5 files changed
+626 -17

No files matched your search

+94 -3
View File
@@ -1,3 +1,11 @@
"""
edkb_system/skills/loader_pdf.py
用途:解析 PDF 为 Block 列表(正文、嵌入图、扫描页整页图)。
依赖:pypdf / pypdfium2、edkb_system.contracts、common.is_figure_rich_handbook。
逻辑:由 controller 在 PDF 分支调用;手册类文件名会放宽嵌图保留阈值。
"""
from __future__ import annotations
import base64
@@ -7,6 +15,7 @@ import re
import xml.etree.ElementTree as ET
from edkb_system.contracts import Block, Context
from edkb_system.skills.common import is_figure_rich_handbook
_PAGE_IMAGE_RENDER_SCALE = 0.75
_PAGE_IMAGE_JPEG_QUALITY = 82
@@ -15,6 +24,9 @@ _DEFAULT_PAGE_TEXT_LIMIT = 120
_MIN_SCAN_IMAGE_BYTES = 4 * 1024
_MIN_DEFAULT_IMAGE_BYTES = 16 * 1024
_MIN_TEXT_HEAVY_IMAGE_BYTES = 24 * 1024
# 图文手册:正文页也保留较大嵌入图,供后续 OCR「图中文字」。
_MIN_FIGURE_HANDBOOK_IMAGE_BYTES = 12 * 1024
_FIGURE_HANDBOOK_PAGE_TEXT_LIMIT = 400
_XFA_ROW_CONTAINER_TAGS = {"DATA"}
_XFA_SKIP_TAGS = {
"config",
@@ -44,11 +56,33 @@ def _should_keep_embedded_image(
page_text_chars: int,
scan_likely: bool,
text_heavy_pdf: bool,
figure_rich_handbook: bool = False,
) -> bool:
"""
判断 PDF 页内嵌入图是否保留为 image block。
核心逻辑:扫描件放宽体积阈值;正文密集 PDF 默认只保留「少字页上的大图」;
手册类则允许正文页保留 ≥12KB 的图,避免机械设计图被滤掉。
Args:
data_size: 图片字节数。
page_text_chars: 当页提取到的正文字符数。
scan_likely: 是否判定为扫描件。
text_heavy_pdf: 是否全文偏文本密集。
figure_rich_handbook: 是否图文手册策略。
Returns:
True 表示保留该嵌入图。
"""
if data_size <= 0:
return False
if scan_likely:
return data_size >= _MIN_SCAN_IMAGE_BYTES
if figure_rich_handbook:
return (
data_size >= _MIN_FIGURE_HANDBOOK_IMAGE_BYTES
and page_text_chars <= _FIGURE_HANDBOOK_PAGE_TEXT_LIMIT
)
if text_heavy_pdf:
return page_text_chars <= _TEXT_HEAVY_PAGE_TEXT_LIMIT and data_size >= _MIN_TEXT_HEAVY_IMAGE_BYTES
return page_text_chars <= 80 or data_size >= _MIN_DEFAULT_IMAGE_BYTES
@@ -181,7 +215,27 @@ def _should_use_page_image_mode(
image_count: int,
largest_ratio: float,
total_ratio: float,
figure_rich_handbook: bool = False,
) -> bool:
"""
判断当页是否改用「整页渲染图」模式(便于 OCR)。
核心逻辑:扫描件/少字大图页走整页图;手册类在嵌图很少但版面以图为主时也启用。
Args:
scan_likely / text_heavy_pdf: PDF 全局特征。
text_chars / image_count / largest_ratio / total_ratio: 当页特征。
figure_rich_handbook: 手册策略开关。
Returns:
True 表示渲染整页图而非仅保留嵌入图。
"""
if figure_rich_handbook:
# 手册扫描分片:嵌图提取失败时,用整页图兜底 OCR
if image_count == 0 and (largest_ratio >= 0.35 or total_ratio >= 0.4 or text_chars < 80):
return True
if largest_ratio >= 0.55 or total_ratio >= 0.65:
return True
if text_heavy_pdf and text_chars > _TEXT_HEAVY_PAGE_TEXT_LIMIT:
return False
if scan_likely:
@@ -201,9 +255,22 @@ def _should_use_page_image_mode(
def _analysis_candidate_pages(
page_payloads: list[dict], scan_likely: bool, text_heavy_pdf: bool
page_payloads: list[dict],
scan_likely: bool,
text_heavy_pdf: bool,
figure_rich_handbook: bool = False,
) -> list[int]:
if scan_likely:
"""
选出需要做版面/图片框分析的页码列表。
Args:
page_payloads: 每页 text/images 摘要。
scan_likely / text_heavy_pdf / figure_rich_handbook: 文档策略。
Returns:
1-based 页码列表。
"""
if scan_likely or figure_rich_handbook:
return [int(payload["page"]) for payload in page_payloads]
if text_heavy_pdf:
return [
@@ -305,8 +372,21 @@ def _render_page_png(
def run(input_data: dict, context: Context) -> list[Block]:
"""
加载 PDF 为 paragraph / image blocks。
核心逻辑:提取文本与嵌入图;扫描或手册类可渲染整页图;结果写入 context.extras。
Args:
input_data: 须含 file_path。
context: 流水线上下文(会写入 pages/images/extras)。
Returns:
Block 列表。
"""
file_path = Path(input_data["file_path"])
blocks: list[Block] = []
figure_rich_handbook = is_figure_rich_handbook(file_path.name)
try:
from pypdf import PdfReader
@@ -338,6 +418,7 @@ def run(input_data: dict, context: Context) -> list[Block]:
page_text_chars=len(text),
scan_likely=False,
text_heavy_pdf=False,
figure_rich_handbook=figure_rich_handbook,
):
continue
ext = (getattr(img, "image_format", "") or "").lower()
@@ -366,10 +447,14 @@ def run(input_data: dict, context: Context) -> list[Block]:
page_text_chars=len(str(payload.get("text") or "")),
scan_likely=scan_likely,
text_heavy_pdf=text_heavy_pdf,
figure_rich_handbook=figure_rich_handbook,
)
]
candidate_pages = _analysis_candidate_pages(
page_payloads, scan_likely, text_heavy_pdf
page_payloads,
scan_likely,
text_heavy_pdf,
figure_rich_handbook=figure_rich_handbook,
)
image_boxes_by_page: dict[int, list[tuple[float, float, float, float]]] = {}
pdfium_doc = None
@@ -402,6 +487,7 @@ def run(input_data: dict, context: Context) -> list[Block]:
image_count=len(page_images) or int(stats["count"]),
largest_ratio=float(stats["largest_ratio"]),
total_ratio=float(stats["total_ratio"]),
figure_rich_handbook=figure_rich_handbook,
)
if text:
@@ -476,6 +562,11 @@ def run(input_data: dict, context: Context) -> list[Block]:
context.extras["scan_likely"] = True
if text_heavy_pdf:
context.extras["text_heavy_pdf"] = True
if figure_rich_handbook:
context.extras["figure_rich_handbook"] = True
context.warnings.append(
"figure-rich handbook mode: keep embedded figures and prefer OCR captions"
)
if page_image_mode_pages:
context.extras["pdf_page_image_mode_pages"] = page_image_mode_pages
context.warnings.append(