data_processor.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """
  2. OCR 数据处理
  3. """
  4. import sys
  5. from pathlib import Path
  6. from typing import List, Dict, Tuple
  7. # 添加父目录到路径
  8. sys.path.insert(0, str(Path(__file__).parent.parent))
  9. try:
  10. from table_line_generator import TableLineGenerator
  11. except ImportError:
  12. from ..table_line_generator import TableLineGenerator
  13. def get_structure_from_ocr(
  14. raw_data: Dict,
  15. tool: str = "ppstructv3"
  16. ) -> Tuple[List[int], Dict]:
  17. """
  18. 从 OCR 数据生成表格结构(统一处理流程)
  19. Args:
  20. raw_data: 原始 OCR 结果
  21. tool: 工具类型 ("ppstructv3" / "mineru")
  22. Returns:
  23. (table_bbox, structure): 表格边界框和结构信息
  24. """
  25. from PIL import Image
  26. # 🎯 第一步:解析数据(统一接口)
  27. table_bbox, ocr_data = TableLineGenerator.parse_ocr_data(raw_data, tool)
  28. # 🎯 第二步:创建生成器
  29. dummy_image = Image.new('RGB', (2000, 3000), 'white')
  30. generator = TableLineGenerator(dummy_image, ocr_data)
  31. # 🎯 第三步:分析结构(根据工具选择算法)
  32. if tool.lower() == "mineru":
  33. # MinerU 使用基于索引的算法
  34. structure = generator.analyze_table_structure(method="mineru")
  35. else:
  36. # PPStructure 使用聚类算法
  37. structure = generator.analyze_table_structure(
  38. y_tolerance=5,
  39. x_tolerance=10,
  40. min_row_height=20,
  41. method="cluster"
  42. )
  43. return table_bbox, structure