mode_setup.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. """
  2. 模式设置(新建/编辑)
  3. """
  4. import streamlit as st
  5. from PIL import Image
  6. from typing import Dict, Tuple
  7. try:
  8. from ..table_line_generator import TableLineGenerator
  9. except ImportError:
  10. from table_line_generator import TableLineGenerator
  11. from .display_controls import create_display_settings_section, create_undo_redo_section
  12. from .analysis_controls import create_analysis_section
  13. def setup_new_annotation_mode(ocr_data, image, config: Dict) -> Tuple:
  14. """
  15. 设置新建标注模式的通用逻辑
  16. Args:
  17. ocr_data: OCR 数据
  18. image: 图片对象
  19. config: 显示配置
  20. Returns:
  21. tuple: (y_tolerance, x_tolerance, min_row_height, line_width,
  22. display_mode, zoom_level, show_line_numbers)
  23. """
  24. # 参数调整
  25. st.sidebar.header("🔧 参数调整")
  26. y_tolerance = st.sidebar.slider(
  27. "Y轴聚类容差(像素)",
  28. 1, 20, 5,
  29. key="new_y_tol"
  30. )
  31. x_tolerance = st.sidebar.slider(
  32. "X轴聚类容差(像素)",
  33. 5, 50, 10,
  34. key="new_x_tol"
  35. )
  36. min_row_height = st.sidebar.slider(
  37. "最小行高(像素)",
  38. 10, 100, 20,
  39. key="new_min_h"
  40. )
  41. # 显示设置
  42. line_width, display_mode, zoom_level, show_line_numbers = \
  43. create_display_settings_section(config)
  44. create_undo_redo_section()
  45. # 初始化生成器
  46. if 'generator' not in st.session_state or st.session_state.generator is None:
  47. try:
  48. generator = TableLineGenerator(image, ocr_data)
  49. st.session_state.generator = generator
  50. except Exception as e:
  51. st.error(f"❌ 初始化生成器失败: {e}")
  52. st.stop()
  53. # 分析按钮
  54. create_analysis_section(y_tolerance, x_tolerance, min_row_height)
  55. return (y_tolerance, x_tolerance, min_row_height,
  56. line_width, display_mode, zoom_level, show_line_numbers)
  57. def setup_edit_annotation_mode(structure: Dict, image, config: Dict) -> Tuple:
  58. """
  59. 设置编辑标注模式的通用逻辑
  60. Args:
  61. structure: 表格结构
  62. image: 图片对象(可为 None)
  63. config: 显示配置
  64. Returns:
  65. tuple: (image, line_width, display_mode, zoom_level, show_line_numbers)
  66. """
  67. # 如果没有图片,创建虚拟画布
  68. if image is None:
  69. if 'table_bbox' in structure:
  70. bbox = structure['table_bbox']
  71. dummy_width = bbox[2] + 100
  72. dummy_height = bbox[3] + 100
  73. else:
  74. dummy_width = 2000
  75. dummy_height = 2000
  76. image = Image.new('RGB', (dummy_width, dummy_height), color='white')
  77. st.info(f"💡 使用虚拟画布 ({dummy_width}x{dummy_height})")
  78. # 显示设置
  79. line_width, display_mode, zoom_level, show_line_numbers = \
  80. create_display_settings_section(config)
  81. create_undo_redo_section()
  82. return image, line_width, display_mode, zoom_level, show_line_numbers