From 5814e0c0a0805a91a76b8dac95229dd684a3543b Mon Sep 17 00:00:00 2001 From: Arrebol01 <1245298208@qq.com> Date: Fri, 19 Dec 2025 22:04:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E6=B7=BB=E5=B8=A7=E7=8E=87?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Drone_hand_gesture_project/main.py | 31 ++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Drone_hand_gesture_project/main.py b/src/Drone_hand_gesture_project/main.py index edcb92f282..6b322cea30 100644 --- a/src/Drone_hand_gesture_project/main.py +++ b/src/Drone_hand_gesture_project/main.py @@ -12,11 +12,13 @@ # 导入自定义模块 try: from gesture_detector_enhanced import EnhancedGestureDetector + print("✅ 导入增强版手势检测器 (机器学习)") HAS_ENHANCED_DETECTOR = True except ImportError: print("⚠️ 未找到增强版检测器,使用原始手势检测器") from gesture_detector import GestureDetector + HAS_ENHANCED_DETECTOR = False from drone_controller import DroneController @@ -246,11 +248,11 @@ def _gesture_recognition_loop(self): # 虚拟模式 frame = np.ones((480, 640, 3), dtype=np.uint8) * 255 cv2.putText(frame, "虚拟摄像头模式", (50, 50), - cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.putText(frame, f"手势指令 ({mode_text}):", (50, 100), - cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 100, 0), 2) + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 100, 0), 2) cv2.putText(frame, "张开手掌 - 起飞", (50, 140), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) cv2.putText(frame, "握拳 - 降落", (50, 170), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) cv2.putText(frame, "胜利手势 - 前进", (50, 200), @@ -428,6 +430,10 @@ def _simulation_loop(self): frame_count = 0 last_status_print = time.time() + # 帧率控制 + target_fps = 60 + frame_delay = 1.0 / target_fps + print("\n🎮 键盘提示:按 'R' 键重置无人机位置到原点") print(" 按 'T' 键手动起飞") print(" 按 'L' 键手动降落") @@ -437,10 +443,16 @@ def _simulation_loop(self): self._last_key_press = {} while self.running: + start_time = time.time() current_time = time.time() dt = current_time - last_time last_time = current_time + if dt <= 0: + dt = frame_delay + elif dt > 0.1: + dt = 0.1 + # 每3秒打印一次状态 if current_time - last_status_print > 3: status = self.drone_controller.get_status_string() @@ -449,11 +461,6 @@ def _simulation_loop(self): print(f"[状态监控] 当前手势: {self.current_gesture} (置信度: {self.gesture_confidence:.2f})") last_status_print = current_time - if dt <= 0: - dt = 0.016 - elif dt > 0.1: - dt = 0.1 - if self.paused: if not self.viewer.handle_events(): self.running = False @@ -526,9 +533,15 @@ def _simulation_loop(self): self.viewer.render(drone_state_with_gesture, trajectory) + # 控制帧率,避免CPU占用过高 + elapsed = time.time() - start_time + sleep_time = frame_delay - elapsed + if sleep_time > 0: + time.sleep(sleep_time) + frame_count += 1 if frame_count % 120 == 0: - fps = 1.0 / dt if dt > 0 else 0 + fps = 1.0 / (time.time() - start_time) if start_time > 0 else 0 print(f"3D仿真帧率: {fps:.1f} FPS") print("3D仿真线程结束") From 515a93d6b41e3f19fcae33e5646dfe32ef345539 Mon Sep 17 00:00:00 2001 From: Arrebol01 <1245298208@qq.com> Date: Sat, 20 Dec 2025 11:33:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E5=86=B7=E5=8D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Drone_hand_gesture_project/main.py | 30 ++++++++++++++++++-------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Drone_hand_gesture_project/main.py b/src/Drone_hand_gesture_project/main.py index 6b322cea30..3bfbea354e 100644 --- a/src/Drone_hand_gesture_project/main.py +++ b/src/Drone_hand_gesture_project/main.py @@ -385,10 +385,17 @@ def _process_gesture_command(self, gesture, confidence): # 检查是否在冷却期内 in_cooldown = current_time - self.last_command_time <= self.command_cooldown + # 检查是否是重复手势(避免频繁处理同一个手势) + same_gesture = (gesture == self.current_gesture and + hasattr(self, 'last_processed_gesture') and + gesture == self.last_processed_gesture and + current_time - getattr(self, 'last_processed_time', 0) < 2.0) + # 只处理置信度高于阈值的手势且不在冷却期 if (gesture not in ["no_hand", "hand_detected"] and confidence > threshold and - not in_cooldown): + not in_cooldown and + not same_gesture): # 获取控制命令 command = self.gesture_detector.get_command(gesture) @@ -411,16 +418,21 @@ def _process_gesture_command(self, gesture, confidence): # 记录命令 self._log_command(gesture, command, confidence, intensity) - # 更新最后命令时间 + # 更新最后命令时间和手势状态 self.last_command_time = current_time + self.last_processed_gesture = gesture + self.last_processed_time = current_time elif gesture not in ["no_hand", "hand_detected"] and confidence > 0.3: - # 显示检测到但未触发的情况(仅调试用,可注释掉) - if in_cooldown: - # 冷却期内,不显示信息避免干扰 - pass - elif confidence < threshold: - # 置信度不足,显示信息 - print(f" [手势检测] {gesture} 置信度不足: {confidence:.2f} < {threshold}") + # 只在调试模式下显示检测到但未触发的情况 + debug_mode = False # 可以设为True启用详细调试 + if debug_mode: + if in_cooldown: + print( + f" [冷却中] {gesture} 冷却时间剩余: {self.command_cooldown - (current_time - self.last_command_time):.1f}s") + elif same_gesture: + print(f" [重复手势] {gesture} 已处理过,冷却中") + elif confidence < threshold: + print(f" [置信度不足] {gesture} 置信度: {confidence:.2f} < 阈值: {threshold}") def _simulation_loop(self): """仿真主循环"""