|
|
@@ -6,19 +6,15 @@ PDF 结构提取器 - 同步并发 OCR 版本
|
|
|
输出格式兼容后续分类与组装流程。
|
|
|
"""
|
|
|
|
|
|
-import base64
|
|
|
-import io
|
|
|
import re
|
|
|
-from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
-from dataclasses import dataclass
|
|
|
from typing import Dict, Any, List, Optional, Tuple
|
|
|
|
|
|
import fitz
|
|
|
-import numpy as np
|
|
|
-import requests
|
|
|
|
|
|
from foundation.observability.logger.loggering import review_logger as logger
|
|
|
|
|
|
+from .ocr_processor import OcrProcessor, TableRegion, OcrResult
|
|
|
+
|
|
|
# 尝试导入 RapidLayout
|
|
|
try:
|
|
|
from rapid_layout import RapidLayout
|
|
|
@@ -28,25 +24,6 @@ except ImportError:
|
|
|
RapidLayout = None
|
|
|
|
|
|
|
|
|
-@dataclass
|
|
|
-class TableRegion:
|
|
|
- """表格区域信息"""
|
|
|
- page_num: int
|
|
|
- page: fitz.Page
|
|
|
- bbox: Tuple[float, float, float, float]
|
|
|
- score: float
|
|
|
-
|
|
|
-
|
|
|
-@dataclass
|
|
|
-class OcrResult:
|
|
|
- """OCR 结果"""
|
|
|
- page_num: int
|
|
|
- bbox: Tuple[float, float, float, float]
|
|
|
- score: float
|
|
|
- text: str
|
|
|
- success: bool
|
|
|
-
|
|
|
-
|
|
|
class PdfStructureExtractor:
|
|
|
"""PDF 章节结构提取器(支持 OCR 异步并发)"""
|
|
|
|
|
|
@@ -54,13 +31,6 @@ class PdfStructureExtractor:
|
|
|
SECTION_PATTERN = re.compile(r'^[一二三四五六七八九十百]+、\s*.*')
|
|
|
TOC_PATTERN = re.compile(r"\.{3,}|…{2,}")
|
|
|
|
|
|
- # OCR 配置
|
|
|
- MAX_SHORT_EDGE = 1024
|
|
|
- JPEG_QUALITY = 90
|
|
|
- OCR_DPI = 200
|
|
|
- OCR_CONFIDENCE_THRESHOLD = 0.5
|
|
|
- OCR_CONCURRENT_WORKERS = 5
|
|
|
-
|
|
|
def __init__(
|
|
|
self,
|
|
|
clip_top: float = 60,
|
|
|
@@ -76,11 +46,12 @@ class PdfStructureExtractor:
|
|
|
self.clip_bottom = clip_bottom
|
|
|
self.use_ocr = use_ocr and RAPID_LAYOUT_AVAILABLE
|
|
|
|
|
|
- # OCR 配置
|
|
|
- self.ocr_api_url = ocr_api_url
|
|
|
- self.ocr_timeout = ocr_timeout
|
|
|
- self.ocr_api_key = ocr_api_key
|
|
|
- self._layout_engine: Optional[Any] = None
|
|
|
+ # 初始化 OCR 处理器
|
|
|
+ self._ocr_processor = OcrProcessor(
|
|
|
+ ocr_api_url=ocr_api_url,
|
|
|
+ ocr_timeout=ocr_timeout,
|
|
|
+ ocr_api_key=ocr_api_key,
|
|
|
+ ) if self.use_ocr else None
|
|
|
|
|
|
# 目录检测配置
|
|
|
self.detect_toc = detect_toc
|
|
|
@@ -90,12 +61,6 @@ class PdfStructureExtractor:
|
|
|
if use_ocr and not RAPID_LAYOUT_AVAILABLE:
|
|
|
logger.warning("RapidLayout 未安装,OCR 功能不可用")
|
|
|
|
|
|
- def _get_layout_engine(self) -> Optional[Any]:
|
|
|
- """延迟初始化 RapidLayout"""
|
|
|
- if self._layout_engine is None and RAPID_LAYOUT_AVAILABLE:
|
|
|
- self._layout_engine = RapidLayout()
|
|
|
- return self._layout_engine
|
|
|
-
|
|
|
def extract(self, file_content: bytes, progress_callback=None) -> Dict[str, Any]:
|
|
|
"""
|
|
|
从 PDF 字节流提取章节结构。
|
|
|
@@ -152,11 +117,17 @@ class PdfStructureExtractor:
|
|
|
from .toc_detector import TOCCatalogExtractor
|
|
|
|
|
|
if self._toc_extractor is None:
|
|
|
+ # 使用 OCR 处理器的配置(如果已初始化)
|
|
|
+ ocr_config = {}
|
|
|
+ if self._ocr_processor:
|
|
|
+ ocr_config = {
|
|
|
+ "ocr_api_url": self._ocr_processor.ocr_api_url,
|
|
|
+ "ocr_api_key": self._ocr_processor.ocr_api_key,
|
|
|
+ "ocr_timeout": self._ocr_processor.ocr_timeout,
|
|
|
+ }
|
|
|
self._toc_extractor = TOCCatalogExtractor(
|
|
|
model_path=self.toc_model_path,
|
|
|
- ocr_api_url=self.ocr_api_url,
|
|
|
- ocr_api_key=self.ocr_api_key,
|
|
|
- ocr_timeout=self.ocr_timeout,
|
|
|
+ **ocr_config
|
|
|
)
|
|
|
|
|
|
return self._toc_extractor.detect_and_extract(file_content, progress_callback)
|
|
|
@@ -251,13 +222,13 @@ class PdfStructureExtractor:
|
|
|
table_regions: List[TableRegion] = []
|
|
|
ocr_results: List[OcrResult] = []
|
|
|
|
|
|
- if self.use_ocr:
|
|
|
+ if self.use_ocr and self._ocr_processor:
|
|
|
logger.info("[阶段2] 扫描表格区域...")
|
|
|
for page_num in range(total_pages):
|
|
|
page = doc.load_page(page_num)
|
|
|
rect = page.rect
|
|
|
clip_box = fitz.Rect(0, self.clip_top, rect.width, rect.height - self.clip_bottom)
|
|
|
- regions = self._detect_table_regions(page, page_num + 1, clip_box)
|
|
|
+ regions = self._ocr_processor.detect_table_regions(page, page_num + 1, clip_box)
|
|
|
for bbox, score in regions:
|
|
|
table_regions.append(TableRegion(
|
|
|
page_num=page_num + 1,
|
|
|
@@ -275,7 +246,12 @@ class PdfStructureExtractor:
|
|
|
# 执行OCR
|
|
|
if table_regions:
|
|
|
_emit_progress("版面分析", 35, f"发现 {len(table_regions)} 个表格,开始OCR识别...")
|
|
|
- ocr_results = self._process_ocr_concurrent(table_regions, progress_callback=_emit_progress)
|
|
|
+ ocr_results = self._ocr_processor.process_ocr_concurrent(
|
|
|
+ table_regions,
|
|
|
+ progress_callback=lambda completed, total: _emit_progress(
|
|
|
+ "版面分析", 35 + int(completed / total * 15), f"OCR识别中 {completed}/{total}"
|
|
|
+ )
|
|
|
+ )
|
|
|
success_count = sum(1 for r in ocr_results if r.success)
|
|
|
logger.info(f"[阶段2] OCR完成 {success_count}/{len(table_regions)}")
|
|
|
_emit_progress("版面分析", 50, f"OCR识别完成 {success_count}/{len(table_regions)}")
|
|
|
@@ -316,295 +292,6 @@ class PdfStructureExtractor:
|
|
|
logger.info(f"[PdfExtractor] 提取完成,共 {len(result['chapters'])} 个章节")
|
|
|
return result
|
|
|
|
|
|
- def _process_ocr_concurrent(self, regions: List[TableRegion], progress_callback=None) -> List[OcrResult]:
|
|
|
- """同步并发处理 OCR(使用 ThreadPoolExecutor)"""
|
|
|
- results: List[OcrResult] = []
|
|
|
- total = len(regions)
|
|
|
- completed = 0
|
|
|
-
|
|
|
- with ThreadPoolExecutor(max_workers=self.OCR_CONCURRENT_WORKERS) as executor:
|
|
|
- # 提交所有任务
|
|
|
- future_to_region = {
|
|
|
- executor.submit(self._ocr_table_region, r.page, r.bbox): r
|
|
|
- for r in regions
|
|
|
- }
|
|
|
-
|
|
|
- # 处理完成的结果
|
|
|
- for future in as_completed(future_to_region):
|
|
|
- region = future_to_region[future]
|
|
|
- completed += 1
|
|
|
- try:
|
|
|
- text = future.result()
|
|
|
- results.append(OcrResult(
|
|
|
- page_num=region.page_num,
|
|
|
- bbox=region.bbox,
|
|
|
- score=region.score,
|
|
|
- text=text,
|
|
|
- success=True,
|
|
|
- ))
|
|
|
- except Exception as e:
|
|
|
- logger.error(f" 第 {region.page_num} 页表格 OCR 失败: {e}")
|
|
|
- results.append(OcrResult(
|
|
|
- page_num=region.page_num,
|
|
|
- bbox=region.bbox,
|
|
|
- score=region.score,
|
|
|
- text="",
|
|
|
- success=False,
|
|
|
- ))
|
|
|
-
|
|
|
- # 每完成5个或最后一个时推送进度
|
|
|
- if progress_callback and (completed % 5 == 0 or completed == total):
|
|
|
- progress = 35 + int(completed / total * 15) # OCR执行占15%进度(35-50)
|
|
|
- progress_callback("版面分析", progress, f"OCR识别中 {completed}/{total}")
|
|
|
-
|
|
|
- return results
|
|
|
-
|
|
|
- def _detect_table_regions(
|
|
|
- self,
|
|
|
- page: fitz.Page,
|
|
|
- page_num: int,
|
|
|
- clip_box: fitz.Rect
|
|
|
- ) -> List[Tuple[Tuple[float, float, float, float], float]]:
|
|
|
- """检测页面中的表格区域,返回坐标列表"""
|
|
|
- table_regions: List[Tuple[Tuple[float, float, float, float], float]] = []
|
|
|
-
|
|
|
- if not RAPID_LAYOUT_AVAILABLE:
|
|
|
- return table_regions
|
|
|
-
|
|
|
- layout_engine = self._get_layout_engine()
|
|
|
- if layout_engine is None:
|
|
|
- return table_regions
|
|
|
-
|
|
|
- # 渲染页面(裁剪区域)
|
|
|
- pix = page.get_pixmap(dpi=self.OCR_DPI, clip=clip_box)
|
|
|
- img = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.height, pix.width, 3)
|
|
|
-
|
|
|
- try:
|
|
|
- layout_output = layout_engine(img)
|
|
|
-
|
|
|
- # 解析版面结果
|
|
|
- if hasattr(layout_output, 'boxes') and hasattr(layout_output, 'class_names'):
|
|
|
- # 获取缩放比例
|
|
|
- scale_x = clip_box.width / img.shape[1]
|
|
|
- scale_y = clip_box.height / img.shape[0]
|
|
|
-
|
|
|
- for box, label, score in zip(layout_output.boxes, layout_output.class_names, layout_output.scores):
|
|
|
- if label == "table" and score > self.OCR_CONFIDENCE_THRESHOLD:
|
|
|
- # 转换为 PDF 坐标
|
|
|
- pdf_x1 = clip_box.x0 + box[0] * scale_x
|
|
|
- pdf_y1 = clip_box.y0 + box[1] * scale_y
|
|
|
- pdf_x2 = clip_box.x0 + box[2] * scale_x
|
|
|
- pdf_y2 = clip_box.y0 + box[3] * scale_y
|
|
|
-
|
|
|
- table_regions.append(((pdf_x1, pdf_y1, pdf_x2, pdf_y2), score))
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- logger.warning(f" 第 {page_num} 页: 版面分析失败 ({e})")
|
|
|
-
|
|
|
- return table_regions
|
|
|
-
|
|
|
- def _ocr_table_region(self, page: fitz.Page, bbox: Tuple[float, float, float, float], max_retries: int = 3) -> str:
|
|
|
- """对指定区域进行 OCR 识别(使用 GLM-OCR),支持指数退避重试"""
|
|
|
- import time
|
|
|
-
|
|
|
- # 渲染指定区域
|
|
|
- rect = fitz.Rect(bbox)
|
|
|
- pix = page.get_pixmap(dpi=self.OCR_DPI, clip=rect)
|
|
|
- img_bytes = pix.tobytes("jpeg")
|
|
|
-
|
|
|
- # 压缩图片
|
|
|
- compressed = self._compress_image(img_bytes)
|
|
|
- img_base64 = base64.b64encode(compressed).decode('utf-8')
|
|
|
-
|
|
|
- # 请求 OCR
|
|
|
- payload = {
|
|
|
- "model": "GLM-OCR",
|
|
|
- "messages": [
|
|
|
- {
|
|
|
- "role": "user",
|
|
|
- "content": [
|
|
|
- {
|
|
|
- "type": "text",
|
|
|
- "text": "识别图片中的表格内容,按原文排版输出。"
|
|
|
- "注意:"
|
|
|
- "1. 表格用 Markdown 表格格式"
|
|
|
- "2. 保持换行和列对齐"
|
|
|
- "3. 只输出表格内容,不要其他说明"
|
|
|
- },
|
|
|
- {
|
|
|
- "type": "image_url",
|
|
|
- "image_url": {"url": f"data:image/jpeg;base64,{img_base64}"}
|
|
|
- }
|
|
|
- ]
|
|
|
- }
|
|
|
- ],
|
|
|
- "max_tokens": 2048,
|
|
|
- "temperature": 0.1
|
|
|
- }
|
|
|
-
|
|
|
- headers = {"Content-Type": "application/json"}
|
|
|
- if self.ocr_api_key:
|
|
|
- headers["Authorization"] = f"Bearer {self.ocr_api_key}"
|
|
|
-
|
|
|
- # 指数退避重试
|
|
|
- last_error = None
|
|
|
- for attempt in range(max_retries):
|
|
|
- try:
|
|
|
- response = requests.post(
|
|
|
- self.ocr_api_url,
|
|
|
- headers=headers,
|
|
|
- json=payload,
|
|
|
- timeout=self.ocr_timeout
|
|
|
- )
|
|
|
- response.raise_for_status()
|
|
|
-
|
|
|
- result = response.json()
|
|
|
- return self._extract_ocr_content(result)
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- last_error = e
|
|
|
- if attempt < max_retries - 1:
|
|
|
- # 指数退避: 2, 4, 8 秒
|
|
|
- wait_time = 2 ** (attempt + 1)
|
|
|
- logger.warning(f" 第 {page.number + 1} 页表格 OCR 第 {attempt + 1} 次失败: {e}, {wait_time}秒后重试...")
|
|
|
- time.sleep(wait_time)
|
|
|
- else:
|
|
|
- logger.error(f" 第 {page.number + 1} 页表格 OCR 最终失败(已重试{max_retries}次): {e}")
|
|
|
-
|
|
|
- # 所有重试都失败,抛出最后一个错误
|
|
|
- raise last_error
|
|
|
-
|
|
|
- def _replace_table_regions(
|
|
|
- self,
|
|
|
- page: fitz.Page,
|
|
|
- original_text: str,
|
|
|
- ocr_results: List[Dict],
|
|
|
- clip_box: fitz.Rect
|
|
|
- ) -> str:
|
|
|
- """用 OCR 结果替换原始文本中的表格区域"""
|
|
|
- if not ocr_results:
|
|
|
- return original_text
|
|
|
-
|
|
|
- # 获取页面上的文本块及其坐标
|
|
|
- text_blocks = []
|
|
|
- for block in page.get_text("blocks"):
|
|
|
- x0, y0, x1, y1, text, _, _ = block
|
|
|
- # 只考虑裁剪区域内的文本
|
|
|
- if y0 >= clip_box.y0 and y1 <= clip_box.y1:
|
|
|
- text_blocks.append({
|
|
|
- "bbox": (x0, y0, x1, y1),
|
|
|
- "text": text.strip(),
|
|
|
- })
|
|
|
-
|
|
|
- # 按 Y 坐标排序
|
|
|
- text_blocks.sort(key=lambda b: (b["bbox"][1], b["bbox"][0]))
|
|
|
-
|
|
|
- # 找出属于表格区域的文本块
|
|
|
- replaced_indices: Set[int] = set()
|
|
|
- for ocr_result in ocr_results:
|
|
|
- bbox = ocr_result["bbox"]
|
|
|
- rx0, ry0, rx1, ry1 = bbox
|
|
|
-
|
|
|
- for idx, block in enumerate(text_blocks):
|
|
|
- if idx in replaced_indices:
|
|
|
- continue
|
|
|
- bx0, by0, bx1, by1 = block["bbox"]
|
|
|
-
|
|
|
- # 检查重叠
|
|
|
- overlap_x = max(0, min(bx1, rx1) - max(bx0, rx0))
|
|
|
- overlap_y = max(0, min(by1, ry1) - max(by0, ry0))
|
|
|
- overlap_area = overlap_x * overlap_y
|
|
|
- block_area = (bx1 - bx0) * (by1 - by0)
|
|
|
-
|
|
|
- if block_area > 0 and overlap_area / block_area > 0.5:
|
|
|
- replaced_indices.add(idx)
|
|
|
-
|
|
|
- # 构建新文本
|
|
|
- result_parts: List[str] = []
|
|
|
- last_idx = 0
|
|
|
-
|
|
|
- for ocr_result in sorted(ocr_results, key=lambda r: r["bbox"][1]):
|
|
|
- bbox = ocr_result["bbox"]
|
|
|
- rx0, ry0, rx1, ry1 = bbox
|
|
|
-
|
|
|
- # 找到该表格区域之前的文本
|
|
|
- region_start_idx = None
|
|
|
- for idx, block in enumerate(text_blocks):
|
|
|
- if idx in replaced_indices:
|
|
|
- bx0, by0, bx1, by1 = block["bbox"]
|
|
|
- if (bx0 >= rx0 - 5 and bx1 <= rx1 + 5 and
|
|
|
- by0 >= ry0 - 5 and by1 <= ry1 + 5):
|
|
|
- if region_start_idx is None:
|
|
|
- region_start_idx = idx
|
|
|
- last_idx = idx + 1
|
|
|
-
|
|
|
- if region_start_idx is not None:
|
|
|
- # 添加表格前的非表格文本
|
|
|
- for idx in range(last_idx - (last_idx - region_start_idx), region_start_idx):
|
|
|
- if idx not in replaced_indices and idx < len(text_blocks):
|
|
|
- result_parts.append(text_blocks[idx]["text"])
|
|
|
- result_parts.append("\n")
|
|
|
-
|
|
|
- # 添加 OCR 结果
|
|
|
- result_parts.append(ocr_result["ocr_text"])
|
|
|
- result_parts.append("\n")
|
|
|
-
|
|
|
- # 添加剩余文本
|
|
|
- for idx in range(last_idx, len(text_blocks)):
|
|
|
- if idx not in replaced_indices:
|
|
|
- result_parts.append(text_blocks[idx]["text"])
|
|
|
- result_parts.append("\n")
|
|
|
-
|
|
|
- return "".join(result_parts).strip() or original_text
|
|
|
-
|
|
|
- def _compress_image(self, img_bytes: bytes) -> bytes:
|
|
|
- """压缩图片"""
|
|
|
- try:
|
|
|
- from PIL import Image
|
|
|
- img = Image.open(io.BytesIO(img_bytes))
|
|
|
-
|
|
|
- if img.mode in ('RGBA', 'LA', 'P'):
|
|
|
- background = Image.new('RGB', img.size, (255, 255, 255))
|
|
|
- if img.mode == 'P':
|
|
|
- img = img.convert('RGBA')
|
|
|
- if img.mode in ('RGBA', 'LA'):
|
|
|
- background.paste(img, mask=img.split()[-1])
|
|
|
- img = background
|
|
|
- elif img.mode != 'RGB':
|
|
|
- img = img.convert('RGB')
|
|
|
-
|
|
|
- min_edge = min(img.size)
|
|
|
- if min_edge > self.MAX_SHORT_EDGE:
|
|
|
- ratio = self.MAX_SHORT_EDGE / min_edge
|
|
|
- new_size = (int(img.width * ratio), int(img.height * ratio))
|
|
|
- img = img.resize(new_size, Image.Resampling.LANCZOS)
|
|
|
-
|
|
|
- buffer = io.BytesIO()
|
|
|
- img.save(buffer, format='JPEG', quality=self.JPEG_QUALITY, optimize=True)
|
|
|
- return buffer.getvalue()
|
|
|
-
|
|
|
- except Exception as e:
|
|
|
- logger.warning(f"图片压缩失败,使用原图: {e}")
|
|
|
- return img_bytes
|
|
|
-
|
|
|
- def _extract_ocr_content(self, result: Dict) -> str:
|
|
|
- """从 OCR 响应提取内容,并将 HTML 表格转换为 Markdown"""
|
|
|
- content = ""
|
|
|
- if "choices" in result and isinstance(result["choices"], list):
|
|
|
- if len(result["choices"]) > 0:
|
|
|
- message = result["choices"][0].get("message", {})
|
|
|
- content = message.get("content", "")
|
|
|
-
|
|
|
- # 如果内容包含 HTML 标签,转换为 Markdown
|
|
|
- if content and "<" in content and ">" in content:
|
|
|
- try:
|
|
|
- from ..doc_worker.pdf_worker.html_to_markdown import convert_html_to_markdown
|
|
|
- content = convert_html_to_markdown(content)
|
|
|
- except Exception as e:
|
|
|
- logger.debug(f"HTML 转 Markdown 失败,保留原始内容: {e}")
|
|
|
-
|
|
|
- return content
|
|
|
-
|
|
|
def _extract_text_blocks_with_position(
|
|
|
self,
|
|
|
page: fitz.Page,
|