diff --git a/src/Drone_hand_gesture_project/drone_controller.py b/src/Drone_hand_gesture_project/drone_controller.py index 065984d051..fd4c2f28e6 100644 --- a/src/Drone_hand_gesture_project/drone_controller.py +++ b/src/Drone_hand_gesture_project/drone_controller.py @@ -44,14 +44,14 @@ def _send_mavlink_command(self, command): def _simulate_command(self, command): commands = { - "takeoff": "无人机起飞", - "land": "无人机降落", - "up": "无人机上升", - "down": "无人机下降", - "forward": "无人机前进", - "backward": "无人机后退", - "hover": "无人机悬停", - "stop": "无人机停止", + "takeoff": "无人机起飞!", + "land": "无人机降落!", + "up": "无人机上升!", + "down": "无人机下降!", + "forward": "无人机前进!", + "backward": "无人机后退!", + "hover": "无人机悬停!", + "stop": "无人机停止!", "none": "等待指令..." } message = commands.get(command, f"未知命令: {command}") diff --git a/src/Drone_hand_gesture_project/gesture_detector.py b/src/Drone_hand_gesture_project/gesture_detector.py index bbbff52b07..1af7a3fe2b 100644 --- a/src/Drone_hand_gesture_project/gesture_detector.py +++ b/src/Drone_hand_gesture_project/gesture_detector.py @@ -23,14 +23,14 @@ def __init__(self): # 手势到控制指令的映射 self.gesture_commands = { - "open_palm": "takeoff", # 张开手掌 - 起飞 - "closed_fist": "land", # 握拳 - 降落 - "pointing_up": "up", # 食指上指 - 上升 - "pointing_down": "down", # 食指向下 - 下降 - "victory": "forward", # 胜利手势 - 前进 - "thumb_up": "backward", # 大拇指 - 后退 - "thumb_down": "stop", # 大拇指向下 - 停止 - "ok_sign": "hover" # OK手势 - 悬停 + "open_palm": "起飞", # 张开手掌 - 起飞 + "closed_fist": "降落", # 握拳 - 降落 + "pointing_up": "上升", # 食指上指 - 上升 + "pointing_down": "下降", # 食指向下 - 下降 + "victory": "前进", # 胜利手势 - 前进 + "thumb_up": "后退", # 大拇指 - 后退 + "thumb_down": "停止", # 大拇指向下 - 停止 + "ok_sign": "悬停" # OK手势 - 悬停 } def detect_gestures(self, image): @@ -73,7 +73,7 @@ def detect_gestures(self, image): cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) # 显示控制指令 - command = self.gesture_commands.get(gesture, "none") + command = self.gesture_commands.get(gesture, "无") cv2.putText(image, f"Command: {command}", (10, 110), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) diff --git a/src/Drone_hand_gesture_project/main.py b/src/Drone_hand_gesture_project/main.py index 5e23534619..7a9164e933 100644 --- a/src/Drone_hand_gesture_project/main.py +++ b/src/Drone_hand_gesture_project/main.py @@ -3,6 +3,7 @@ import time import sys import os +from PIL import Image, ImageDraw, ImageFont # 添加路径 sys.path.append(os.path.dirname(os.path.abspath(__file__))) @@ -11,35 +12,56 @@ from drone_controller import DroneController -def create_test_frame(message="Gesture Drone Control - VM Mode"): - """创建测试帧""" +def cv2_add_chinese_text(img, text, position, text_color=(0, 255, 0), text_size=30): + """ + 在OpenCV图像上添加中文文字 + """ + if isinstance(img, np.ndarray): + img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) + + draw = ImageDraw.Draw(img) + + # 尝试加载中文字体,如果失败使用默认字体 + try: + font = ImageFont.truetype("simsun.ttc", text_size, encoding="utf-8") + except: + try: + font = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", text_size, encoding="utf-8") + except: + font = ImageFont.load_default() + + draw.text(position, text, text_color, font=font) + + return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) + + +def create_test_frame(message="手势控制无人机 - 虚拟模式"): + """创建测试帧(支持中文)""" + # 创建白色背景 frame = np.ones((480, 640, 3), dtype=np.uint8) * 255 # 添加标题 - cv2.putText(frame, message, (50, 50), - cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2) + frame = cv2_add_chinese_text(frame, message, (50, 50), (0, 0, 255), 30) # 添加手势说明 gestures = [ - "Gesture Commands:", - "Open Palm - Takeoff", - "Closed Fist - Land", - "Victory - Forward", - "Thumb Up - Backward", - "Point Up - Up", - "Point Down - Down", - "OK Sign - Hover", - "Thumb Down - Stop" + "手势指令对照表:", + "张开手掌 - 起飞", + "握拳 - 降落", + "胜利手势 - 前进", + "大拇指 - 后退", + "食指上指 - 上升", + "食指向下 - 下降", + "OK手势 - 悬停", + "大拇指向下 - 停止" ] for i, text in enumerate(gestures): y_pos = 90 + i * 25 color = (0, 0, 255) if i == 0 else (0, 100, 0) - cv2.putText(frame, text, (50, y_pos), - cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1) + frame = cv2_add_chinese_text(frame, text, (50, y_pos), color, 20) - cv2.putText(frame, "Press 'q' to quit", (50, 430), - cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2) + frame = cv2_add_chinese_text(frame, "按 'q' 键退出程序", (50, 430), (0, 0, 0), 20) return frame @@ -50,14 +72,16 @@ def main(): print("=" * 60) print("程序已启动,正在尝试显示窗口...") print("如果看不到窗口,请检查虚拟机显示设置") + print("按 'q' 键退出程序") + print("按 'c' 键切换摄像头") print("=" * 60) detector = GestureDetector() controller = DroneController(simulation_mode=True) # 测试显示 - test_frame = create_test_frame("Testing Display...") - cv2.imshow('Gesture Drone - VM', test_frame) + test_frame = create_test_frame("显示测试中...") + cv2.imshow('手势控制无人机 - 虚拟机版', test_frame) cv2.waitKey(1000) # 显示1秒 # 尝试打开摄像头 @@ -85,19 +109,19 @@ def main(): if ret: frame = cv2.flip(frame, 1) else: - frame = create_test_frame("Camera Error - Virtual Mode") + frame = create_test_frame("摄像头错误 - 虚拟模式") else: # 虚拟模式 - 创建动态测试帧 if frame_count % 30 == 0: # 每30帧切换消息 messages = [ - "Virtual Camera Mode - Make gestures", - "Hand Detection Active - VM", - "Gesture Recognition Ready" + "虚拟摄像头模式 - 请做出手势", + "手势检测已激活 - 虚拟机", + "手势识别系统准备就绪" ] message = messages[(frame_count // 30) % len(messages)] frame = create_test_frame(message) else: - frame = create_test_frame("Virtual Camera Mode - Make gestures") + frame = create_test_frame("虚拟摄像头模式 - 请做出手势") # 手势检测 try: @@ -119,7 +143,7 @@ def main(): last_command_time = current_time # 显示帧 - cv2.imshow('Gesture Drone Control - VM', processed_frame) + cv2.imshow('手势控制无人机系统', processed_frame) # 退出检测 key = cv2.waitKey(1) & 0xFF @@ -139,4 +163,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file