data_processor.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. # 🎯 第一步:解析数据(统一接口)
  26. table_bbox, ocr_data = TableLineGenerator.parse_ocr_data(raw_data, tool)
  27. # 🎯 第二步:分析结构(根据工具选择算法)
  28. if tool.lower() == "mineru":
  29. # ✅ 使用静态方法,无需图片
  30. structure = TableLineGenerator.analyze_structure_only(
  31. ocr_data,
  32. method="mineru"
  33. )
  34. else:
  35. # PPStructure 使用聚类算法
  36. structure = TableLineGenerator.analyze_structure_only(
  37. ocr_data,
  38. y_tolerance=5,
  39. x_tolerance=10,
  40. min_row_height=20,
  41. method="cluster"
  42. )
  43. return table_bbox, structure