diff --git a/src/autonomus_drone_hand_gesture_project/README.md b/src/autonomus_drone_hand_gesture_project/README.md index 45f02648d4..7ce101e1e5 100644 --- a/src/autonomus_drone_hand_gesture_project/README.md +++ b/src/autonomus_drone_hand_gesture_project/README.md @@ -2,12 +2,12 @@ ## 项目简介 -基于计算机视觉和语音反馈技术的手势控制无人机系统。使用纯OpenCV实现手部检测和手势识别,结合语音反馈系统,提供直观的无人机控制体验。系统支持AirSim仿真环境和真实无人机控制,具备手势平滑处理、状态追踪、性能监控和轨迹记录等完整功能。 +基于计算机视觉和语音反馈技术的手势控制无人机系统。使用OpenCV实现手部检测和手势识别,结合语音反馈系统,提供直观的无人机控制体验。系统支持AirSim仿真环境和真实无人机控制,具备手势平滑处理、状态追踪、性能监控和轨迹记录等完整功能。 ## 核心功能 ### 1. 增强手势识别与控制 -- **纯OpenCV实现**:不依赖深度学习框架,使用肤色检测、轮廓分析和凸缺陷检测识别手势 +- **OpenCV实现**:不依赖深度学习框架,使用肤色检测、轮廓分析和凸缺陷检测识别手势 - **多种手势支持**:支持停止、前进、上升、下降、左转、右转、悬停等多种控制手势 - **智能手势平滑**:使用手势稳定性检测和位置平滑算法,减少误判和抖动 - **实时状态追踪**:跟踪手势生命周期(开始/活跃/结束),提供精准控制反馈 diff --git a/src/autonomus_drone_hand_gesture_project/config_manager.py b/src/autonomus_drone_hand_gesture_project/config_manager.py index 7c6baff3bb..0de8c7a415 100644 --- a/src/autonomus_drone_hand_gesture_project/config_manager.py +++ b/src/autonomus_drone_hand_gesture_project/config_manager.py @@ -86,6 +86,9 @@ def __init__(self): 'transition_threshold': 0.3, 'position_stability_weight': 0.4, 'gesture_cooldown': 0.5, + # 新增手势稳定性分析配置 + 'stability_window_size': 15, + 'enable_gesture_stability_analysis': True, }, 'drone': { 'velocity': 2.5, @@ -111,7 +114,11 @@ def __init__(self): 'show_performance_mode': True, 'show_performance_stats': True, 'show_system_resources': True, - 'show_advanced_gestures': True, # 新增:显示高级手势信息 + 'show_advanced_gestures': True, + # 新增性能可视化配置 + 'show_performance_visualization': True, + 'performance_visualization_mode': 'charts', # 'none', 'charts', 'gauges' + 'show_gesture_stability_info': True, }, 'performance': { 'target_fps': 30, @@ -122,11 +129,20 @@ def __init__(self): 'modes': ['fast', 'balanced', 'accurate'], 'auto_report_interval': 60, 'enable_performance_monitor': True, + # 新增性能监控配置 + 'enable_gpu_monitoring': True, + 'enable_performance_prediction': True, + 'enable_anomaly_detection': True, + 'performance_visualization': True, + 'detailed_performance_charts': False, }, 'calibration': { 'auto_calibrate_skin': True, 'skin_calibration_frames': 30, - 'hand_size_calibration': True + 'hand_size_calibration': True, + # 新增自动校准配置 + 'auto_adjust_parameters': True, + 'calibration_interval': 300, # 每5分钟校准一次 }, 'speech': { 'enabled': True, @@ -144,6 +160,10 @@ def __init__(self): 'min_gesture_confidence': 0.7, 'gesture_start_threshold': 3, 'gesture_end_threshold': 10, + # 新增语音配置 + 'announce_stability_status': True, + 'announce_visualization_changes': True, + 'performance_warnings_enabled': True, }, 'recording': { 'auto_save_interval': 5, @@ -152,13 +172,46 @@ def __init__(self): 'trajectory_thickness': 2, 'trajectory_max_length': 100, 'default_save_dir': 'trajectories', + # 新增录制配置 + 'auto_playback_after_recording': False, + 'save_compressed_trajectories': True, + 'max_auto_save_files': 10, + }, + 'stability': { + # 新增稳定性分析配置 + 'enable_stability_analysis': True, + 'stability_update_interval': 1.0, + 'min_stability_score': 0.6, + 'max_transition_count': 10, + 'stability_notification_enabled': True, + 'auto_reset_on_unstable': True, + }, + 'visualization': { + # 新增可视化配置 + 'chart_update_rate': 2, # 每2帧更新一次图表 + 'gauge_update_rate': 5, # 每5帧更新一次仪表盘 + 'chart_history_length': 30, + 'show_chart_grid': True, + 'chart_background_opacity': 0.7, + 'performance_colors': { + 'excellent': (0, 255, 0), + 'good': (100, 255, 100), + 'fair': (255, 255, 0), + 'poor': (255, 165, 0), + 'critical': (255, 0, 0) + } } } + self.config = self.load_config() self.skin_calibration_data = [] self.hand_size_calibration_done = False self.reference_hand_size = 0 + # 新增:可视化状态 + self.visualization_enabled = self.get('display', 'show_performance_visualization') + self.visualization_mode = self.get('display', 'performance_visualization_mode') + def load_config(self): """加载配置""" if os.path.exists(self.config_file): @@ -217,6 +270,13 @@ def set(self, *keys, value): config[keys[-1]] = value self.save_config() + # 更新内部状态 + if len(keys) == 2: + if keys[0] == 'display' and keys[1] == 'show_performance_visualization': + self.visualization_enabled = value + elif keys[0] == 'display' and keys[1] == 'performance_visualization_mode': + self.visualization_mode = value + def get_performance_mode_config(self, mode=None): """获取性能模式配置""" if mode is None: @@ -311,4 +371,160 @@ def calibrate_hand_size(self, hand_area): if hand_area > 0: self.reference_hand_size = hand_area self.hand_size_calibration_done = True - print(f"✓ 手部大小校准完成: {self.reference_hand_size:.0f} 像素") \ No newline at end of file + print(f"✓ 手部大小校准完成: {self.reference_hand_size:.0f} 像素") + + def toggle_visualization_mode(self): + """切换可视化模式""" + modes = ['none', 'charts', 'gauges'] + current_mode = self.get('display', 'performance_visualization_mode') + + if current_mode not in modes: + current_mode = 'none' + + current_index = modes.index(current_mode) + next_index = (current_index + 1) % len(modes) + next_mode = modes[next_index] + + self.set('display', 'performance_visualization_mode', value=next_mode) + self.visualization_mode = next_mode + + print(f"✓ 可视化模式切换为: {next_mode}") + return next_mode + + def toggle_visualization(self): + """切换可视化显示""" + current = self.get('display', 'show_performance_visualization') + new_value = not current + self.set('display', 'show_performance_visualization', value=new_value) + self.visualization_enabled = new_value + + print(f"✓ 性能可视化: {'开启' if new_value else '关闭'}") + return new_value + + def get_visualization_status(self): + """获取可视化状态""" + return { + 'enabled': self.visualization_enabled, + 'mode': self.visualization_mode, + 'show_chart_grid': self.get('visualization', 'show_chart_grid'), + 'chart_background_opacity': self.get('visualization', 'chart_background_opacity') + } + + def get_stability_config(self): + """获取稳定性分析配置""" + return { + 'enabled': self.get('stability', 'enable_stability_analysis'), + 'window_size': self.get('gesture', 'stability_window_size'), + 'update_interval': self.get('stability', 'stability_update_interval'), + 'min_score': self.get('stability', 'min_stability_score'), + 'auto_reset': self.get('stability', 'auto_reset_on_unstable') + } + + def get_visualization_config(self): + """获取可视化配置""" + return { + 'chart_update_rate': self.get('visualization', 'chart_update_rate'), + 'gauge_update_rate': self.get('visualization', 'gauge_update_rate'), + 'chart_history_length': self.get('visualization', 'chart_history_length'), + 'show_grid': self.get('visualization', 'show_chart_grid'), + 'background_opacity': self.get('visualization', 'chart_background_opacity'), + 'colors': self.get('visualization', 'performance_colors') + } + + def validate_config(self): + """验证配置有效性""" + issues = [] + + # 检查性能模式 + current_mode = self.get('performance', 'mode') + if current_mode not in self.performance_modes: + issues.append(f"无效的性能模式: {current_mode}") + self.set('performance', 'mode', value='balanced') + + # 检查可视化模式 + viz_mode = self.get('display', 'performance_visualization_mode') + valid_viz_modes = ['none', 'charts', 'gauges'] + if viz_mode not in valid_viz_modes: + issues.append(f"无效的可视化模式: {viz_mode}") + self.set('display', 'performance_visualization_mode', value='charts') + + # 检查手势稳定性窗口大小 + window_size = self.get('gesture', 'stability_window_size') + if not isinstance(window_size, int) or window_size < 5 or window_size > 50: + issues.append(f"无效的手势稳定性窗口大小: {window_size}") + self.set('gesture', 'stability_window_size', value=15) + + if issues: + print("⚠ 配置验证发现问题:") + for issue in issues: + print(f" - {issue}") + print("✓ 已自动修复配置问题") + + return len(issues) == 0 + + def reset_to_defaults(self, section=None): + """重置配置为默认值""" + if section is None: + self.config = self.default_config.copy() + print("✓ 所有配置已重置为默认值") + elif section in self.default_config: + self.config[section] = self.default_config[section].copy() + print(f"✓ {section} 配置已重置为默认值") + else: + print(f"⚠ 无效的配置节: {section}") + return False + + self.save_config() + return True + + def export_config(self, filename=None): + """导出配置到文件""" + import os + current_dir = os.path.dirname(os.path.abspath(__file__)) + if filename is None: + timestamp = time.strftime("%Y%m%d_%H%M%S") + filename = os.path.join(current_dir, f'gesture_config_backup_{timestamp}.json') + + try: + with open(filename, 'w', encoding='utf-8') as f: + json.dump(self.config, f, indent=2, ensure_ascii=False) + print(f"📤 配置已导出到: {filename}") + return True + except Exception as e: + print(f"❌ 导出配置失败: {e}") + return False + + def import_config(self, filename): + """从文件导入配置""" + try: + if not os.path.exists(filename): + print(f"❌ 配置文件不存在: {filename}") + return False + + with open(filename, 'r', encoding='utf-8') as f: + loaded_config = json.load(f) + + # 合并配置 + self._merge_config(self.config, loaded_config) + self.save_config() + + print(f"📥 配置已从文件导入: {filename}") + return True + except Exception as e: + print(f"❌ 导入配置失败: {e}") + return False + +# 用于测试配置管理器 +if __name__ == "__main__": + import time + + config = ConfigManager() + + print("当前配置状态:") + print(f" 性能模式: {config.get('performance', 'mode')}") + print(f" 可视化模式: {config.get('display', 'performance_visualization_mode')}") + print(f" 手势稳定性分析: {config.get('gesture', 'enable_gesture_stability_analysis')}") + print(f" GPU监控: {config.get('performance', 'enable_gpu_monitoring')}") + + # 验证配置 + config.validate_config() \ No newline at end of file diff --git a/src/autonomus_drone_hand_gesture_project/gesture_config.json b/src/autonomus_drone_hand_gesture_project/gesture_config.json index fb3da8e6fd..1e5e3d1fd6 100644 --- a/src/autonomus_drone_hand_gesture_project/gesture_config.json +++ b/src/autonomus_drone_hand_gesture_project/gesture_config.json @@ -18,11 +18,11 @@ }, "gesture": { "skin_lower_h": 0, - "skin_upper_h": 175, + "skin_upper_h": 178, "skin_lower_s": 0, - "skin_upper_s": 87, + "skin_upper_s": 125, "skin_lower_v": 0, - "skin_upper_v": 95, + "skin_upper_v": 96, "min_hand_area": 3000, "max_hand_area": 30000, "hand_ratio_threshold": 1.5, @@ -31,6 +31,8 @@ "transition_threshold": 0.3, "position_stability_weight": 0.4, "gesture_cooldown": 0.5, + "stability_window_size": 15, + "enable_gesture_stability_analysis": true, "history_size": 10, "smooth_frames": 5, "min_confidence": 0.6, @@ -79,7 +81,7 @@ "show_fingertips": true, "show_palm_center": true, "show_hand_direction": true, - "show_debug_info": true, + "show_debug_info": false, "show_speech_status": true, "show_gesture_history": true, "show_stability_indicator": true, @@ -89,6 +91,10 @@ "show_performance_stats": true, "show_system_resources": true, "show_advanced_gestures": true, + "show_performance_visualization": true, + "performance_visualization_mode": "charts", + "show_gesture_stability_info": true, + "show_custom_gestures": true, "show_hotkey_hints": true, "show_operation_hints": true, "font_size_large": 20, @@ -120,6 +126,11 @@ ], "auto_report_interval": 60, "enable_performance_monitor": true, + "enable_gpu_monitoring": true, + "enable_performance_prediction": true, + "enable_anomaly_detection": true, + "performance_visualization": true, + "detailed_performance_charts": false, "enable_gpu_acceleration": false, "enable_hardware_acceleration": true, "enable_multithreading": false, @@ -132,6 +143,7 @@ "auto_calibrate_skin": true, "skin_calibration_frames": 30, "hand_size_calibration": true, + "auto_adjust_parameters": true, "calibration_interval": 300, "calibration_timeout": 10.0, "enable_adaptive_calibration": true, @@ -153,6 +165,9 @@ "min_gesture_confidence": 0.7, "gesture_start_threshold": 3, "gesture_end_threshold": 10, + "announce_stability_status": true, + "announce_visualization_changes": true, + "performance_warnings_enabled": true, "announce_gesture_directions": true, "announce_recording": true, "enable_short_feedback": true, @@ -167,11 +182,75 @@ "trajectory_thickness": 2, "trajectory_max_length": 100, "default_save_dir": "trajectories", + "auto_playback_after_recording": false, + "save_compressed_trajectories": true, + "max_auto_save_files": 10, "auto_save": false, "default_filename": "gesture_recording.pkl", "loop_playback": false, "playback_speed": 1.0 }, + "stability": { + "enable_stability_analysis": true, + "stability_update_interval": 1.0, + "min_stability_score": 0.6, + "max_transition_count": 10, + "stability_notification_enabled": true, + "auto_reset_on_unstable": true + }, + "visualization": { + "chart_update_rate": 2, + "gauge_update_rate": 5, + "chart_history_length": 30, + "show_chart_grid": true, + "chart_background_opacity": 0.7, + "performance_colors": { + "excellent": [ + 0, + 255, + 0 + ], + "good": [ + 100, + 255, + 100 + ], + "fair": [ + 255, + 255, + 0 + ], + "poor": [ + 255, + 165, + 0 + ], + "critical": [ + 255, + 0, + 0 + ] + } + }, + "training": { + "samples_needed": 20, + "min_sample_confidence": 0.6, + "match_threshold": 0.7, + "enable_custom_gestures": true, + "auto_load_templates": true, + "template_dir": "gesture_templates", + "auto_save_training": true, + "enable_training_mode": false, + "samples_per_gesture": 50, + "training_gestures": [ + "Stop", + "Forward", + "Up", + "Down", + "Left", + "Right" + ] + }, "hotkeys": { "quick_save_enabled": true, "quick_load_enabled": true, @@ -222,18 +301,5 @@ "debug_mode": false, "last_updated": "2025-12-19 20:51:44", "version": "1.0" - }, - "training": { - "auto_save_training": true, - "enable_training_mode": false, - "samples_per_gesture": 50, - "training_gestures": [ - "Stop", - "Forward", - "Up", - "Down", - "Left", - "Right" - ] } } \ No newline at end of file diff --git a/src/autonomus_drone_hand_gesture_project/gesture_recognizer.py b/src/autonomus_drone_hand_gesture_project/gesture_recognizer.py index aa8baee682..89c2b7c767 100644 --- a/src/autonomus_drone_hand_gesture_project/gesture_recognizer.py +++ b/src/autonomus_drone_hand_gesture_project/gesture_recognizer.py @@ -2,6 +2,7 @@ 手势识别器模块 负责识别和分析手势 作者: xiaoshiyuan888 +优化版本:改进手势历史记录和平滑算法 """ import time @@ -29,10 +30,13 @@ def __init__(self, speech_manager=None, config=None): self.min_confidence = self.mode_config['min_confidence'] self.resize_factor = self.mode_config['resize_factor'] - # 增强的手势历史和平滑 + # 改进的手势历史和平滑 - 增加时间戳和权重 self.gesture_history = deque(maxlen=self.history_size) self.confidence_history = deque(maxlen=self.history_size) self.position_history = deque(maxlen=self.history_size) + self.timestamp_history = deque(maxlen=self.history_size) # 新增:时间戳历史 + self.weight_history = deque(maxlen=self.history_size) # 新增:权重历史 + self.current_gesture = "Waiting" self.current_confidence = 0.0 @@ -73,13 +77,13 @@ def __init__(self, speech_manager=None, config=None): "Waiting": (200, 200, 200), "Error": (255, 0, 0), "Hover": (255, 255, 255), - "Grab": (255, 0, 255), # 新增:紫色 - "Release": (0, 255, 0), # 新增:绿色 - "RotateCW": (0, 255, 255), # 新增:黄色 - "RotateCCW": (255, 255, 0), # 新增:青色 - "TakePhoto": (255, 165, 0), # 新增:橙色 - "ReturnHome": (0, 128, 128), # 新增:深青色 - "AutoFlight": (128, 0, 128), # 新增:紫色 + "Grab": (255, 0, 255), + "Release": (0, 255, 0), + "RotateCW": (0, 255, 255), + "RotateCCW": (255, 255, 0), + "TakePhoto": (255, 165, 0), + "ReturnHome": (0, 128, 128), + "AutoFlight": (128, 0, 128), } # 手势到语音的映射 @@ -93,13 +97,13 @@ def __init__(self, speech_manager=None, config=None): "Waiting": "gesture_waiting", "Error": "gesture_error", "Hover": "gesture_hover", - "Grab": "gesture_grab", # 新增 - "Release": "gesture_release", # 新增 - "RotateCW": "gesture_rotate_cw", # 新增 - "RotateCCW": "gesture_rotate_ccw", # 新增 - "TakePhoto": "gesture_photo", # 新增 - "ReturnHome": "gesture_return_home", # 新增 - "AutoFlight": "gesture_auto_flight", # 新增 + "Grab": "gesture_grab", + "Release": "gesture_release", + "RotateCW": "gesture_rotate_cw", + "RotateCCW": "gesture_rotate_ccw", + "TakePhoto": "gesture_photo", + "ReturnHome": "gesture_return_home", + "AutoFlight": "gesture_auto_flight", } # 手势状态颜色 @@ -165,6 +169,8 @@ def set_performance_mode(self, mode): self.gesture_history = deque(maxlen=self.history_size) self.confidence_history = deque(maxlen=self.history_size) self.position_history = deque(maxlen=self.history_size) + self.timestamp_history = deque(maxlen=self.history_size) + self.weight_history = deque(maxlen=self.history_size) # 更新显示信息 self.performance_mode_color = self.mode_config['color'] @@ -501,58 +507,116 @@ def recognize_gesture_improved(self, hand_data): return "Waiting", confidence * 0.5 def smooth_gesture_enhanced(self, new_gesture, new_confidence, hand_data): - """增强的手势平滑处理""" + """增强的手势平滑处理 - 使用加权平均""" current_time = time.time() # 检查手势冷却时间 if current_time - self.last_gesture_change_time < self.config.get('gesture', 'gesture_cooldown'): return self.current_gesture, self.current_confidence + # 计算当前手势的权重(基于置信度和时间) + # 权重 = 置信度 * 时间衰减因子 + if len(self.timestamp_history) > 0: + time_since_last = current_time - self.timestamp_history[-1] + time_weight = max(0.1, 1.0 - time_since_last / 5.0) # 时间衰减因子 + else: + time_weight = 1.0 + + weight = new_confidence * time_weight + # 添加到历史 self.gesture_history.append(new_gesture) self.confidence_history.append(new_confidence) + self.timestamp_history.append(current_time) + self.weight_history.append(weight) if hand_data is not None: self.position_history.append(hand_data['position']) - # 计算手势稳定性 - if len(self.gesture_history) >= 3: - recent_gestures = list(self.gesture_history)[-3:] - gesture_counter = Counter(recent_gestures) - most_common_gesture, most_common_count = gesture_counter.most_common(1)[0] - - # 计算位置稳定性 - position_stability = 1.0 - if len(self.position_history) >= 2 and hand_data is not None: - current_pos = hand_data['position'] - prev_pos = self.position_history[-2] if len(self.position_history) >= 2 else current_pos - position_diff = math.sqrt((current_pos[0] - prev_pos[0]) ** 2 + (current_pos[1] - prev_pos[1]) ** 2) - position_stability = max(0, 1.0 - position_diff * 5.0) - - # 增强的稳定性检查 - stability_threshold = self.mode_config['gesture_stability_threshold'] - transition_threshold = self.config.get('gesture', 'transition_threshold') - position_weight = self.config.get('gesture', 'position_stability_weight') - - # 计算综合稳定性得分 - gesture_stability = most_common_count / 3.0 - overall_stability = gesture_stability * (1.0 - position_weight) + position_stability * position_weight - - # 手势状态转换逻辑 - if overall_stability >= transition_threshold: - if most_common_gesture != self.last_stable_gesture: - self.gesture_stability_counter += 1 - else: - self.gesture_stability_counter = max(0, self.gesture_stability_counter - 1) - - if self.gesture_stability_counter >= stability_threshold: - self.current_gesture = most_common_gesture - self.current_confidence = np.mean(list(self.confidence_history)[-3:]) - self.last_stable_gesture = most_common_gesture - self.gesture_stability_counter = 0 - self.last_gesture_change_time = current_time + # 如果历史数据不足,返回当前手势 + if len(self.gesture_history) < 3: + self.current_gesture = new_gesture + self.current_confidence = new_confidence + return self.current_gesture, self.current_confidence + + # 使用加权投票算法确定最佳手势 + gesture_scores = {} + total_weight = 0 + + # 为历史中的每个手势计算加权分数 + for i in range(len(self.gesture_history)): + gesture = self.gesture_history[i] + conf = self.confidence_history[i] + wgt = self.weight_history[i] + + # 计算时间衰减因子(越近的权重越高) + time_factor = 1.0 + if i < len(self.timestamp_history): + time_diff = current_time - self.timestamp_history[i] + time_factor = max(0.1, 1.0 - time_diff / 3.0) # 3秒内衰减到0.1 + + # 计算综合权重 + final_weight = wgt * time_factor * (i / len(self.gesture_history) + 0.5) # 越近权重越高 + + if gesture not in gesture_scores: + gesture_scores[gesture] = 0 + gesture_scores[gesture] += final_weight * conf + total_weight += final_weight + + if total_weight == 0: + self.current_gesture = new_gesture + self.current_confidence = new_confidence + return self.current_gesture, self.current_confidence + + # 找出得分最高的手势 + best_gesture = max(gesture_scores.items(), key=lambda x: x[1])[0] + best_score = gesture_scores[best_gesture] / total_weight + + # 计算位置稳定性 + position_stability = 1.0 + if len(self.position_history) >= 2 and hand_data is not None: + current_pos = hand_data['position'] + prev_pos = self.position_history[-2] if len(self.position_history) >= 2 else current_pos + position_diff = math.sqrt((current_pos[0] - prev_pos[0]) ** 2 + (current_pos[1] - prev_pos[1]) ** 2) + position_stability = max(0, 1.0 - position_diff * 5.0) + + # 检查手势稳定性阈值 + stability_threshold = self.mode_config['gesture_stability_threshold'] + transition_threshold = self.config.get('gesture', 'transition_threshold') + position_weight = self.config.get('gesture', 'position_stability_weight') + + # 计算综合稳定性得分 + gesture_stability = best_score + overall_stability = gesture_stability * (1.0 - position_weight) + position_stability * position_weight + + # 手势状态转换逻辑 + if overall_stability >= transition_threshold: + if best_gesture != self.last_stable_gesture: + self.gesture_stability_counter += 1 else: - self.gesture_stability_counter = max(0, self.gesture_stability_counter - 2) + self.gesture_stability_counter = max(0, self.gesture_stability_counter - 1) + + if self.gesture_stability_counter >= stability_threshold: + self.current_gesture = best_gesture + + # 计算加权平均置信度 + weighted_conf_sum = 0 + weight_sum = 0 + for i in range(len(self.gesture_history)): + if self.gesture_history[i] == best_gesture: + weighted_conf_sum += self.confidence_history[i] * self.weight_history[i] + weight_sum += self.weight_history[i] + + if weight_sum > 0: + self.current_confidence = weighted_conf_sum / weight_sum + else: + self.current_confidence = best_score + + self.last_stable_gesture = best_gesture + self.gesture_stability_counter = 0 + self.last_gesture_change_time = current_time + else: + self.gesture_stability_counter = max(0, self.gesture_stability_counter - 2) return self.current_gesture, self.current_confidence diff --git a/src/autonomus_drone_hand_gesture_project/gesture_stabilizer.py b/src/autonomus_drone_hand_gesture_project/gesture_stabilizer.py new file mode 100644 index 0000000000..aaca4572a3 --- /dev/null +++ b/src/autonomus_drone_hand_gesture_project/gesture_stabilizer.py @@ -0,0 +1,170 @@ +""" +手势稳定性模块 +负责手势的稳定性分析和优化 +作者: xiaoshiyuan888 +""" + +import numpy as np +from collections import deque +import time +import statistics + + +class GestureStabilizer: + """手势稳定性分析器""" + + def __init__(self, window_size=10): + self.window_size = window_size + self.gesture_history = deque(maxlen=window_size) + self.confidence_history = deque(maxlen=window_size) + self.timestamp_history = deque(maxlen=window_size) + self.position_history = deque(maxlen=window_size) + + # 稳定性参数 + self.stability_score = 0.0 + self.last_stable_gesture = None + self.last_stable_time = 0 + self.transition_count = 0 + + def add_gesture(self, gesture, confidence, position=None): + """添加手势到历史""" + current_time = time.time() + + self.gesture_history.append(gesture) + self.confidence_history.append(confidence) + self.timestamp_history.append(current_time) + + if position is not None: + self.position_history.append(position) + + # 检查手势转换 + if len(self.gesture_history) > 1: + if self.gesture_history[-1] != self.gesture_history[-2]: + self.transition_count += 1 + + return self.analyze_stability() + + def analyze_stability(self): + """分析手势稳定性""" + if len(self.gesture_history) < 2: + return { + 'stable': False, + 'stability_score': 0.0, + 'dominant_gesture': None, + 'confidence': 0.0 + } + + # 计算手势一致性 + gesture_counts = {} + for gesture in self.gesture_history: + if gesture not in gesture_counts: + gesture_counts[gesture] = 0 + gesture_counts[gesture] += 1 + + # 找出主导手势 + dominant_gesture = max(gesture_counts.items(), key=lambda x: x[1])[0] + consistency_score = gesture_counts[dominant_gesture] / len(self.gesture_history) + + # 计算置信度稳定性 + if len(self.confidence_history) > 0: + avg_confidence = np.mean(list(self.confidence_history)) + confidence_std = np.std(list(self.confidence_history)) if len(self.confidence_history) >= 2 else 0 + confidence_score = max(0, 1.0 - confidence_std) + else: + avg_confidence = 0 + confidence_score = 0 + + # 计算时间稳定性 + if len(self.timestamp_history) >= 2: + time_intervals = [] + for i in range(1, len(self.timestamp_history)): + interval = self.timestamp_history[i] - self.timestamp_history[i - 1] + time_intervals.append(interval) + + if len(time_intervals) > 0: + avg_interval = np.mean(time_intervals) + interval_std = np.std(time_intervals) if len(time_intervals) >= 2 else 0 + time_score = max(0, 1.0 - min(1.0, interval_std / 0.5)) # 0.5秒为标准间隔 + else: + time_score = 1.0 + else: + time_score = 1.0 + + # 计算位置稳定性 + position_score = 1.0 + if len(self.position_history) >= 2: + position_changes = [] + for i in range(1, len(self.position_history)): + pos1 = self.position_history[i - 1] + pos2 = self.position_history[i] + if pos1 is not None and pos2 is not None: + distance = np.sqrt((pos2[0] - pos1[0]) ** 2 + (pos2[1] - pos1[1]) ** 2) + position_changes.append(distance) + + if position_changes: + avg_change = np.mean(position_changes) + position_score = max(0, 1.0 - min(1.0, avg_change * 10)) # 缩放因子 + + # 计算综合稳定性分数 + weights = { + 'consistency': 0.4, + 'confidence': 0.3, + 'time': 0.2, + 'position': 0.1 + } + + self.stability_score = ( + consistency_score * weights['consistency'] + + confidence_score * weights['confidence'] + + time_score * weights['time'] + + position_score * weights['position'] + ) + + # 检查是否稳定 + is_stable = self.stability_score > 0.7 and consistency_score > 0.6 + + if is_stable and dominant_gesture != self.last_stable_gesture: + self.last_stable_gesture = dominant_gesture + self.last_stable_time = time.time() + + return { + 'stable': is_stable, + 'stability_score': self.stability_score, + 'dominant_gesture': dominant_gesture if is_stable else None, + 'confidence': avg_confidence, + 'consistency': consistency_score, + 'transition_count': self.transition_count + } + + def get_stability_level(self): + """获取稳定性等级""" + if self.stability_score >= 0.8: + return "excellent" + elif self.stability_score >= 0.6: + return "good" + elif self.stability_score >= 0.4: + return "fair" + else: + return "poor" + + def reset(self): + """重置稳定性分析""" + self.gesture_history.clear() + self.confidence_history.clear() + self.timestamp_history.clear() + self.position_history.clear() + self.stability_score = 0.0 + self.last_stable_gesture = None + self.transition_count = 0 + + def get_stats(self): + """获取统计信息""" + return { + 'history_size': len(self.gesture_history), + 'stability_score': self.stability_score, + 'stability_level': self.get_stability_level(), + 'last_stable_gesture': self.last_stable_gesture, + 'transition_count': self.transition_count, + 'avg_confidence': np.mean(list(self.confidence_history)) if self.confidence_history else 0, + 'gesture_variety': len(set(self.gesture_history)) + } \ No newline at end of file diff --git a/src/autonomus_drone_hand_gesture_project/main.py b/src/autonomus_drone_hand_gesture_project/main.py index 15368704bf..9abd02fc59 100644 --- a/src/autonomus_drone_hand_gesture_project/main.py +++ b/src/autonomus_drone_hand_gesture_project/main.py @@ -3,6 +3,7 @@ 手势控制无人机系统 - 主入口模块 协调所有子模块,实现完整的手势控制无人机系统 作者: xiaoshiyuan888 +优化版本:集成性能可视化模块 """ import sys @@ -25,6 +26,8 @@ from ui_renderer import ChineseUIRenderer from trajectory_recorder import GestureTrajectoryRecorder from advanced_gesture_analyzer import AdvancedGestureAnalyzer +from gesture_stabilizer import GestureStabilizer # 新增 +from performance_visualizer import PerformanceVisualizer # 新增 # 安全导入所需库 @@ -157,13 +160,13 @@ def init_camera(config): def print_welcome_message(cap, speech_manager, libs): """打印欢迎信息""" print("\n" + "=" * 80) - print("手势控制无人机系统 - 增强手势识别版") + print("手势控制无人机系统 - 增强手势识别版 v2.0") print("=" * 80) print("系统状态:") print(f" 摄像头: {'已连接' if cap else '模拟模式'}") - print(f" 手势识别: 增强的平滑处理算法 + 新手势支持") + print(f" 手势识别: 改进的平滑算法 + 手势稳定性分析") print(f" 语音反馈: {'已启用' if speech_manager.enabled else '已禁用'}") - print(f" 性能监控: 已启用") + print(f" 性能监控: 增强版 - 支持GPU监控和趋势预测") print(f" 轨迹记录: 支持录制/回放功能") print(f" 高级飞行模式: 方形轨迹、圆形盘旋、8字形飞行") print(f" AirSim: {'可用' if libs['airsim'] else '模拟模式'}") @@ -180,7 +183,7 @@ def print_instructions(): print(" - 按 [L] 键记录性能快照") print(" - 按 [K] 键导出性能日志") print(" - 按 [R] 键重置性能统计") - print(" - 系统自动监控: FPS, CPU, 内存, 识别时间等") + print(" - 系统自动监控: FPS, CPU, 内存, GPU, 识别时间等") print("4. 性能模式选择:") print(" - 按 [O] 键循环切换性能模式: 最快(fast) → 平衡(balanced) → 最准(accurate)") print("5. 新手势控制:") @@ -198,7 +201,9 @@ def print_instructions(): print(" [H]切换帮助显示 [R]重置手势识别 [T]切换显示模式 [D]调试信息") print("9. 语音控制:") print(" [V]切换语音反馈 [M]测试语音") - print("10. 按 [ESC] 安全退出") + print("10. 性能可视化:") + print(" [F]切换性能图表显示 [G]切换性能仪表盘显示") + print("11. 按 [ESC] 安全退出") print("=" * 80) print("程序启动成功!") print("-" * 80) @@ -207,7 +212,7 @@ def print_instructions(): def main(): """主函数""" print("=" * 60) - print("手势控制无人机系统 - 增强手势识别版") + print("手势控制无人机系统 - 增强手势识别版 v2.0") print("=" * 60) # 导入库 @@ -254,6 +259,14 @@ def main(): print("初始化高级手势分析器...") advanced_gesture_analyzer = AdvancedGestureAnalyzer(speech_manager, config) + # 新增:手势稳定性分析器 + print("初始化手势稳定性分析器...") + gesture_stabilizer = GestureStabilizer(window_size=15) + + # 新增:性能可视化器 + print("初始化性能可视化器...") + performance_visualizer = PerformanceVisualizer() + # 初始化摄像头 cap = init_camera(config) if cap and speech_manager.enabled: @@ -294,6 +307,10 @@ def main(): display_modes = ['normal', 'detailed', 'minimal'] current_display_mode = 0 + # 新增:性能可视化模式 + visualization_modes = ['none', 'charts', 'gauges'] + current_visualization_mode = 0 + # 主循环 print("\n进入主循环,按ESC退出...") @@ -324,6 +341,11 @@ def main(): # 记录手势统计 if gesture and gesture != "Waiting" and gesture != "摄像头错误": performance_analyzer.record_gesture(gesture, confidence) + + # 新增:更新手势稳定性分析 + if gesture_recognizer.last_hand_data: + position = gesture_recognizer.last_hand_data.get('position') + stability_info = gesture_stabilizer.add_gesture(gesture, confidence, position) else: # 模拟模式 frame = np.zeros((480, 640, 3), dtype=np.uint8) @@ -400,9 +422,11 @@ def main(): if config.get('display', 'show_flight_mode'): frame = ui_renderer.draw_flight_mode(frame, drone_controller) - # 绘制性能图表(如果启用详细模式) - if display_modes[current_display_mode] == 'detailed': - frame = ui_renderer.draw_performance_chart(frame, performance_analyzer) + # 绘制性能可视化 + if visualization_modes[current_visualization_mode] == 'charts': + frame = performance_visualizer.draw_comprehensive_charts(frame, performance_analyzer) + elif visualization_modes[current_visualization_mode] == 'gauges': + frame = performance_visualizer.draw_performance_gauges(frame, performance_analyzer) frame = ui_renderer.draw_help_bar(frame) @@ -417,7 +441,7 @@ def main(): frame = ui_renderer.draw_warning(frame, warning_msg) # 显示图像 - cv2.imshow('Gesture Controlled Drone - Enhanced Gestures', frame) + cv2.imshow('Gesture Controlled Drone - Enhanced Gestures v2.0', frame) # ========== 键盘控制 ========== key = cv2.waitKey(1) & 0xFF @@ -456,6 +480,10 @@ def main(): gesture_recognizer.set_ui_renderer(ui_renderer) print("✓ 手势识别已重置") + # 重置手势稳定性分析 + gesture_stabilizer.reset() + print("✓ 手势稳定性分析已重置") + # 语音提示 if speech_manager.enabled: speech_manager.speak_direct("手势识别已重置") @@ -523,6 +551,11 @@ def main(): print(f" CPU使用率: {snapshot['cpu_usage']:.1f}%") print(f" 内存使用率: {snapshot['memory_usage']:.1f}%") + # 显示手势稳定性信息 + stability_stats = gesture_stabilizer.get_stats() + print(f" 手势稳定性评分: {stability_stats['stability_score']:.2f}") + print(f" 手势稳定性等级: {stability_stats['stability_level']}") + # 语音提示 if speech_manager.enabled: speech_manager.speak('performance_snapshot', immediate=True) @@ -565,6 +598,16 @@ def main(): if speech_manager.enabled: speech_manager.speak_direct("性能统计已重置") + # 切换性能可视化模式 + elif key == ord('f') or key == ord('F'): + current_visualization_mode = (current_visualization_mode + 1) % len(visualization_modes) + mode_name = visualization_modes[current_visualization_mode] + print(f"性能可视化模式: {mode_name}") + + # 语音提示 + if speech_manager.enabled: + speech_manager.speak_direct(f"性能可视化模式: {mode_name}") + # 轨迹记录控制 elif key == ord('1'): # 开始录制轨迹 diff --git a/src/autonomus_drone_hand_gesture_project/performance_analyzer.py b/src/autonomus_drone_hand_gesture_project/performance_analyzer.py index c7a8d7cd8c..28cc037f97 100644 --- a/src/autonomus_drone_hand_gesture_project/performance_analyzer.py +++ b/src/autonomus_drone_hand_gesture_project/performance_analyzer.py @@ -2,6 +2,7 @@ 性能分析器模块 负责监控和报告系统性能 作者: xiaoshiyuan888 +优化版本:增加更多性能指标和趋势分析 """ import time @@ -12,6 +13,7 @@ from collections import deque, Counter import statistics + class PerformanceAnalyzer: """增强的性能分析器 - 监控和报告系统性能""" @@ -23,10 +25,14 @@ def __init__(self, speech_manager=None, psutil_lib=None, config=None): self.session_start_time = time.time() # 帧率统计 - self.frame_times = deque(maxlen=500) # 增加到500 + self.frame_times = deque(maxlen=500) self.frame_count = 0 - self.fps_history = deque(maxlen=200) # 增加到200 - self.frame_time_history = deque(maxlen=100) # 新增:帧时间历史 + self.fps_history = deque(maxlen=200) + self.frame_time_history = deque(maxlen=100) + + # 新增:FPS波动性分析 + self.fps_jitter_history = deque(maxlen=50) # FPS波动 + self.fps_stability_score = 100 # FPS稳定性评分 # 手势识别性能 self.gesture_recognition_times = deque(maxlen=200) @@ -35,22 +41,40 @@ def __init__(self, speech_manager=None, psutil_lib=None, config=None): self.min_recognition_time = float('inf') self.recognition_time_std = 0 + # 新增:手势识别延迟分布 + self.recognition_latency_distribution = { + 'excellent': 0, # <20ms + 'good': 0, # 20-35ms + 'fair': 0, # 35-50ms + 'poor': 0, # 50-100ms + 'bad': 0 # >100ms + } + # 系统资源监控 self.cpu_usage_history = deque(maxlen=200) self.memory_usage_history = deque(maxlen=200) - self.cpu_trend = deque(maxlen=50) # CPU使用趋势 - self.memory_trend = deque(maxlen=50) # 内存使用趋势 + self.cpu_trend = deque(maxlen=50) + self.memory_trend = deque(maxlen=50) + + # 新增:GPU监控(如果可用) + self.gpu_usage_history = deque(maxlen=100) + self.gpu_available = False + self.gpu_memory_history = deque(maxlen=100) # 性能事件记录 self.performance_events = [] self.performance_snapshots = [] - self.performance_anomalies = [] # 新增:性能异常记录 + self.performance_anomalies = [] # 手势统计 self.gesture_counts = {} self.gesture_confidence_sum = {} self.gesture_confidence_count = {} - self.gesture_recognition_latency = {} # 新增:手势识别延迟 + self.gesture_recognition_latency = {} + + # 新增:手势转换统计 + self.gesture_transitions = {} # 记录手势切换频率 + self.last_gesture = None # 错误统计 self.error_count = 0 @@ -61,16 +85,20 @@ def __init__(self, speech_manager=None, psutil_lib=None, config=None): self.drone_commands = 0 self.successful_commands = 0 self.failed_commands = 0 - self.command_latency_history = deque(maxlen=100) # 新增:命令延迟 + self.command_latency_history = deque(maxlen=100) # 轨迹记录统计 self.recording_sessions = 0 self.total_trajectory_points = 0 self.trajectory_recording_time = 0 + # 新增:系统响应时间统计 + self.system_response_times = deque(maxlen=50) + self.avg_system_response_time = 0 + # 性能日志 self.performance_log = [] - self.anomaly_log = [] # 新增:异常日志 + self.anomaly_log = [] self.log_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'performance_log.csv') self.anomaly_log_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'anomaly_log.csv') @@ -92,14 +120,14 @@ def __init__(self, speech_manager=None, psutil_lib=None, config=None): 'recognition_good': 35, 'recognition_warning': 50, 'recognition_critical': 100, - 'frame_time_excellent': 33, # 对应30fps - 'frame_time_warning': 66, # 对应15fps - 'frame_time_critical': 200, # 对应5fps + 'frame_time_excellent': 33, + 'frame_time_warning': 66, + 'frame_time_critical': 200, } # 性能状态 self.performance_status = "优秀" - self.performance_score = 100 # 新增:性能评分 + self.performance_score = 100 self.last_performance_report = 0 self.auto_report_interval = 60 @@ -108,13 +136,47 @@ def __init__(self, speech_manager=None, psutil_lib=None, config=None): self.cpu_trend = "稳定" self.memory_trend = "稳定" + # 新增:性能预测 + self.fps_prediction = 0 + self.cpu_prediction = 0 + self.memory_prediction = 0 + # 异常检测 self.anomaly_detection_enabled = True self.last_anomaly_check = 0 self.anomaly_check_interval = 10 + # 初始化GPU监控 + self.init_gpu_monitoring() + print("✓ 增强的性能分析器已初始化") + def init_gpu_monitoring(self): + """初始化GPU监控""" + try: + # 尝试导入GPU监控库 + import GPUtil + self.gpu_available = True + self.gpu_lib = GPUtil + print("[GPU] ✓ GPU监控库就绪") + except ImportError: + print("[GPU] ⚠ GPU监控库未找到,GPU监控功能受限") + self.gpu_available = False + + def update_gpu_stats(self): + """更新GPU统计""" + if not self.gpu_available: + return + + try: + gpus = self.gpu_lib.getGPUs() + if gpus: + gpu = gpus[0] # 使用第一个GPU + self.gpu_usage_history.append(gpu.load * 100) + self.gpu_memory_history.append(gpu.memoryUtil * 100) + except: + pass + def update_frame(self): """更新帧统计""" current_time = time.time() @@ -133,9 +195,22 @@ def update_frame(self): frame_time = (self.frame_times[-1] - self.frame_times[-2]) * 1000 self.frame_time_history.append(frame_time) + # 计算FPS波动 + if len(self.fps_history) >= 2: + fps_jitter = abs(self.fps_history[-1] - self.fps_history[-2]) + self.fps_jitter_history.append(fps_jitter) + + # 更新GPU统计 + if self.frame_count % 30 == 0: # 每30帧更新一次GPU信息 + self.update_gpu_stats() + # 更新性能评分 self.update_performance_score() + # 更新系统响应时间 + if len(self.system_response_times) > 0: + self.avg_system_response_time = statistics.mean(list(self.system_response_times)) + def update_performance_score(self): """更新性能评分""" score = 100 @@ -154,6 +229,19 @@ def update_performance_score(self): else: score -= 70 + # 基于FPS稳定性评分 + if len(self.fps_jitter_history) > 0: + avg_jitter = statistics.mean(list(self.fps_jitter_history)) + if avg_jitter < 2: + score -= 0 + elif avg_jitter < 5: + score -= 5 + elif avg_jitter < 10: + score -= 15 + else: + score -= 25 + self.fps_stability_score = max(0, 100 - avg_jitter * 10) + # 基于CPU评分 cpu_usage = self.get_current_cpu_usage() if cpu_usage <= self.performance_thresholds['cpu_excellent']: @@ -193,6 +281,18 @@ def update_performance_score(self): else: score -= 50 + # 基于GPU评分(如果可用) + if self.gpu_available and len(self.gpu_usage_history) > 0: + gpu_usage = self.gpu_usage_history[-1] if self.gpu_usage_history else 0 + if gpu_usage < 60: + score -= 0 + elif gpu_usage < 80: + score -= 5 + elif gpu_usage < 90: + score -= 10 + else: + score -= 20 + self.performance_score = max(0, min(100, score)) # 更新性能状态 @@ -221,6 +321,18 @@ def update_gesture_recognition_time(self, recognition_time_ms): if len(times_list) >= 2: self.recognition_time_std = np.std(times_list) + # 更新识别延迟分布 + if recognition_time_ms < 20: + self.recognition_latency_distribution['excellent'] += 1 + elif recognition_time_ms < 35: + self.recognition_latency_distribution['good'] += 1 + elif recognition_time_ms < 50: + self.recognition_latency_distribution['fair'] += 1 + elif recognition_time_ms < 100: + self.recognition_latency_distribution['poor'] += 1 + else: + self.recognition_latency_distribution['bad'] += 1 + def update_system_resources(self): """更新系统资源使用情况""" try: @@ -240,12 +352,46 @@ def update_system_resources(self): recent_memory = list(self.memory_usage_history)[-10:] self.memory_trend.append(statistics.mean(recent_memory)) + # 性能预测 + self.predict_performance() + # 检查性能问题和异常 self.check_performance_issues(cpu_percent, memory_percent) self.detect_anomalies() except: pass + def predict_performance(self): + """性能预测""" + try: + # 简单的线性回归预测 + if len(self.fps_history) >= 10: + recent_fps = list(self.fps_history)[-10:] + if len(recent_fps) >= 2: + # 计算趋势 + x = np.arange(len(recent_fps)) + y = np.array(recent_fps) + z = np.polyfit(x, y, 1) + self.fps_prediction = max(0, z[0] * len(recent_fps) + z[1]) + + if len(self.cpu_usage_history) >= 10: + recent_cpu = list(self.cpu_usage_history)[-10:] + if len(recent_cpu) >= 2: + x = np.arange(len(recent_cpu)) + y = np.array(recent_cpu) + z = np.polyfit(x, y, 1) + self.cpu_prediction = min(100, max(0, z[0] * len(recent_cpu) + z[1])) + + if len(self.memory_usage_history) >= 10: + recent_memory = list(self.memory_usage_history)[-10:] + if len(recent_memory) >= 2: + x = np.arange(len(recent_memory)) + y = np.array(recent_memory) + z = np.polyfit(x, y, 1) + self.memory_prediction = min(100, max(0, z[0] * len(recent_memory) + z[1])) + except: + pass + def detect_anomalies(self): """检测性能异常""" if not self.anomaly_detection_enabled: @@ -273,6 +419,10 @@ def detect_anomalies(self): if older_avg > 0 and avg_fps / older_avg < 0.5: anomalies.append(("FPS骤降", f"FPS从{older_avg:.1f}降至{avg_fps:.1f}")) + # 检测FPS波动过大 + if std_fps > 10: + anomalies.append(("FPS不稳定", f"FPS波动过大: 标准差{std_fps:.1f}")) + # 检测CPU使用率异常 if len(self.cpu_usage_history) >= 10: recent_cpu = list(self.cpu_usage_history)[-10:] @@ -299,6 +449,13 @@ def detect_anomalies(self): if second_avg - first_avg > 10: # 内存增长超过10% anomalies.append(("内存增长", f"内存从{first_avg:.1f}%增长到{second_avg:.1f}%")) + # 检测手势识别时间异常 + if len(self.gesture_recognition_times) >= 10: + recent_times = list(self.gesture_recognition_times)[-10:] + avg_time = statistics.mean(recent_times) + if avg_time > 80: # 识别时间超过80ms + anomalies.append(("识别缓慢", f"手势识别平均时间{avg_time:.1f}ms")) + # 记录异常 for anomaly_type, message in anomalies: anomaly = { @@ -308,7 +465,8 @@ def detect_anomalies(self): 'fps': self.get_current_fps(), 'cpu': self.get_current_cpu_usage(), 'memory': self.get_current_memory_usage(), - 'recognition_time': self.avg_recognition_time + 'recognition_time': self.avg_recognition_time, + 'performance_score': self.performance_score } self.performance_anomalies.append(anomaly) self.log_anomaly(anomaly) @@ -316,7 +474,7 @@ def detect_anomalies(self): # 语音提示严重异常 if (self.speech_manager and self.speech_manager.enabled and - "骤降" in anomaly_type or "飙升" in anomaly_type): + ("骤降" in anomaly_type or "飙升" in anomaly_type or "缓慢" in anomaly_type)): self.speech_manager.speak_direct(f"检测到性能{anomaly_type}") def check_performance_issues(self, cpu_percent, memory_percent): @@ -384,6 +542,19 @@ def check_performance_issues(self, cpu_percent, memory_percent): self.performance_status = "警告" self.warning_count += 1 + # 检查GPU使用率 + if self.gpu_available and len(self.gpu_usage_history) > 0: + gpu_usage = self.gpu_usage_history[-1] if self.gpu_usage_history else 0 + if gpu_usage > 95: + issues.append(("严重", f"GPU使用率极高: {gpu_usage:.1f}%")) + self.performance_status = "严重" + self.critical_count += 1 + elif gpu_usage > 90: + issues.append(("警告", f"GPU使用率较高: {gpu_usage:.1f}%")) + if self.performance_status == "优秀" or self.performance_status == "良好": + self.performance_status = "警告" + self.warning_count += 1 + # 记录性能事件 if issues: for level, message in issues: @@ -408,7 +579,8 @@ def add_performance_event(self, level, message): 'fps': self.get_current_fps(), 'cpu': self.get_current_cpu_usage(), 'memory': self.get_current_memory_usage(), - 'recognition_time': self.avg_recognition_time + 'recognition_time': self.avg_recognition_time, + 'performance_score': self.performance_score } self.performance_events.append(event) @@ -430,7 +602,8 @@ def log_performance_event(self, event): 'fps': f"{event['fps']:.1f}", 'cpu': f"{event['cpu']:.1f}", 'memory': f"{event['memory']:.1f}", - 'recognition_time': f"{event['recognition_time']:.1f}" + 'recognition_time': f"{event['recognition_time']:.1f}", + 'performance_score': f"{event['performance_score']:.1f}" } self.performance_log.append(log_entry) @@ -443,7 +616,8 @@ def log_anomaly(self, anomaly): 'fps': f"{anomaly['fps']:.1f}", 'cpu': f"{anomaly['cpu']:.1f}", 'memory': f"{anomaly['memory']:.1f}", - 'recognition_time': f"{anomaly['recognition_time']:.1f}" + 'recognition_time': f"{anomaly['recognition_time']:.1f}", + 'performance_score': f"{anomaly['performance_score']:.1f}" } self.anomaly_log.append(log_entry) @@ -459,6 +633,15 @@ def record_gesture(self, gesture, confidence): self.gesture_confidence_sum[gesture] += confidence self.gesture_confidence_count[gesture] += 1 + # 记录手势转换 + if self.last_gesture is not None and self.last_gesture != gesture: + transition_key = f"{self.last_gesture}->{gesture}" + if transition_key not in self.gesture_transitions: + self.gesture_transitions[transition_key] = 0 + self.gesture_transitions[transition_key] += 1 + + self.last_gesture = gesture + # 记录最近一次识别时间 if len(self.gesture_recognition_times) > 0: last_time = self.gesture_recognition_times[-1] @@ -485,6 +668,10 @@ def record_recording_session(self, points_count=0): if points_count > 0: self.trajectory_recording_time = time.time() - self.session_start_time + def record_system_response_time(self, response_time): + """记录系统响应时间""" + self.system_response_times.append(response_time) + def take_snapshot(self, label=""): """拍摄性能快照""" snapshot = { @@ -508,8 +695,18 @@ def take_snapshot(self, label=""): 'session_duration': time.time() - self.session_start_time, 'warning_count': self.warning_count, 'error_count': self.error_count, - 'critical_count': self.critical_count + 'critical_count': self.critical_count, + 'fps_stability_score': self.fps_stability_score, + 'recognition_latency_distribution': dict(self.recognition_latency_distribution), + 'gesture_transitions': dict(self.gesture_transitions), + 'avg_system_response_time': self.avg_system_response_time } + + # 添加GPU信息(如果可用) + if self.gpu_available and len(self.gpu_usage_history) > 0: + snapshot['gpu_usage'] = self.gpu_usage_history[-1] if self.gpu_usage_history else 0 + snapshot['gpu_memory'] = self.gpu_memory_history[-1] if self.gpu_memory_history else 0 + self.performance_snapshots.append(snapshot) print(f"📸 性能快照已保存: {label}") @@ -582,6 +779,16 @@ def get_fps_trend(self): return "下降" return "稳定" + def get_fps_stability(self): + """获取FPS稳定性评分""" + return self.fps_stability_score + + def get_gpu_usage(self): + """获取GPU使用率""" + if self.gpu_available and len(self.gpu_usage_history) > 0: + return self.gpu_usage_history[-1] + return 0 + def generate_report(self, detailed=True): """生成性能报告""" report_time = time.time() @@ -596,6 +803,7 @@ def generate_report(self, detailed=True): '当前FPS': f"{self.get_current_fps():.1f}", '最低FPS': f"{min(self.fps_history) if self.fps_history else 0:.1f}", 'FPS稳定性': f"{self.get_fps_percentile(90) - self.get_fps_percentile(10):.1f}", + 'FPS稳定性评分': f"{self.fps_stability_score:.0f}", '平均手势识别时间': f"{self.avg_recognition_time:.1f}ms", '最快识别时间': f"{self.min_recognition_time if self.min_recognition_time != float('inf') else 0:.1f}ms", '最慢识别时间': f"{self.max_recognition_time:.1f}ms", @@ -622,10 +830,18 @@ def generate_report(self, detailed=True): '趋势分析': { 'FPS趋势': self.get_fps_trend(), 'CPU趋势': self.get_cpu_trend(), - '内存趋势': self.get_memory_trend() - } + '内存趋势': self.get_memory_trend(), + 'FPS预测': f"{self.fps_prediction:.1f}", + 'CPU预测': f"{self.cpu_prediction:.1f}%", + '内存预测': f"{self.memory_prediction:.1f}%" + }, + '系统响应时间': f"{self.avg_system_response_time:.1f}ms" } + # 添加GPU信息(如果可用) + if self.gpu_available: + report['GPU使用率'] = f"{self.get_gpu_usage():.1f}%" + # 详细报告 if detailed: # 手势统计 @@ -651,6 +867,23 @@ def generate_report(self, detailed=True): report['手势统计'] = gesture_stats + # 手势转换统计 + if self.gesture_transitions: + report['手势转换统计'] = dict(sorted( + self.gesture_transitions.items(), + key=lambda x: x[1], + reverse=True + )[:10]) # 只显示前10个最常见转换 + + # 识别延迟分布 + total_latency = sum(self.recognition_latency_distribution.values()) + if total_latency > 0: + latency_dist = {} + for category, count in self.recognition_latency_distribution.items(): + percentage = (count / total_latency * 100) if total_latency > 0 else 0 + latency_dist[category] = f"{count}次({percentage:.1f}%)" + report['识别延迟分布'] = latency_dist + # 性能事件 if self.performance_events: recent_events = list(self.performance_events)[-10:] @@ -712,6 +945,12 @@ def generate_suggestions(self): if self.performance_score < 50: suggestions.append("考虑升级硬件配置(CPU/GPU)以提升性能") + # 检查FPS稳定性 + if len(self.fps_jitter_history) > 0: + avg_jitter = statistics.mean(list(self.fps_jitter_history)) + if avg_jitter > 5: + suggestions.append(f"FPS波动较大({avg_jitter:.1f}FPS),建议关闭其他运行程序,优化系统性能") + # 检查CPU cpu_usage = self.get_current_cpu_usage() if cpu_usage > self.performance_thresholds['cpu_warning']: @@ -732,16 +971,23 @@ def generate_suggestions(self): if self.avg_recognition_time > 80: suggestions.append("考虑使用更简单的手势识别算法或优化当前算法") - # 检查FPS稳定性 - if len(self.fps_history) >= 20: - fps_std = np.std(list(self.fps_history)[-20:]) - if fps_std > 10: - suggestions.append(f"帧率不稳定(标准差{fps_std:.1f}FPS),建议检查系统负载和网络连接") + # 检查GPU使用率(如果可用) + if self.gpu_available and len(self.gpu_usage_history) > 0: + gpu_usage = self.get_gpu_usage() + if gpu_usage > 90: + suggestions.append(f"GPU使用率较高({gpu_usage:.1f}%),可能影响系统性能") # 检查性能趋势 if self.get_fps_trend() == "下降": suggestions.append("FPS呈下降趋势,建议重启程序或检查系统资源") + # 检查手势识别延迟分布 + total_latency = sum(self.recognition_latency_distribution.values()) + if total_latency > 0: + poor_ratio = (self.recognition_latency_distribution['poor'] + self.recognition_latency_distribution['bad']) / total_latency + if poor_ratio > 0.3: # 超过30%的识别时间较差 + suggestions.append(f"手势识别延迟较高,{poor_ratio*100:.0f}%的识别时间超过50ms") + return suggestions def print_report(self, detailed=True): @@ -760,6 +1006,7 @@ def print_report(self, detailed=True): print(f"当前FPS: {report['当前FPS']}") print(f"最低FPS: {report['最低FPS']}") print(f"FPS稳定性: {report['FPS稳定性']}") + print(f"FPS稳定性评分: {report['FPS稳定性评分']}") print(f"平均手势识别时间: {report['平均手势识别时间']}") print(f"最快识别时间: {report['最快识别时间']}") print(f"最慢识别时间: {report['最慢识别时间']}") @@ -772,12 +1019,23 @@ def print_report(self, detailed=True): print(f"错误数量: {report['错误数量']}") print(f"严重问题数量: {report['严重问题数量']}") + # GPU信息 + if 'GPU使用率' in report: + print(f"GPU使用率: {report['GPU使用率']}") + # 趋势分析 trends = report['趋势分析'] print(f"\n趋势分析:") print(f" FPS趋势: {trends['FPS趋势']}") print(f" CPU趋势: {trends['CPU趋势']}") print(f" 内存趋势: {trends['内存趋势']}") + if 'FPS预测' in trends: + print(f" FPS预测: {trends['FPS预测']} FPS") + print(f" CPU预测: {trends['CPU预测']}") + print(f" 内存预测: {trends['内存预测']}") + + # 系统响应时间 + print(f"系统响应时间: {report['系统响应时间']}") # 无人机命令统计 cmd_stats = report['无人机命令'] @@ -801,6 +1059,18 @@ def print_report(self, detailed=True): for gesture, stats in report['手势统计'].items(): print(f" {gesture}: {stats['次数']}次 ({stats['占比']}), 平均置信度: {stats['平均置信度']}, 平均延迟: {stats['平均延迟']}") + # 手势转换统计 + if detailed and '手势转换统计' in report and report['手势转换统计']: + print(f"\n最常见的手势转换:") + for transition, count in report['手势转换统计'].items(): + print(f" {transition}: {count}次") + + # 识别延迟分布 + if detailed and '识别延迟分布' in report and report['识别延迟分布']: + print(f"\n识别延迟分布:") + for category, dist in report['识别延迟分布'].items(): + print(f" {category}: {dist}") + # FPS分布 if detailed and 'FPS分布' in report: fps_dist = report['FPS分布'] @@ -913,6 +1183,8 @@ def reset_session(self): self.gesture_confidence_sum = {} self.gesture_confidence_count = {} self.gesture_recognition_latency = {} + self.gesture_transitions = {} + self.last_gesture = None self.error_count = 0 self.warning_count = 0 self.critical_count = 0 @@ -926,12 +1198,23 @@ def reset_session(self): self.recording_sessions = 0 self.total_trajectory_points = 0 self.trajectory_recording_time = 0 + self.recognition_latency_distribution = { + 'excellent': 0, + 'good': 0, + 'fair': 0, + 'poor': 0, + 'bad': 0 + } + self.fps_jitter_history.clear() + self.fps_stability_score = 100 + self.system_response_times.clear() + self.avg_system_response_time = 0 print("✓ 性能统计会话已重置") def get_stats_summary(self): """获取统计摘要""" - return { + summary = { 'fps': self.get_current_fps(), 'avg_fps': self.get_average_fps(), 'min_fps': min(self.fps_history) if self.fps_history else 0, @@ -946,9 +1229,17 @@ def get_stats_summary(self): 'cpu_trend': self.get_cpu_trend(), 'memory_trend': self.get_memory_trend(), 'warning_count': self.warning_count, - 'error_count': self.error_count + 'error_count': self.error_count, + 'fps_stability': self.fps_stability_score, + 'system_response_time': self.avg_system_response_time } + # 添加GPU信息(如果可用) + if self.gpu_available: + summary['gpu_usage'] = self.get_gpu_usage() + + return summary + def get_detailed_stats(self): """获取详细统计""" return { @@ -971,5 +1262,11 @@ def get_detailed_stats(self): 'memory_max': max(self.memory_usage_history) if self.memory_usage_history else 0 }, 'anomaly_count': len(self.performance_anomalies), - 'snapshot_count': len(self.performance_snapshots) + 'snapshot_count': len(self.performance_snapshots), + 'fps_stability_stats': { + 'avg_jitter': statistics.mean(self.fps_jitter_history) if self.fps_jitter_history else 0, + 'max_jitter': max(self.fps_jitter_history) if self.fps_jitter_history else 0 + }, + 'recognition_latency_distribution': dict(self.recognition_latency_distribution), + 'gesture_transition_count': len(self.gesture_transitions) } \ No newline at end of file diff --git a/src/autonomus_drone_hand_gesture_project/performance_visualizer.py b/src/autonomus_drone_hand_gesture_project/performance_visualizer.py new file mode 100644 index 0000000000..99ccb05979 --- /dev/null +++ b/src/autonomus_drone_hand_gesture_project/performance_visualizer.py @@ -0,0 +1,232 @@ +""" +性能可视化模块 +负责绘制性能图表和可视化 +作者: xiaoshiyuan888 +""" + +import cv2 +import numpy as np +from collections import deque +import math + + +class PerformanceVisualizer: + """性能可视化器""" + + def __init__(self, width=300, height=200): + self.width = width + self.height = height + self.colors = { + 'fps': (0, 255, 0), + 'cpu': (255, 165, 0), + 'memory': (0, 165, 255), + 'recognition': (255, 0, 255), + 'background': (30, 30, 30), + 'grid': (60, 60, 60), + 'text': (200, 200, 200), + 'warning': (255, 165, 0), + 'critical': (255, 0, 0), + 'good': (0, 255, 0) + } + + def draw_performance_chart(self, frame, data_history, title="", x_offset=0, y_offset=0, + max_value=None, color=None, unit=""): + """绘制单个性能图表""" + if not data_history or len(data_history) < 2: + return frame + + # 创建图表画布 + chart_height = 80 + chart_width = 200 + + # 绘制图表背景 + cv2.rectangle(frame, + (x_offset, y_offset), + (x_offset + chart_width, y_offset + chart_height), + self.colors['background'], -1) + + cv2.rectangle(frame, + (x_offset, y_offset), + (x_offset + chart_width, y_offset + chart_height), + self.colors['grid'], 1) + + # 绘制标题 + cv2.putText(frame, title, (x_offset + 5, y_offset + 15), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.colors['text'], 1) + + # 获取数据 + values = list(data_history)[-min(20, len(data_history)):] + if not values: + return frame + + # 计算最大值 + if max_value is None: + data_max = max(values) + else: + data_max = max_value + + data_min = min(values) + + if data_max == data_min: + data_max = data_min + 1 + + # 绘制网格线 + for i in range(1, 4): + grid_y = y_offset + chart_height - 20 - i * (chart_height - 40) // 4 + cv2.line(frame, + (x_offset + 10, grid_y), + (x_offset + chart_width - 10, grid_y), + self.colors['grid'], 1) + + # 绘制刻度值 + tick_value = data_min + (data_max - data_min) * i / 4 + tick_text = f"{tick_value:.0f}{unit}" + cv2.putText(frame, tick_text, + (x_offset + chart_width - 35, grid_y - 5), + cv2.FONT_HERSHEY_SIMPLEX, 0.4, self.colors['text'], 1) + + # 绘制数据曲线 + points = [] + for i, value in enumerate(values): + point_x = x_offset + 10 + i * (chart_width - 20) / (len(values) - 1) if len(values) > 1 else x_offset + 10 + point_y = y_offset + chart_height - 20 - (value - data_min) * (chart_height - 40) / (data_max - data_min) + points.append((int(point_x), int(point_y))) + + # 连接点形成曲线 + chart_color = color if color else self.colors['fps'] + for i in range(len(points) - 1): + cv2.line(frame, points[i], points[i + 1], chart_color, 2) + + # 绘制当前值 + if values: + current_value = values[-1] + current_text = f"当前: {current_value:.1f}{unit}" + cv2.putText(frame, current_text, + (x_offset + 10, y_offset + 30), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, chart_color, 1) + + return frame + + def draw_comprehensive_charts(self, frame, performance_analyzer): + """绘制综合性能图表""" + x_start = frame.shape[1] - 250 + y_start = 150 + chart_spacing = 90 + + # FPS图表 + frame = self.draw_performance_chart( + frame, performance_analyzer.fps_history, + "FPS监控", x_start, y_start, + max_value=60, color=self.colors['fps'], unit="FPS" + ) + + # CPU图表 + frame = self.draw_performance_chart( + frame, performance_analyzer.cpu_usage_history, + "CPU使用率", x_start, y_start + chart_spacing, + max_value=100, color=self.colors['cpu'], unit="%" + ) + + # 内存图表 + frame = self.draw_performance_chart( + frame, performance_analyzer.memory_usage_history, + "内存使用率", x_start, y_start + chart_spacing * 2, + max_value=100, color=self.colors['memory'], unit="%" + ) + + # 识别时间图表 + if len(performance_analyzer.gesture_recognition_times) > 1: + frame = self.draw_performance_chart( + frame, performance_analyzer.gesture_recognition_times, + "识别时间", x_start, y_start + chart_spacing * 3, + max_value=100, color=self.colors['recognition'], unit="ms" + ) + + return frame + + def draw_performance_gauge(self, frame, value, max_value, label, x_offset, y_offset, color=None): + """绘制性能仪表盘""" + gauge_radius = 30 + center_x = x_offset + gauge_radius + 5 + center_y = y_offset + gauge_radius + 5 + + # 绘制仪表盘背景 + cv2.circle(frame, (center_x, center_y), gauge_radius, self.colors['background'], -1) + cv2.circle(frame, (center_x, center_y), gauge_radius, self.colors['grid'], 2) + + # 计算角度 + angle = 180 + (value / max_value) * 180 + + # 绘制指针 + end_x = center_x + int(gauge_radius * 0.7 * math.cos(math.radians(angle))) + end_y = center_y + int(gauge_radius * 0.7 * math.sin(math.radians(angle))) + + gauge_color = color if color else self.colors['good'] + cv2.line(frame, (center_x, center_y), (end_x, end_y), gauge_color, 3) + + # 绘制中心点 + cv2.circle(frame, (center_x, center_y), 5, gauge_color, -1) + + # 绘制标签和值 + cv2.putText(frame, label, (x_offset, y_offset + gauge_radius * 2 + 20), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, self.colors['text'], 1) + cv2.putText(frame, f"{value:.0f}", (center_x - 15, center_y + 5), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, gauge_color, 1) + + return frame + + def draw_performance_gauges(self, frame, performance_analyzer): + """绘制性能仪表盘组""" + x_start = frame.shape[1] - 180 + y_start = 150 + gauge_spacing = 80 + + # CPU仪表盘 + cpu_usage = performance_analyzer.get_current_cpu_usage() + cpu_color = self.colors['good'] + if cpu_usage > 80: + cpu_color = self.colors['critical'] + elif cpu_usage > 60: + cpu_color = self.colors['warning'] + + frame = self.draw_performance_gauge( + frame, cpu_usage, 100, "CPU", x_start, y_start, cpu_color + ) + + # 内存仪表盘 + memory_usage = performance_analyzer.get_current_memory_usage() + memory_color = self.colors['good'] + if memory_usage > 85: + memory_color = self.colors['critical'] + elif memory_usage > 70: + memory_color = self.colors['warning'] + + frame = self.draw_performance_gauge( + frame, memory_usage, 100, "内存", x_start, y_start + gauge_spacing, memory_color + ) + + # FPS仪表盘 + fps = performance_analyzer.get_current_fps() + fps_color = self.colors['good'] + if fps < 15: + fps_color = self.colors['critical'] + elif fps < 25: + fps_color = self.colors['warning'] + + frame = self.draw_performance_gauge( + frame, min(fps, 60), 60, "FPS", x_start, y_start + gauge_spacing * 2, fps_color + ) + + # 性能评分仪表盘 + performance_score = performance_analyzer.performance_score + score_color = self.colors['good'] + if performance_score < 40: + score_color = self.colors['critical'] + elif performance_score < 70: + score_color = self.colors['warning'] + + frame = self.draw_performance_gauge( + frame, performance_score, 100, "评分", x_start, y_start + gauge_spacing * 3, score_color + ) + + return frame \ No newline at end of file