analysis_controls.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. 分析功能控件
  3. """
  4. import streamlit as st
  5. from typing import Dict, Optional
  6. import json
  7. def create_analysis_section(generator, tool: str = "ppstructv3") -> Optional[Dict]:
  8. """
  9. 创建分析控件
  10. Args:
  11. generator: TableLineGenerator 实例
  12. tool: 工具类型
  13. Returns:
  14. 分析后的表格结构(如果点击了分析按钮)
  15. """
  16. st.sidebar.subheader("🔍 表格结构分析")
  17. # 检查是否需要自动触发分析(例如旋转微调后)
  18. auto_analyze = st.session_state.get('need_reanalysis', False)
  19. if auto_analyze:
  20. st.session_state.need_reanalysis = False
  21. # 🔑 根据工具类型显示不同的参数
  22. if tool.lower() == "mineru":
  23. st.sidebar.info("📋 MinerU 格式:直接使用 table_cells 生成结构")
  24. if st.sidebar.button("🚀 生成表格结构", type="primary") or auto_analyze:
  25. with st.spinner("正在分析表格结构..."):
  26. try:
  27. # 🔑 使用 generator 分析 (支持旋转后的坐标)
  28. structure = generator.analyze_table_structure(method="mineru")
  29. # 保存到 session_state
  30. st.session_state.structure = structure
  31. if 'table_bbox' in structure:
  32. st.session_state.table_bbox = structure['table_bbox']
  33. st.session_state.undo_stack = []
  34. st.session_state.redo_stack = []
  35. # 清除缓存的图片
  36. from .drawing import clear_table_image_cache
  37. clear_table_image_cache()
  38. st.success(
  39. f"✅ 表格结构生成成功!\n\n"
  40. f"检测到 {len(structure.get('rows', []))} 行,{len(structure.get('columns', []))} 列"
  41. )
  42. return structure
  43. except Exception as e:
  44. st.error(f"❌ 分析失败: {e}")
  45. import traceback
  46. with st.expander("🔍 详细错误"):
  47. st.code(traceback.format_exc())
  48. else:
  49. # 🔑 PPStructure V3 格式:使用参数调整
  50. y_tolerance = st.sidebar.slider(
  51. "Y轴聚类容差(行检测)",
  52. min_value=1,
  53. max_value=20,
  54. value=5,
  55. help="相邻文本框Y坐标差小于此值时合并为同一行"
  56. )
  57. x_tolerance = st.sidebar.slider(
  58. "X轴聚类容差(列检测)",
  59. min_value=5,
  60. max_value=30,
  61. value=10,
  62. help="相邻文本框X坐标差小于此值时合并为同一列"
  63. )
  64. min_row_height = st.sidebar.slider(
  65. "最小行高",
  66. min_value=10,
  67. max_value=50,
  68. value=20,
  69. help="行高小于此值的将被过滤"
  70. )
  71. if st.sidebar.button("🚀 分析表格结构", type="primary") or auto_analyze:
  72. with st.spinner("正在分析表格结构..."):
  73. try:
  74. structure = generator.analyze_table_structure(
  75. y_tolerance=y_tolerance,
  76. x_tolerance=x_tolerance,
  77. min_row_height=min_row_height
  78. )
  79. st.session_state.structure = structure
  80. st.session_state.undo_stack = []
  81. st.session_state.redo_stack = []
  82. # 清除缓存的图片
  83. from .drawing import clear_table_image_cache
  84. clear_table_image_cache()
  85. st.success(
  86. f"✅ 分析完成!\n\n"
  87. f"检测到 {len(structure['rows'])} 行,{len(structure['columns'])} 列"
  88. )
  89. return structure
  90. except Exception as e:
  91. st.error(f"❌ 分析失败: {e}")
  92. import traceback
  93. with st.expander("🔍 详细错误"):
  94. st.code(traceback.format_exc())
  95. return None