Jelajahi Sumber

优化图像方向检测功能,调整控件状态管理,简化用户交互体验

zhch158_admin 2 bulan lalu
induk
melakukan
21c772f29e
1 mengubah file dengan 43 tambahan dan 18 penghapusan
  1. 43 18
      ocr_validator_layout.py

+ 43 - 18
ocr_validator_layout.py

@@ -103,8 +103,8 @@ class OCRLayoutManager:
                 # 获取OCR工具的旋转配置
                 rotation_config = get_ocr_tool_rotation_config(self.validator.ocr_data, self.config)
                 
-                st.info(f"🔄 检测到文档旋转角度: {rotation_angle}°,正在处理图像和坐标...")
-                st.info(f"📋 OCR工具配置: 坐标{'已预旋转' if rotation_config['coordinates_are_pre_rotated'] else '需要旋转'}")
+                # st.info(f"🔄 检测到文档旋转角度: {rotation_angle}°,正在处理图像和坐标...")
+                # st.info(f"📋 OCR工具配置: 坐标{'已预旋转' if rotation_config['coordinates_are_pre_rotated'] else '需要旋转'}")
                 
                 # 判断是否需要旋转坐标
                 if rotation_config['coordinates_are_pre_rotated']:
@@ -381,11 +381,33 @@ class OCRLayoutManager:
         # 图片控制选项
         col1, col2, col3, col4 = st.columns(4)
         with col1:
-            current_zoom = st.slider("图片缩放", 0.3, 2.0, zoom_level, 0.1, key=f"{layout_type}_zoom_level")
+            # 判断{layout_type}_zoom_level是否有值,如果有值直接使用,否则使用传入的zoom_level
+            current_zoom = self.validator.zoom_level
+            current_zoom = st.slider("图片缩放", 0.3, 2.0, current_zoom, 0.1, key=f"{layout_type}_zoom_level")
+            if current_zoom != self.validator.zoom_level:
+                self.validator.zoom_level = current_zoom
         with col2:
-            show_all_boxes = st.checkbox("显示所有框", value=False, key=f"{layout_type}_show_all_boxes")
+            # 判断{layout_type}_show_all_boxes是否有值,如果有值直接使用,否则默认False
+            # if f"{layout_type}_show_all_boxes" not in st.session_state:
+            #     st.session_state[f"{layout_type}_show_all_boxes"] = False
+
+            show_all_boxes = st.checkbox(
+                "显示所有框",
+                # value=st.session_state[f"{layout_type}_show_all_boxes"],
+                value = self.validator.show_all_boxes,
+                key=f"{layout_type}_show_all_boxes"
+            )
+            if show_all_boxes != self.validator.show_all_boxes:
+                self.validator.show_all_boxes = show_all_boxes
         with col3:
-            fit_to_container = st.checkbox("适应容器", value=True, key=f"{layout_type}_fit_container")
+            # 判断{layout_type}_fit_to_container是否有值,如果有值直接使用,否则默认True
+            fit_to_container = st.checkbox(
+                "适应容器", 
+                value=self.validator.fit_to_container,
+                key=f"{layout_type}_fit_to_container"
+            )
+            if fit_to_container != self.validator.fit_to_container:
+                self.validator.fit_to_container = fit_to_container
         with col4:
             # 显示当前角度状态
             current_angle = self.get_rotation_angle()
@@ -393,29 +415,32 @@ class OCRLayoutManager:
 
          # 方向检测控制面板
         with st.expander("🔄 图片方向检测", expanded=False):
-            col1, col2, col3 = st.columns(3)
+            col1, col2, col3 = st.columns([1, 1, 1], width='stretch')
             
             with col1:
-                if st.button("🔍 自动检测方向", key=f"{layout_type}_detect_orientation"):
-                    if self.validator.image_path:
-                        with st.spinner("正在检测图片方向..."):
-                            detection_result = self.detect_and_suggest_rotation(self.validator.image_path)
-                            st.session_state[f'{layout_type}_detection_result'] = detection_result
-                        st.rerun()
-            
-            with col2:
                 manual_angle = st.selectbox(
-                    "手动设置角度",
+                    "设置角度",
                     [0, 90, 180, 270],
-                    key=f"{layout_type}_manual_angle"
+                    index = 0,
+                    label_visibility="collapsed",
+                    # key=f"{layout_type}_manual_angle"
                 )
-                if st.button("应用手动角度", key=f"{layout_type}_apply_manual"):
+                # if st.button("应用手动角度", key=f"{layout_type}_apply_manual"):
+                if not hasattr(self, '_auto_detected_angle') or self._auto_detected_angle != manual_angle:
                     self._auto_detected_angle = float(manual_angle)
-                    st.success(f"已设置旋转角度为 {manual_angle}°")
+                    # st.success(f"已设置旋转角度为 {manual_angle}")
                     # 需要清除图片缓存,以及text_bbox_mapping中的bbox
                     self.clear_image_cache()
                     self.validator.process_data()
                     st.rerun()
+                    
+            with col2:
+                if st.button("🔍 自动检测方向", key=f"{layout_type}_detect_orientation"):
+                    if self.validator.image_path:
+                        with st.spinner("正在检测图片方向..."):
+                            detection_result = self.detect_and_suggest_rotation(self.validator.image_path)
+                            st.session_state[f'{layout_type}_detection_result'] = detection_result
+                        st.rerun()
             
             with col3:
                 if st.button("🔄 重置角度", key=f"{layout_type}_reset_angle"):