diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/README.md b/src/AI_Drone_Face_Recognition_Human_Tracking/README.md index a04496c9aa..ccc69a5d5e 100644 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/README.md +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/README.md @@ -1,25 +1,9 @@ 该项目是一个基于人工智能的无人机控制系统,能够用YOLOv8检测图像中的人物,用OpenCV检测人脸,用DeepFace识别人,并能飞近用户从视频流中选定的目标。 -AI-Drone-Face-Tracking/ -│ -├── main.py # 主程序入口 -├── requirements.txt # 依赖包列表 -│ -├── modules/ -│ ├── drone_controller.py # 无人机控制模块 -│ ├── face_detector.py # 人脸检测模块 -│ ├── person_detector.py # 人物检测模块(YOLOv8) -│ ├── face_recognizer.py # 人脸识别模块(DeepFace) -│ ├── ui_controller.py # 用户界面控制模块 -│ └── voice_synthesizer.py # 语音合成模块 -│ -├── faces/ # 人脸数据库文件夹 -│ ├── person1/ -│ │ ├── face1.jpg -│ │ └── face2.jpg -│ └── person2/ -│ └── face1.jpg -│ -├── models/ # 模型文件夹(可选) -└── utils/ # 工具函数 - └── helpers.py \ No newline at end of file +C:\Users\20728\nn\ +├── drone_system_complete.py # 主程序(所有功能集成) +├── install_deps.py # 安装依赖脚本 +├── run.bat # Windows启动脚本 +├── requirements.txt # 依赖列表 +└── flight_logs\ # 自动创建:数据记录目录 + └── flight_20240101_120000.json # 数据记录文件 \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/__init__.py b/src/AI_Drone_Face_Recognition_Human_Tracking/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/dist/drone_system_complete.exe b/src/AI_Drone_Face_Recognition_Human_Tracking/dist/drone_system_complete.exe new file mode 100644 index 0000000000..98fcf1133e Binary files /dev/null and b/src/AI_Drone_Face_Recognition_Human_Tracking/dist/drone_system_complete.exe differ diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/drone_system_complete.py b/src/AI_Drone_Face_Recognition_Human_Tracking/drone_system_complete.py new file mode 100644 index 0000000000..28c0c0de2d --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/drone_system_complete.py @@ -0,0 +1,1436 @@ +# drone_system_complete.py +# !/usr/bin/env python3 + +print("=" * 60) +print("🚀 AI无人机人脸跟踪系统 - 完整版") +print("=" * 60) + +# ============ 第一步:应用兼容性补丁 ============ +try: + import sys + import pkgutil + + if not hasattr(pkgutil, 'ImpImporter'): + class ImpImporter: + def __init__(self, path=None): + self.path = path + + def find_module(self, fullname, path=None): + return None + + def load_module(self, fullname): + raise ImportError(f"无法加载: {fullname}") + + + pkgutil.ImpImporter = ImpImporter + + print("✅ 兼容性补丁已应用") +except Exception as e: + print(f"⚠️ 补丁应用失败: {e}") + +# ============ 第二步:导入核心模块 ============ +import pygame +import cv2 +import numpy as np +import time +import random +import json +import os +import threading +import queue + + +# ============ 第三步:创建简化模块 ============ + +class SimpleDroneController: + def __init__(self): + self.cap = None + self.init_camera() + + def init_camera(self): + """初始化摄像头""" + try: + self.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) + if self.cap.isOpened(): + self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) + self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) + print("✅ 摄像头初始化成功") + return True + else: + print("⚠️ 摄像头打开失败,使用模拟模式") + return False + except: + print("⚠️ 摄像头初始化异常,使用模拟模式") + return False + + def get_frame(self): + """获取画面""" + if self.cap and self.cap.isOpened(): + ret, frame = self.cap.read() + if ret: + return frame + + # 生成模拟画面 + return self._generate_test_frame() + + def _generate_test_frame(self): + """生成测试画面""" + frame = np.zeros((480, 640, 3), dtype=np.uint8) + + # 网格 + for i in range(0, 640, 40): + cv2.line(frame, (i, 0), (i, 480), (50, 50, 50), 1) + for i in range(0, 480, 40): + cv2.line(frame, (0, i), (640, i), (50, 50, 50), 1) + + # 中心十字 + center_x, center_y = 320, 240 + cv2.line(frame, (center_x - 20, center_y), (center_x + 20, center_y), (80, 80, 120), 2) + cv2.line(frame, (center_x, center_y - 20), (center_x, center_y + 20), (80, 80, 120), 2) + + # 添加一些模拟检测目标 + num_faces = random.randint(0, 3) + for i in range(num_faces): + x = random.randint(100, 540) + y = random.randint(100, 380) + size = random.randint(40, 80) + cv2.rectangle(frame, (x, y), (x + size, y + size), (0, 255, 0), 2) + cv2.putText(frame, "Face", (x, y - 5), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) + + num_persons = random.randint(0, 4) + for i in range(num_persons): + x = random.randint(50, 590) + y = random.randint(50, 430) + width = random.randint(40, 100) + height = random.randint(80, 160) + cv2.rectangle(frame, (x, y), (x + width, y + height), (255, 0, 0), 2) + cv2.putText(frame, f"Person{i + 1}", (x, y - 5), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1) + + # 文字 + cv2.putText(frame, "AI无人机跟踪系统", (20, 40), + cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) + cv2.putText(frame, "按T起飞,按Y切换追踪", (20, 80), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 200, 100), 1) + cv2.putText(frame, "WASD移动,空格/Ctrl升降", (20, 110), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (150, 200, 255), 1) + + return frame + + def disconnect(self): + """断开连接""" + if self.cap: + self.cap.release() + print("✅ 摄像头已释放") + + +class UIController: + def __init__(self): + # 初始化Pygame + pygame.init() + + # 窗口配置 + self.screen_width = 1000 + self.screen_height = 750 + + # 创建窗口 + try: + self.screen = pygame.display.set_mode( + (self.screen_width, self.screen_height), + pygame.HWSURFACE | pygame.DOUBLEBUF + ) + except: + self.screen = pygame.display.set_mode((self.screen_width, self.screen_height)) + + pygame.display.set_caption("AI无人机人脸跟踪系统 - 全功能版") + self.clock = pygame.time.Clock() + + # 字体 + self.font_small = pygame.font.SysFont(None, 18) + self.font_normal = pygame.font.SysFont(None, 22) + self.font_large = pygame.font.SysFont(None, 28) + self.font_title = pygame.font.SysFont(None, 32, bold=True) + + # 颜色 + self.COLORS = { + 'bg': (15, 20, 30), + 'panel_bg': (25, 30, 40), + 'panel_border': (60, 80, 100), + 'text': (220, 230, 240), + 'text_secondary': (160, 180, 200), + 'success': (0, 200, 100), + 'warning': (255, 180, 0), + 'error': (255, 80, 80), + 'drone': (0, 180, 255), + 'detection': (255, 100, 100), + 'tracking': (255, 200, 0), + 'camera': (100, 200, 255), + 'voice': (200, 100, 255), + 'data': (100, 255, 200), + } + + # 显示区域 + self.display_width = 640 + self.display_height = 480 + self.display_x = (self.screen_width - self.display_width) // 2 + self.display_y = 50 + + # 状态 + self.state = { + "running": True, + "drone_status": "未连接", + "drone_position": (0.0, 0.0, 1.0), + "drone_yaw": 0.0, + "detected_faces": 0, + "detected_persons": 0, + "recognized_person": "Unknown", + "fps": 0, + "camera_status": "初始化中...", + "tracking_mode": "手动", + "is_flying": False, + "detection_active": True, + "recognition_active": False, + "voice_enabled": False, + "data_logging": False, + "flight_time": 0, + "total_distance": 0.0, + "drone_3d_open": False, + } + + # 性能监控 + self.frame_count = 0 + self.last_fps_update = time.time() + self.fps_history = [] + + # 错误信息 + self.error_messages = [] + + # 飞行路径 + self.flight_path = [] + self.max_path_points = 100 + + # 语音消息队列 + self.voice_messages = [] + + # 数据记录 + self.data_records = [] + + print("✅ UI控制器初始化完成") + + def set_error(self, message, level="error"): + """设置错误信息""" + self.error_messages.append({ + 'message': message, + 'level': level, + 'time': time.time() + }) + print(f"UI {level}: {message}") + + def clear_old_errors(self): + """清除过期错误""" + current_time = time.time() + self.error_messages = [ + err for err in self.error_messages + if current_time - err['time'] < 5.0 + ] + + def add_voice_message(self, message): + """添加语音消息""" + self.voice_messages.append({ + 'message': message, + 'time': time.time(), + 'played': False + }) + print(f"🗣️ 语音: {message}") + + def add_data_record(self, record_type, data): + """添加数据记录""" + record = { + 'timestamp': time.time(), + 'type': record_type, + 'data': data + } + self.data_records.append(record) + + if len(self.data_records) > 1000: + self.data_records = self.data_records[-1000:] + + def handle_events(self): + """处理事件""" + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return "quit" + elif event.type == pygame.KEYDOWN: + if event.key in (pygame.K_q, pygame.K_ESCAPE): + return "quit" + elif event.key == pygame.K_t: + return "takeoff_land" + elif event.key == pygame.K_y: + return "toggle_tracking" + elif event.key == pygame.K_r: + return "reset_position" + elif event.key == pygame.K_f: + return "toggle_fullscreen" + elif event.key == pygame.K_g: + return "toggle_3d_window" + elif event.key == pygame.K_v: + return "toggle_voice" + elif event.key == pygame.K_d: + return "toggle_data_logging" + elif event.key == pygame.K_s: + return "save_data" + elif event.key == pygame.K_p: + return "playback_data" + elif event.key == pygame.K_h: + return "hover_mode" + elif event.key == pygame.K_l: + return "land" + elif event.key == pygame.K_1: + return "mode_manual" + elif event.key == pygame.K_2: + return "mode_tracking" + elif event.key == pygame.K_3: + return "mode_hover" + elif event.key == pygame.K_4: + return "toggle_detection" + elif event.key == pygame.K_5: + return "toggle_recognition" + + return None + + def update_frame_display(self, frame): + """更新画面显示""" + if frame is None or frame.size == 0: + self._draw_no_frame() + return + + try: + # 检查帧 + if len(frame.shape) < 2: + self.set_error("无效帧格式", "warning") + self._draw_no_frame() + return + + # 转换颜色 + if len(frame.shape) == 3: + if frame.shape[2] == 3: + frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + else: + frame_rgb = frame + else: + frame_rgb = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB) + + # 缩放 + try: + frame_resized = cv2.resize(frame_rgb, (self.display_width, self.display_height)) + except: + self.set_error("缩放失败", "error") + self._draw_no_frame() + return + + # 转换为Surface + try: + frame_surface = pygame.surfarray.make_surface( + np.transpose(frame_resized, (1, 0, 2)) + ) + except: + try: + frame_bytes = frame_resized.tobytes() + frame_surface = pygame.image.frombuffer( + frame_bytes, + (self.display_width, self.display_height), + 'RGB' + ) + except: + self.set_error("Surface创建失败", "error") + self._draw_no_frame() + return + + # 绘制背景和边框 + pygame.draw.rect( + self.screen, + (10, 15, 25), + (self.display_x - 10, self.display_y - 10, + self.display_width + 20, self.display_height + 20), + 0, 8 + ) + + # 边框颜色 + border_color = self.COLORS['camera'] + if self.state['tracking_mode'] == "追踪": + import math + current_time = time.time() + pulse = (math.sin(current_time * 3) + 1) / 2 + border_color = ( + int(255 * pulse), + int(200 * pulse), + 0 + ) + + pygame.draw.rect( + self.screen, + border_color, + (self.display_x - 5, self.display_y - 5, + self.display_width + 10, self.display_height + 10), + 3, 10 + ) + + # 显示画面 + self.screen.blit(frame_surface, (self.display_x, self.display_y)) + + # 画面信息 + h, w = frame.shape[:2] + info_text = f"画面: {w}x{h}" + info_surf = self.font_small.render(info_text, True, (180, 180, 220)) + self.screen.blit(info_surf, (self.display_x, self.display_y + self.display_height + 5)) + + except Exception as e: + self.set_error(f"画面显示异常: {str(e)[:30]}", "error") + self._draw_no_frame() + + def _draw_no_frame(self): + """无画面显示""" + pygame.draw.rect( + self.screen, + (5, 10, 15), + (self.display_x, self.display_y, self.display_width, self.display_height), + 0, 8 + ) + + # 网格 + grid_size = 40 + grid_color = (40, 50, 70) + for x in range(self.display_x, self.display_x + self.display_width, grid_size): + pygame.draw.line( + self.screen, grid_color, + (x, self.display_y), (x, self.display_y + self.display_height), 1 + ) + for y in range(self.display_y, self.display_y + self.display_height, grid_size): + pygame.draw.line( + self.screen, grid_color, + (self.display_x, y), (self.display_x + self.display_width, y), 1 + ) + + # 中心十字 + center_x = self.display_x + self.display_width // 2 + center_y = self.display_y + self.display_height // 2 + pygame.draw.line( + self.screen, (80, 90, 120), + (center_x - 20, center_y), (center_x + 20, center_y), 2 + ) + pygame.draw.line( + self.screen, (80, 90, 120), + (center_x, center_y - 20), (center_x, center_y + 20), 2 + ) + + # 文本 + if self.error_messages: + error = self.error_messages[-1] + error_text = error['message'] + error_color = self.COLORS[error['level']] + error_lines = self._wrap_text(error_text, 50) + for i, line in enumerate(error_lines): + error_surf = self.font_normal.render(line, True, error_color) + error_rect = error_surf.get_rect(center=(center_x, center_y - 30 + i * 25)) + self.screen.blit(error_surf, error_rect) + tip_text = "按R重试,检查摄像头" + else: + wait_surf = self.font_large.render("等待摄像头画面...", True, (255, 255, 0)) + wait_rect = wait_surf.get_rect(center=(center_x, center_y - 30)) + self.screen.blit(wait_surf, wait_rect) + tip_text = "请检查摄像头连接" + + tip_surf = self.font_small.render(tip_text, True, (150, 150, 150)) + tip_rect = tip_surf.get_rect(center=(center_x, center_y + 30)) + self.screen.blit(tip_surf, tip_rect) + + def _wrap_text(self, text, max_width): + """文本换行""" + words = text.split(' ') + lines = [] + current_line = [] + for word in words: + current_line.append(word) + test_line = ' '.join(current_line) + if len(test_line) > max_width: + if len(current_line) > 1: + lines.append(' '.join(current_line[:-1])) + current_line = [current_line[-1]] + else: + lines.append(test_line) + current_line = [] + if current_line: + lines.append(' '.join(current_line)) + return lines + + def _draw_status_panel(self): + """绘制状态面板""" + panel_x = 20 + panel_y = self.display_y + self.display_height + 20 + panel_width = self.screen_width - 40 + panel_height = 160 + + # 背景 + pygame.draw.rect( + self.screen, + self.COLORS['panel_bg'], + (panel_x, panel_y, panel_width, panel_height), + 0, 10 + ) + + # 边框 + border_color = self.COLORS['panel_border'] + if self.state['is_flying']: + import math + current_time = time.time() + pulse = (math.sin(current_time * 3) + 1) / 2 + border_color = ( + int(border_color[0] * (0.7 + 0.3 * pulse)), + int(border_color[1] * (0.7 + 0.3 * pulse)), + int(border_color[2] * (0.7 + 0.3 * pulse)) + ) + + pygame.draw.rect( + self.screen, + border_color, + (panel_x, panel_y, panel_width, panel_height), + 2, 10 + ) + + # 三列显示 + col_width = panel_width // 3 - 20 + + # 第一列:无人机状态 + left_col_x = panel_x + 15 + drone_status = [ + ("无人机状态:", self.state['drone_status'], + self.COLORS['success'] if "已连接" in self.state['drone_status'] else self.COLORS['warning']), + ("飞行状态:", "飞行中" if self.state['is_flying'] else "地面", + self.COLORS['drone'] if self.state['is_flying'] else self.COLORS['text_secondary']), + ("控制模式:", self.state['tracking_mode'], + self.COLORS['tracking'] if self.state['tracking_mode'] == "追踪" else self.COLORS['text']), + ("3D窗口:", "开启" if self.state['drone_3d_open'] else "关闭", + self.COLORS['success'] if self.state['drone_3d_open'] else self.COLORS['text_secondary']), + ] + + line_height = 22 + for i, (label, value, color) in enumerate(drone_status): + label_surf = self.font_small.render(label, True, self.COLORS['text_secondary']) + self.screen.blit(label_surf, (left_col_x, panel_y + 15 + i * line_height)) + value_surf = self.font_normal.render(value, True, color) + self.screen.blit(value_surf, (left_col_x + 80, panel_y + 15 + i * line_height)) + + # 第二列:检测状态 + middle_col_x = panel_x + col_width + 25 + detection_status = [ + ("检测状态:", "活跃" if self.state['detection_active'] else "暂停", + self.COLORS['success'] if self.state['detection_active'] else self.COLORS['warning']), + ("人脸检测:", f"{self.state['detected_faces']} 个", + self.COLORS['detection'] if self.state['detected_faces'] > 0 else self.COLORS['text_secondary']), + ("行人检测:", f"{self.state['detected_persons']} 个", + self.COLORS['detection'] if self.state['detected_persons'] > 0 else self.COLORS['text_secondary']), + ("识别结果:", self.state['recognized_person'], + self.COLORS['success'] if self.state['recognized_person'] != "Unknown" else self.COLORS['text_secondary']), + ] + + for i, (label, value, color) in enumerate(detection_status): + label_surf = self.font_small.render(label, True, self.COLORS['text_secondary']) + self.screen.blit(label_surf, (middle_col_x, panel_y + 15 + i * line_height)) + value_surf = self.font_normal.render(value, True, color) + self.screen.blit(value_surf, (middle_col_x + 80, panel_y + 15 + i * line_height)) + + # 第三列:系统功能 + right_col_x = panel_x + 2 * col_width + 35 + system_status = [ + ("语音播报:", "启用" if self.state['voice_enabled'] else "禁用", + self.COLORS['voice'] if self.state['voice_enabled'] else self.COLORS['text_secondary']), + ("数据记录:", "进行中" if self.state['data_logging'] else "停止", + self.COLORS['data'] if self.state['data_logging'] else self.COLORS['text_secondary']), + ("飞行时间:", f"{self.state['flight_time']:.0f}秒", self.COLORS['text']), + ("飞行距离:", f"{self.state['total_distance']:.1f}米", self.COLORS['text']), + ] + + for i, (label, value, color) in enumerate(system_status): + label_surf = self.font_small.render(label, True, self.COLORS['text_secondary']) + self.screen.blit(label_surf, (right_col_x, panel_y + 15 + i * line_height)) + value_surf = self.font_normal.render(value, True, color) + self.screen.blit(value_surf, (right_col_x + 80, panel_y + 15 + i * line_height)) + + def _draw_control_panel(self): + """绘制控制面板""" + panel_x = 20 + panel_y = self.screen_height - 220 + panel_width = self.screen_width - 40 + panel_height = 200 + + # 背景 + pygame.draw.rect( + self.screen, + (30, 35, 45), + (panel_x, panel_y, panel_width, panel_height), + 0, 10 + ) + + # 标题 + title_surf = self.font_large.render("控制面板", True, self.COLORS['text']) + self.screen.blit(title_surf, (panel_x + 15, panel_y + 10)) + + # 分隔线 + pygame.draw.line( + self.screen, self.COLORS['panel_border'], + (panel_x + 10, panel_y + 40), (panel_x + panel_width - 10, panel_y + 40), 1 + ) + + # 控制说明(三列) + col_width = panel_width // 3 - 20 + + # 第一列:基本控制 + basic_controls = [ + ("T", "起飞/降落"), + ("Y", "追踪开关"), + ("R", "重置位置"), + ("H", "悬停模式"), + ("L", "安全降落"), + ] + + for i, (key, desc) in enumerate(basic_controls): + key_surf = self.font_normal.render(key, True, self.COLORS['drone']) + desc_surf = self.font_small.render(desc, True, self.COLORS['text_secondary']) + x, y = panel_x + 20, panel_y + 55 + i * 25 + self.screen.blit(key_surf, (x, y)) + self.screen.blit(desc_surf, (x + 30, y + 2)) + + # 第二列:飞行控制 + flight_controls = [ + ("W/S", "前进/后退"), + ("A/D", "左移/右移"), + ("空格/Ctrl", "上升/下降"), + ("Shift+Q/E", "左转/右转"), + ] + + for i, (key, desc) in enumerate(flight_controls): + key_surf = self.font_normal.render(key, True, self.COLORS['drone']) + desc_surf = self.font_small.render(desc, True, self.COLORS['text_secondary']) + x, y = panel_x + col_width + 40, panel_y + 55 + i * 25 + self.screen.blit(key_surf, (x, y)) + self.screen.blit(desc_surf, (x + 60, y + 2)) + + # 第三列:系统控制 + system_controls = [ + ("1/2/3", "手动/追踪/悬停"), + ("4/5", "检测/识别开关"), + ("G/V/D", "3D/语音/数据"), + ("S/P", "保存/回放"), + ("Q/ESC", "退出程序"), + ] + + for i, (key, desc) in enumerate(system_controls): + key_surf = self.font_normal.render(key, True, self.COLORS['text']) + desc_surf = self.font_small.render(desc, True, self.COLORS['text_secondary']) + x, y = panel_x + 2 * col_width + 60, panel_y + 55 + i * 25 + self.screen.blit(key_surf, (x, y)) + self.screen.blit(desc_surf, (x + 50, y + 2)) + + def _draw_system_info(self): + """绘制系统信息""" + info_x = self.screen_width - 220 + info_y = 20 + + # 背景 + pygame.draw.rect( + self.screen, + (30, 35, 45, 200), + (info_x - 10, info_y - 10, 210, 120), + 0, 5 + ) + + # FPS + fps_color = self.COLORS['success'] if self.state['fps'] >= 25 else \ + self.COLORS['warning'] if self.state['fps'] >= 15 else \ + self.COLORS['error'] + fps_surf = self.font_normal.render(f"FPS: {self.state['fps']}", True, fps_color) + self.screen.blit(fps_surf, (info_x, info_y)) + + # 3D窗口状态 + drone_color = self.COLORS['success'] if self.state['drone_3d_open'] else self.COLORS['text_secondary'] + drone_surf = self.font_small.render(f"3D窗口: {'开启' if self.state['drone_3d_open'] else '关闭'}", True, + drone_color) + self.screen.blit(drone_surf, (info_x, info_y + 25)) + + # 语音状态 + voice_color = self.COLORS['voice'] if self.state['voice_enabled'] else self.COLORS['text_secondary'] + voice_surf = self.font_small.render(f"语音: {'启用' if self.state['voice_enabled'] else '禁用'}", True, + voice_color) + self.screen.blit(voice_surf, (info_x, info_y + 45)) + + # 数据记录状态 + data_color = self.COLORS['data'] if self.state['data_logging'] else self.COLORS['text_secondary'] + data_surf = self.font_small.render(f"数据: {'记录中' if self.state['data_logging'] else '停止'}", True, + data_color) + self.screen.blit(data_surf, (info_x, info_y + 65)) + + def _draw_voice_queue(self): + """绘制语音消息队列""" + if not self.voice_messages: + return + + queue_x = 20 + queue_y = self.screen_height - 60 + + # 显示最新的语音消息 + latest_msg = self.voice_messages[-1] + if time.time() - latest_msg['time'] < 3: # 显示最近3秒的消息 + msg_text = f"🗣️ {latest_msg['message']}" + msg_surf = self.font_small.render(msg_text, True, self.COLORS['voice']) + self.screen.blit(msg_surf, (queue_x, queue_y)) + + def update_fps(self): + """更新FPS""" + current_time = time.time() + self.frame_count += 1 + + if current_time - self.last_fps_update > 0.5: + elapsed = current_time - self.last_fps_update + current_fps = self.frame_count / elapsed + + self.fps_history.append(current_fps) + if len(self.fps_history) > 10: + self.fps_history = self.fps_history[-10:] + + self.state["fps"] = int(np.mean(self.fps_history)) if self.fps_history else 0 + self.frame_count = 0 + self.last_fps_update = current_time + + def update_drone_state(self, state): + """更新无人机状态""" + old_position = self.state['drone_position'] + self.state.update(state) + + # 计算飞行距离 + if self.state['is_flying']: + new_position = self.state['drone_position'] + import math + dx = new_position[0] - old_position[0] + dy = new_position[1] - old_position[1] + dz = new_position[2] - old_position[2] + distance = math.sqrt(dx * dx + dy * dy + dz * dz) + self.state['total_distance'] += distance + + # 更新飞行路径 + self.flight_path.append(self.state['drone_position']) + if len(self.flight_path) > self.max_path_points: + self.flight_path = self.flight_path[-self.max_path_points:] + + def update_lightweight(self, frame): + """更新UI""" + try: + # 清除过期错误 + self.clear_old_errors() + + # 填充背景 + self.screen.fill(self.COLORS['bg']) + + # 更新画面显示 + self.update_frame_display(frame) + + # 更新状态面板 + self._draw_status_panel() + + # 绘制控制面板 + self._draw_control_panel() + + # 绘制系统信息 + self._draw_system_info() + + # 绘制语音队列 + self._draw_voice_queue() + + # 更新FPS + self.update_fps() + + # 更新显示 + pygame.display.flip() + + except Exception as e: + self.set_error(f"UI更新异常: {e}", "error") + + def update_empty(self): + """无画面更新""" + self.screen.fill(self.COLORS['bg']) + + center_x = self.screen_width // 2 + center_y = self.screen_height // 2 + + # 标题 + title_surf = self.font_title.render("AI无人机跟踪系统", True, self.COLORS['text']) + title_rect = title_surf.get_rect(center=(center_x, center_y - 100)) + self.screen.blit(title_surf, title_rect) + + # 状态 + status_text = f"系统初始化中... FPS: {self.state['fps']}" + status_surf = self.font_large.render(status_text, True, self.COLORS['warning']) + status_rect = status_surf.get_rect(center=(center_x, center_y - 40)) + self.screen.blit(status_surf, status_rect) + + # 提示 + hint_text = "按任意键继续,按Q退出" + hint_surf = self.font_small.render(hint_text, True, (150, 170, 200)) + hint_rect = hint_surf.get_rect(center=(center_x, center_y + 150)) + self.screen.blit(hint_surf, hint_rect) + + pygame.display.flip() + + def save_data_records(self, filename=None): + """保存数据记录""" + if not filename: + import datetime + filename = f"flight_data_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.json" + + try: + import json + with open(filename, 'w', encoding='utf-8') as f: + json.dump(self.data_records, f, ensure_ascii=False, indent=2) + print(f"✅ 数据已保存到: {filename}") + return True + except Exception as e: + print(f"❌ 数据保存失败: {e}") + return False + + def quit(self): + """退出""" + print("🔄 关闭UI...") + try: + pygame.quit() + print("✅ UI已关闭") + except: + pass + + +class VoiceSynthesizer: + """语音合成器""" + + def __init__(self, enabled=True): + self.enabled = enabled + self.message_queue = queue.Queue() + self.currently_speaking = False + self.running = False + self.worker_thread = None + + if self.enabled: + self.start() + + def speak(self, text): + """语音播报""" + if not self.enabled or not text: + return False + + try: + self.message_queue.put(text) + return True + except: + return False + + def _voice_worker(self): + """语音工作线程""" + while self.running: + try: + # 从队列获取消息 + text = self.message_queue.get(timeout=1) + + # 模拟语音播报(实际应用中可以使用pyttsx3) + print(f"🗣️ 语音播报: {text}") + + # 标记任务完成 + self.message_queue.task_done() + + # 短暂暂停 + time.sleep(0.5) + + except queue.Empty: + continue + except Exception as e: + print(f"语音播报错误: {e}") + time.sleep(1) + + def start(self): + """启动语音服务""" + if self.running: + return + + self.running = True + self.worker_thread = threading.Thread(target=self._voice_worker, daemon=True) + self.worker_thread.start() + print("✅ 语音服务已启动") + + def stop(self): + """停止语音服务""" + self.running = False + if self.worker_thread: + self.worker_thread.join(timeout=2) + print("✅ 语音服务已停止") + + +class DataLogger: + """数据记录器""" + + def __init__(self, enabled=True): + self.enabled = enabled + self.records = [] + self.max_records = 1000 + + # 创建日志目录 + self.log_dir = "flight_logs" + if not os.path.exists(self.log_dir): + os.makedirs(self.log_dir) + + print("✅ 数据记录器初始化完成") + + def log_drone_state(self, position, yaw, is_flying, mode): + """记录无人机状态""" + if not self.enabled: + return + + record = { + 'timestamp': time.time(), + 'type': 'drone_state', + 'position': position, + 'yaw': yaw, + 'is_flying': is_flying, + 'mode': mode + } + self.records.append(record) + + # 保持记录数量 + if len(self.records) > self.max_records: + self.records = self.records[-self.max_records:] + + def log_detection_result(self, face_count, person_count, recognized_person): + """记录检测结果""" + if not self.enabled: + return + + record = { + 'timestamp': time.time(), + 'type': 'detection', + 'face_count': face_count, + 'person_count': person_count, + 'recognized_person': recognized_person + } + self.records.append(record) + + def log_control_action(self, action, params=None): + """记录控制动作""" + if not self.enabled: + return + + record = { + 'timestamp': time.time(), + 'type': 'control', + 'action': action, + 'params': params or {} + } + self.records.append(record) + + def save_to_file(self, filename=None): + """保存数据到文件""" + if not self.records: + return False + + try: + if not filename: + import datetime + timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + filename = os.path.join(self.log_dir, f"flight_{timestamp}.json") + + # 添加文件头信息 + data = { + 'metadata': { + 'created_at': time.time(), + 'total_records': len(self.records) + }, + 'records': self.records + } + + with open(filename, 'w', encoding='utf-8') as f: + json.dump(data, f, ensure_ascii=False, indent=2) + + print(f"✅ 已保存 {len(self.records)} 条记录到: {filename}") + return True + + except Exception as e: + print(f"❌ 保存数据失败: {e}") + return False + + def get_statistics(self): + """获取统计信息""" + if not self.records: + return {} + + stats = { + 'total_records': len(self.records), + 'flight_time': 0, + 'drone_states': 0, + 'detections': 0, + 'controls': 0 + } + + for record in self.records: + if record['type'] == 'drone_state': + stats['drone_states'] += 1 + if record['is_flying']: + stats['flight_time'] += 1 # 简化的飞行时间 + elif record['type'] == 'detection': + stats['detections'] += 1 + elif record['type'] == 'control': + stats['controls'] += 1 + + return stats + + +class Drone3DViewer: + """3D无人机查看器""" + + def __init__(self): + self.window_open = False + self.running = False + self.viewer_thread = None + + print("✅ 3D查看器初始化完成") + + def start_viewer(self): + """启动3D查看器""" + if self.window_open: + return True + + try: + self.running = True + self.viewer_thread = threading.Thread(target=self._run_viewer, daemon=True) + self.viewer_thread.start() + + # 等待窗口初始化 + time.sleep(1) + self.window_open = True + + print("✅ 3D无人机查看器已启动") + return True + + except Exception as e: + print(f"❌ 启动3D查看器失败: {e}") + self.window_open = False + return False + + def stop_viewer(self): + """停止3D查看器""" + self.running = False + if self.viewer_thread: + self.viewer_thread.join(timeout=2) + self.window_open = False + print("✅ 3D无人机查看器已停止") + + def _run_viewer(self): + """运行3D查看器(模拟)""" + try: + print("🖥️ 3D窗口: 正在运行模拟3D视图...") + + while self.running: + # 模拟3D视图更新 + time.sleep(0.1) + + except Exception as e: + print(f"3D查看器错误: {e}") + + +# ============ 第四步:主程序 ============ +def main(): + print("\n🎬 初始化系统...") + + # 初始化组件 + try: + drone = SimpleDroneController() + print("✅ 无人机控制器") + except Exception as e: + print(f"❌ 无人机控制器失败: {e}") + return + + try: + ui = UIController() + print("✅ UI控制器") + except Exception as e: + print(f"❌ UI控制器失败: {e}") + return + + # 初始化3D查看器 + drone_3d = Drone3DViewer() + + # 初始化语音合成器 + voice = VoiceSynthesizer(enabled=True) + ui.state['voice_enabled'] = True + + # 初始化数据记录器 + data_logger = DataLogger(enabled=True) + ui.state['data_logging'] = True + + print("✅ 所有组件初始化完成") + + # 运行参数 + frame_count = 0 + is_flying = False + tracking_mode = "手动" + detection_active = True + recognition_active = False + + # 无人机状态 + drone_position = [0.0, 0.0, 1.0] # x, y, z + drone_yaw = 0.0 + + # 模拟检测结果 + detected_faces = 0 + detected_persons = 0 + recognized_person = "Unknown" + + # 飞行时间统计 + flight_start_time = 0 + total_flight_time = 0 + + print("\n🎬 开始主循环...") + print("控制说明:") + print(" T - 起飞/降落") + print(" Y - 切换追踪模式") + print(" R - 重置位置") + print(" WASD - 移动控制") + print(" 空格/Ctrl - 上升/下降") + print(" Shift+Q/E - 左转/右转") + print(" G - 开关3D窗口") + print(" V - 开关语音") + print(" D - 开关数据记录") + print(" S - 保存数据") + print(" P - 回放数据") + print(" Q/ESC - 退出程序") + print("=" * 50) + + # 语音播报欢迎信息 + voice.speak("无人机系统启动完成") + ui.add_voice_message("系统启动完成") + + try: + while ui.state["running"]: + frame_count += 1 + + # 处理事件 + event = ui.handle_events() + if event == "quit": + print("用户请求退出") + break + elif event == "takeoff_land": + is_flying = not is_flying + status = "起飞" if is_flying else "降落" + print(f"无人机: {status}") + + # 语音播报 + voice.speak(f"无人机{status}") + ui.add_voice_message(f"无人机{status}") + + # 数据记录 + data_logger.log_control_action("takeoff_land" if is_flying else "land", {"status": status}) + + # 更新飞行时间 + if is_flying: + flight_start_time = time.time() + else: + if flight_start_time > 0: + total_flight_time += time.time() - flight_start_time + flight_start_time = 0 + + drone_position[2] = 1.5 if is_flying else 0.5 + + elif event == "toggle_tracking": + tracking_mode = "追踪" if tracking_mode == "手动" else "手动" + print(f"追踪模式: {tracking_mode}") + + # 语音播报 + voice.speak(f"切换到{tracking_mode}模式") + ui.add_voice_message(f"切换到{tracking_mode}模式") + + # 数据记录 + data_logger.log_control_action("toggle_tracking", {"mode": tracking_mode}) + + elif event == "reset_position": + drone_position = [0.0, 0.0, 1.0] + drone_yaw = 0.0 + print("位置已重置") + + # 语音播报 + voice.speak("位置已重置") + ui.add_voice_message("位置已重置") + + # 数据记录 + data_logger.log_control_action("reset_position") + + elif event == "toggle_3d_window": + if drone_3d.window_open: + drone_3d.stop_viewer() + ui.state['drone_3d_open'] = False + voice.speak("3D窗口已关闭") + ui.add_voice_message("3D窗口已关闭") + else: + if drone_3d.start_viewer(): + ui.state['drone_3d_open'] = True + voice.speak("3D窗口已开启") + ui.add_voice_message("3D窗口已开启") + + elif event == "toggle_voice": + voice.enabled = not voice.enabled + ui.state['voice_enabled'] = voice.enabled + status = "启用" if voice.enabled else "禁用" + print(f"语音: {status}") + ui.add_voice_message(f"语音播报{status}") + + elif event == "toggle_data_logging": + data_logger.enabled = not data_logger.enabled + ui.state['data_logging'] = data_logger.enabled + status = "启动" if data_logger.enabled else "停止" + print(f"数据记录: {status}") + voice.speak(f"数据记录{status}") + ui.add_voice_message(f"数据记录{status}") + + elif event == "save_data": + if data_logger.records: + if data_logger.save_to_file(): + voice.speak("数据保存成功") + ui.add_voice_message("数据保存成功") + else: + voice.speak("数据保存失败") + ui.add_voice_message("数据保存失败") + + elif event == "playback_data": + if data_logger.records: + print("开始回放数据...") + voice.speak("开始回放数据") + ui.add_voice_message("开始回放数据") + # 这里可以添加数据回放逻辑 + + elif event == "mode_manual": + tracking_mode = "手动" + print("模式: 手动控制") + elif event == "mode_tracking": + tracking_mode = "追踪" + print("模式: 自动追踪") + elif event == "mode_hover": + print("模式: 悬停") + elif event == "toggle_detection": + detection_active = not detection_active + status = "开启" if detection_active else "关闭" + print(f"检测: {status}") + voice.speak(f"检测功能{status}") + ui.add_voice_message(f"检测功能{status}") + elif event == "toggle_recognition": + recognition_active = not recognition_active + status = "开启" if recognition_active else "关闭" + print(f"识别: {status}") + voice.speak(f"识别功能{status}") + ui.add_voice_message(f"识别功能{status}") + + # 无人机移动控制 + if is_flying: + keys = pygame.key.get_pressed() + if keys[pygame.K_w]: + drone_position[0] += 0.1 + data_logger.log_control_action("move_forward", {"distance": 0.1}) + if keys[pygame.K_s]: + drone_position[0] -= 0.1 + data_logger.log_control_action("move_backward", {"distance": 0.1}) + if keys[pygame.K_a]: + drone_position[1] -= 0.1 + data_logger.log_control_action("move_left", {"distance": 0.1}) + if keys[pygame.K_d]: + drone_position[1] += 0.1 + data_logger.log_control_action("move_right", {"distance": 0.1}) + if keys[pygame.K_SPACE]: + drone_position[2] += 0.05 + data_logger.log_control_action("move_up", {"distance": 0.05}) + if keys[pygame.K_LCTRL]: + drone_position[2] = max(0.5, drone_position[2] - 0.05) + data_logger.log_control_action("move_down", {"distance": 0.05}) + if keys[pygame.K_q] and keys[pygame.K_LSHIFT]: # Shift+Q旋转左 + drone_yaw = (drone_yaw + 2.0) % 360 + data_logger.log_control_action("rotate_left", {"angle": 2.0}) + if keys[pygame.K_e]: # E键旋转右 + drone_yaw = (drone_yaw - 2.0) % 360 + data_logger.log_control_action("rotate_right", {"angle": 2.0}) + + # 更新3D视图 + if drone_3d.window_open: + # 在实际应用中,这里会更新3D模型的位置和姿态 + pass + + # 获取画面 + frame = drone.get_frame() + + if frame is None: + ui.update_empty() + time.sleep(0.1) + continue + + # 处理画面 + result_frame = frame.copy() + h, w = frame.shape[:2] + + # 模拟检测结果更新 + if frame_count % 30 == 0: + if tracking_mode == "追踪" and detection_active: + detected_faces = random.randint(1, 3) + detected_persons = random.randint(1, 4) + if random.random() > 0.5 and recognition_active: + names = ["张三", "李四", "王五", "赵六"] + recognized_person = random.choice(names) + + # 语音播报识别结果 + if voice.enabled: + voice.speak(f"识别到{recognized_person}") + ui.add_voice_message(f"识别到{recognized_person}") + else: + recognized_person = "Unknown" + else: + detected_faces = random.randint(0, 2) + detected_persons = random.randint(0, 3) + recognized_person = "Unknown" + + # 语音播报检测结果 + if voice.enabled and detection_active and (detected_faces > 0 or detected_persons > 0): + voice.speak(f"检测到{detected_faces}个人脸,{detected_persons}个行人") + ui.add_voice_message(f"检测到{detected_faces}个人脸,{detected_persons}个行人") + + # 数据记录 + if data_logger.enabled and detection_active: + data_logger.log_detection_result(detected_faces, detected_persons, recognized_person) + + # 在画面上添加信息 + if tracking_mode == "追踪": + cv2.putText(result_frame, "追踪中", (w - 100, 30), + cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2) + + info_y = 30 + if detected_faces > 0: + cv2.putText(result_frame, f"人脸: {detected_faces}", (10, info_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) + info_y += 30 + + if detected_persons > 0: + cv2.putText(result_frame, f"行人: {detected_persons}", (10, info_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2) + info_y += 30 + + if recognized_person != "Unknown": + cv2.putText(result_frame, f"识别: {recognized_person}", (10, info_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2) + + # 添加功能状态 + status_y = h - 80 + if drone_3d.window_open: + cv2.putText(result_frame, "3D窗口: 开启", (w - 150, status_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 180, 255), 1) + status_y += 20 + + if voice.enabled: + cv2.putText(result_frame, "语音: 启用", (w - 150, status_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 100, 255), 1) + status_y += 20 + + if data_logger.enabled: + cv2.putText(result_frame, "数据: 记录中", (w - 150, status_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (100, 255, 200), 1) + + # 添加帧编号 + cv2.putText(result_frame, f"帧: {frame_count}", (w - 100, h - 20), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (200, 200, 200), 1) + + # 更新无人机位置(模拟漂移) + if is_flying: + drone_position[0] += (random.random() - 0.5) * 0.02 + drone_position[1] += (random.random() - 0.5) * 0.02 + drone_yaw = (drone_yaw + 0.2) % 360 + + # 更新飞行时间 + if flight_start_time > 0: + ui.state['flight_time'] = total_flight_time + (time.time() - flight_start_time) + else: + ui.state['flight_time'] = total_flight_time + + # 数据记录:无人机状态 + if data_logger.enabled: + data_logger.log_drone_state(tuple(drone_position), drone_yaw, is_flying, tracking_mode) + + # 更新UI状态 + ui.update_drone_state({ + 'drone_status': '已连接', + 'is_flying': is_flying, + 'tracking_mode': tracking_mode, + 'drone_position': tuple(drone_position), + 'drone_yaw': drone_yaw, + 'detected_faces': detected_faces, + 'detected_persons': detected_persons, + 'recognized_person': recognized_person, + 'camera_status': f"{w}x{h} @ 30fps", + 'detection_active': detection_active, + 'recognition_active': recognition_active, + 'flight_time': ui.state.get('flight_time', 0), + 'total_distance': ui.state.get('total_distance', 0), + 'drone_3d_open': drone_3d.window_open, + }) + + # 更新UI + ui.update_lightweight(result_frame) + + # 每100帧打印状态 + if frame_count % 100 == 0: + print(f"运行中... 帧数: {frame_count}, FPS: {ui.state['fps']}") + if detection_active: + print(f" 检测: {detected_faces}人脸, {detected_persons}行人") + print(f" 位置: ({drone_position[0]:.1f}, {drone_position[1]:.1f}, {drone_position[2]:.1f})") + + # 显示数据记录统计 + if data_logger.enabled: + stats = data_logger.get_statistics() + print(f" 数据: {stats['total_records']}条记录") + + except KeyboardInterrupt: + print("\n🛑 用户中断") + except Exception as e: + print(f"\n❌ 程序错误: {e}") + import traceback + traceback.print_exc() + finally: + # 清理资源 + print("\n🔄 清理资源...") + + # 停止3D查看器 + drone_3d.stop_viewer() + + # 停止语音 + voice.stop() + + # 停止数据记录并保存 + if data_logger.records: + data_logger.save_to_file() + + # 显示统计信息 + stats = data_logger.get_statistics() + print(f"📊 飞行数据统计:") + print(f" 总记录数: {stats['total_records']}") + print(f" 飞行时间: {stats['flight_time']}秒") + print(f" 无人机状态记录: {stats['drone_states']}") + print(f" 检测记录: {stats['detections']}") + print(f" 控制记录: {stats['controls']}") + + # 断开连接 + drone.disconnect() + ui.quit() + cv2.destroyAllWindows() + + print("\n✅ 程序已安全退出") + print("=" * 60) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/install_deps.py b/src/AI_Drone_Face_Recognition_Human_Tracking/install_deps.py new file mode 100644 index 0000000000..aa7bc09b7f --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/install_deps.py @@ -0,0 +1,34 @@ +# install_deps.py +# !/usr/bin/env python3 + +import subprocess +import sys + + +def install_packages(): + """安装所需依赖包""" + print("正在安装AI无人机系统所需依赖...") + print("=" * 50) + + packages = [ + "pygame", + "opencv-python", + "numpy", + ] + + for package in packages: + try: + print(f"安装 {package}...") + subprocess.check_call([sys.executable, "-m", "pip", "install", package]) + print(f"✅ {package} 安装成功") + except subprocess.CalledProcessError as e: + print(f"⚠️ {package} 安装失败: {e}") + + print("\n" + "=" * 50) + print("✅ 所有依赖安装完成!") + print("现在可以运行: python drone_system_complete.py") + print("=" * 50) + + +if __name__ == "__main__": + install_packages() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/main_complete.py b/src/AI_Drone_Face_Recognition_Human_Tracking/main_complete.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/main_final.py b/src/AI_Drone_Face_Recognition_Human_Tracking/main_final.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/main_fixed.py b/src/AI_Drone_Face_Recognition_Human_Tracking/main_fixed.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/minimal_working.py b/src/AI_Drone_Face_Recognition_Human_Tracking/minimal_working.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/__init__.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/__init__.py new file mode 100644 index 0000000000..92469ffb9c --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/__init__.py @@ -0,0 +1,20 @@ +# modules/__init__.py +""" +AI无人机人脸跟踪系统模块包 +""" + +from .drone_controller import DroneController +from .face_detector import FaceDetector +from .person_detector import PersonDetector +from .face_recognizer import FaceRecognizer +from .ui_controller import UIController +from .voice_synthesizer import VoiceSynthesizer + +__all__ = [ + 'DroneController', + 'FaceDetector', + 'PersonDetector', + 'FaceRecognizer', + 'UIController', + 'VoiceSynthesizer' +] \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/camera_fix.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/camera_fix.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/config.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/config.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/data_logger.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/data_logger.py new file mode 100644 index 0000000000..2ddebd0669 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/data_logger.py @@ -0,0 +1,333 @@ +# modules/data_logger.py +import json +import time +import datetime +import os +import threading +from collections import deque + + +class DataLogger: + def __init__(self, enabled=True, max_records=1000, auto_save_interval=60): + self.enabled = enabled + self.max_records = max_records + self.auto_save_interval = auto_save_interval + self.records = deque(maxlen=max_records) + self.last_save_time = time.time() + self.log_file = None + self.running = False + self.save_thread = None + + # 创建日志目录 + self.log_dir = "flight_logs" + if not os.path.exists(self.log_dir): + os.makedirs(self.log_dir) + + if self.enabled: + self.start() + + def start(self): + """启动数据记录""" + if self.running: + return + + self.running = True + + # 创建新的日志文件 + timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + self.log_file = os.path.join(self.log_dir, f"flight_{timestamp}.json") + + # 启动自动保存线程 + self.save_thread = threading.Thread(target=self._auto_save_worker, daemon=True) + self.save_thread.start() + + print(f"✅ 数据记录已启动,日志文件: {self.log_file}") + + def stop(self): + """停止数据记录""" + self.running = False + if self.save_thread: + self.save_thread.join(timeout=2) + + # 保存剩余数据 + self.save_to_file() + print("✅ 数据记录已停止") + + def _auto_save_worker(self): + """自动保存工作线程""" + while self.running: + current_time = time.time() + if current_time - self.last_save_time >= self.auto_save_interval: + self.save_to_file() + self.last_save_time = current_time + time.sleep(5) + + def log_drone_state(self, position, yaw, is_flying, mode): + """记录无人机状态""" + if not self.enabled: + return + + record = { + 'timestamp': time.time(), + 'type': 'drone_state', + 'position': position, + 'yaw': yaw, + 'is_flying': is_flying, + 'mode': mode, + 'battery': 100.0, # 模拟电池电量 + 'signal_strength': 5 # 模拟信号强度 + } + self.records.append(record) + + def log_detection_result(self, face_count, person_count, recognized_person): + """记录检测结果""" + if not self.enabled: + return + + record = { + 'timestamp': time.time(), + 'type': 'detection', + 'face_count': face_count, + 'person_count': person_count, + 'recognized_person': recognized_person + } + self.records.append(record) + + def log_control_action(self, action, params=None): + """记录控制动作""" + if not self.enabled: + return + + record = { + 'timestamp': time.time(), + 'type': 'control', + 'action': action, + 'params': params or {} + } + self.records.append(record) + + def log_system_event(self, event_type, message): + """记录系统事件""" + if not self.enabled: + return + + record = { + 'timestamp': time.time(), + 'type': 'system', + 'event': event_type, + 'message': message + } + self.records.append(record) + + def save_to_file(self, filename=None): + """保存数据到文件""" + if not self.records: + return False + + try: + save_file = filename or self.log_file + if not save_file: + return False + + # 转换deque为list + records_list = list(self.records) + + # 添加文件头信息 + data = { + 'metadata': { + 'created_at': datetime.datetime.now().isoformat(), + 'total_records': len(records_list), + 'record_types': set(record['type'] for record in records_list), + 'duration': records_list[-1]['timestamp'] - records_list[0]['timestamp'] if len( + records_list) > 1 else 0 + }, + 'records': records_list + } + + with open(save_file, 'w', encoding='utf-8') as f: + json.dump(data, f, ensure_ascii=False, indent=2) + + print(f"✅ 已保存 {len(records_list)} 条记录到: {save_file}") + return True + + except Exception as e: + print(f"❌ 保存数据失败: {e}") + return False + + def load_from_file(self, filename): + """从文件加载数据""" + try: + with open(filename, 'r', encoding='utf-8') as f: + data = json.load(f) + + # 清空当前记录 + self.records.clear() + + # 加载记录 + for record in data.get('records', []): + self.records.append(record) + + print(f"✅ 已从 {filename} 加载 {len(self.records)} 条记录") + return True + + except Exception as e: + print(f"❌ 加载数据失败: {e}") + return False + + def get_statistics(self): + """获取统计信息""" + if not self.records: + return {} + + records_list = list(self.records) + + stats = { + 'total_records': len(records_list), + 'first_timestamp': records_list[0]['timestamp'] if records_list else 0, + 'last_timestamp': records_list[-1]['timestamp'] if records_list else 0, + 'duration': records_list[-1]['timestamp'] - records_list[0]['timestamp'] if len(records_list) > 1 else 0, + 'record_types': {}, + 'flight_time': 0, + 'total_distance': 0.0, + 'max_altitude': 0.0 + } + + # 统计记录类型 + for record in records_list: + record_type = record['type'] + stats['record_types'][record_type] = stats['record_types'].get(record_type, 0) + 1 + + # 计算飞行数据 + flight_start = None + last_position = None + + for record in records_list: + if record['type'] == 'drone_state': + position = record['position'] + + # 计算飞行时间 + if record['is_flying']: + if flight_start is None: + flight_start = record['timestamp'] + else: + if flight_start is not None: + stats['flight_time'] += record['timestamp'] - flight_start + flight_start = None + + # 计算飞行距离 + if last_position: + dx = position[0] - last_position[0] + dy = position[1] - last_position[1] + dz = position[2] - last_position[2] + distance = (dx ** 2 + dy ** 2 + dz ** 2) ** 0.5 + stats['total_distance'] += distance + + # 更新最大高度 + if position[2] > stats['max_altitude']: + stats['max_altitude'] = position[2] + + last_position = position + + # 处理最后的飞行时间 + if flight_start is not None and records_list: + stats['flight_time'] += records_list[-1]['timestamp'] - flight_start + + return stats + + def export_to_csv(self, filename=None): + """导出数据到CSV""" + if not self.records: + return False + + try: + import csv + + if not filename: + timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + filename = os.path.join(self.log_dir, f"flight_export_{timestamp}.csv") + + records_list = list(self.records) + + with open(filename, 'w', newline='', encoding='utf-8') as f: + writer = csv.writer(f) + + # 写入标题 + writer.writerow([ + '时间戳', '记录类型', 'X位置', 'Y位置', 'Z位置', '航向', + '飞行状态', '控制模式', '人脸数', '行人数', '识别结果' + ]) + + # 写入数据 + for record in records_list: + row = [ + datetime.datetime.fromtimestamp(record['timestamp']).strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], + record['type'] + ] + + if record['type'] == 'drone_state': + row.extend([ + record['position'][0], + record['position'][1], + record['position'][2], + record['yaw'], + '是' if record['is_flying'] else '否', + record['mode'], + '', '', '' # 检测相关字段为空 + ]) + elif record['type'] == 'detection': + row.extend([ + '', '', '', '', '', '', # 无人机相关字段为空 + record['face_count'], + record['person_count'], + record['recognized_person'] + ]) + else: + row.extend(['', '', '', '', '', '', '', '', '']) + + writer.writerow(row) + + print(f"✅ 数据已导出到CSV: {filename}") + return True + + except ImportError: + print("❌ CSV导出失败: 未安装csv模块") + except Exception as e: + print(f"❌ CSV导出失败: {e}") + + return False + + def replay_data(self, speed=1.0, callback=None): + """回放数据""" + if not self.records: + print("⚠️ 无数据可回放") + return False + + try: + print(f"🎬 开始数据回放,速度: {speed}x") + records_list = list(self.records) + + start_time = records_list[0]['timestamp'] + current_time = start_time + + for i, record in enumerate(records_list): + # 计算等待时间 + if i > 0: + time_diff = (record['timestamp'] - records_list[i - 1]['timestamp']) / speed + if time_diff > 0: + time.sleep(time_diff) + + # 调用回调函数处理记录 + if callback: + callback(record, i, len(records_list)) + + current_time = record['timestamp'] + + print("✅ 数据回放完成") + return True + + except KeyboardInterrupt: + print("⏸️ 数据回放被中断") + except Exception as e: + print(f"❌ 数据回放失败: {e}") + + return False \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/drone_controller.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/drone_controller.py new file mode 100644 index 0000000000..b1ea4ba69b --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/drone_controller.py @@ -0,0 +1,68 @@ +import cv2 +import numpy as np +import time + + +class DroneController: + def __init__(self): + self.cap = None + self.mode = "camera" + self.init_camera() + + def init_camera(self): + """初始化摄像头""" + self.mode = "camera" + + # 尝试打开摄像头 + for cam_index in [0, 1]: + try: + self.cap = cv2.VideoCapture(cam_index, cv2.CAP_DSHOW) + if self.cap.isOpened(): + self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) + self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) + print(f"✅ 摄像头 {cam_index} 初始化成功") + return True + except: + continue + + print("⚠️ 摄像头打开失败,使用模拟模式") + return False + + def get_frame(self): + """获取画面""" + if self.cap and self.cap.isOpened(): + ret, frame = self.cap.read() + if ret: + return frame + + # 生成模拟画面 + return self._generate_test_frame() + + def _generate_test_frame(self): + """生成测试画面""" + frame = np.zeros((480, 640, 3), dtype=np.uint8) + + # 网格 + for i in range(0, 640, 40): + cv2.line(frame, (i, 0), (i, 480), (50, 50, 50), 1) + for i in range(0, 480, 40): + cv2.line(frame, (0, i), (640, i), (50, 50, 50), 1) + + # 中心 + center_x, center_y = 320, 240 + cv2.line(frame, (center_x - 20, center_y), (center_x + 20, center_y), (80, 80, 120), 2) + cv2.line(frame, (center_x, center_y - 20), (center_x, center_y + 20), (80, 80, 120), 2) + + # 文字 + cv2.putText(frame, "模拟画面", (20, 40), + cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) + cv2.putText(frame, "按T起飞,按Y追踪", (20, 80), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (200, 200, 100), 1) + + return frame + + def disconnect(self): + """断开连接""" + if self.cap: + self.cap.release() + print("✅ 摄像头已释放") \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/drone_model.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/drone_model.py new file mode 100644 index 0000000000..1159cd1da8 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/drone_model.py @@ -0,0 +1,459 @@ +# drone_model.py - 完全干净版本 +import pygame +from pygame.locals import * +from OpenGL.GL import * +from OpenGL.GLU import * +import math +import sys +import time + + +# === 无人机类 === +class Drone: + def __init__(self): + self.x = 0.0 + self.y = 0.0 + self.z = 1.0 + self.yaw = 0.0 + self.pitch = 0.0 + self.roll = 0.0 + self.propeller_angle = 0.0 + self.is_flying = False + + self.move_forward = False + self.move_backward = False + self.move_left = False + self.move_right = False + self.rotate_left = False + self.rotate_right = False + + self.speed = 2.5 + self.rotate_speed = 150.0 + self.strafe_speed = 1.8 + + print("无人机初始化完成") + + def update(self, delta_time): + if not self.is_flying: + return + + self.propeller_angle += delta_time * 500.0 + if self.propeller_angle > 360: + self.propeller_angle -= 360 + + move_amount = self.speed * delta_time + strafe_amount = self.strafe_speed * delta_time + rotate_amount = self.rotate_speed * delta_time + yaw_rad = math.radians(self.yaw) + + if self.move_forward: + self.x += math.sin(yaw_rad) * move_amount + self.y += -math.cos(yaw_rad) * move_amount + self.pitch = -8.0 + + if self.move_backward: + self.x += -math.sin(yaw_rad) * move_amount + self.y += math.cos(yaw_rad) * move_amount + self.pitch = 8.0 + + if self.move_left: + self.x += -math.cos(yaw_rad) * strafe_amount + self.y += -math.sin(yaw_rad) * strafe_amount + self.roll = -8.0 + + if self.move_right: + self.x += math.cos(yaw_rad) * strafe_amount + self.y += math.sin(yaw_rad) * strafe_amount + self.roll = 8.0 + + if self.rotate_left: + self.yaw += rotate_amount + if self.rotate_right: + self.yaw -= rotate_amount + + self.yaw %= 360.0 + self.pitch *= 0.9 + self.roll *= 0.9 + + if self.z < 0.5: + self.z = 0.5 + + def render(self): + glPushMatrix() + glTranslatef(self.x, self.y, self.z) + glRotatef(self.yaw, 0, 0, 1) + glRotatef(self.pitch, 1, 0, 0) + glRotatef(self.roll, 0, 1, 0) + + glColor3f(0.3, 0.3, 0.3) + self._draw_cube(0.2, 0.15, 0.1) + + arm_positions = [ + (0.25, 0.25, 0), + (-0.25, 0.25, 0), + (0.25, -0.25, 0), + (-0.25, -0.25, 0) + ] + + for i, (ax, ay, az) in enumerate(arm_positions): + glPushMatrix() + glTranslatef(ax, ay, az) + glRotatef(45, 0, 0, 1) + glColor3f(0.4, 0.4, 0.4) + self._draw_cylinder(0.015, 0.18) + glPopMatrix() + + glPushMatrix() + glTranslatef(ax, ay, 0.08) + glColor3f(0.6, 0.6, 0.6) + self._draw_cylinder(0.025, 0.05) + glPopMatrix() + + glPushMatrix() + glTranslatef(ax, ay, 0.13) + glColor3f(0.9, 0.1, 0.1) + glRotatef(self.propeller_angle + i * 90, 0, 0, 1) + self._draw_propeller(0.1) + glPopMatrix() + + glPushMatrix() + glTranslatef(0, 0, 0.08) + if self.is_flying: + blink = int(time.time() * 5) % 2 == 0 + glColor3f(0.0, 1.0 if blink else 0.5, 0.0) + else: + glColor3f(1.0, 0.0, 0.0) + self._draw_sphere(0.02) + glPopMatrix() + + glPushMatrix() + glTranslatef(0, 0.2, 0.05) + glColor3f(1.0, 1.0, 0.0) + self._draw_sphere(0.018) + glPopMatrix() + + glDisable(GL_LIGHTING) + glColor3f(1.0, 1.0, 0.0) + glLineWidth(3.0) + + glBegin(GL_LINES) + glVertex3f(0, 0.1, 0.08) + glVertex3f(0, 0.3, 0.08) + glVertex3f(0, 0.3, 0.08) + glVertex3f(-0.05, 0.22, 0.08) + glVertex3f(0, 0.3, 0.08) + glVertex3f(0.05, 0.22, 0.08) + glVertex3f(-0.05, 0.22, 0.08) + glVertex3f(0.05, 0.22, 0.08) + glEnd() + + glEnable(GL_LIGHTING) + glPopMatrix() + + def _draw_cube(self, width, height, depth): + w, h, d = width / 2, height / 2, depth / 2 + vertices = [ + [-w, -h, -d], [w, -h, -d], [w, h, -d], [-w, h, -d], + [-w, -h, d], [w, -h, d], [w, h, d], [-w, h, d] + ] + faces = [ + [0, 1, 2, 3], [4, 5, 6, 7], + [0, 3, 7, 4], [1, 2, 6, 5], + [0, 1, 5, 4], [2, 3, 7, 6] + ] + + glBegin(GL_QUADS) + for face in faces: + for vertex in face: + glVertex3fv(vertices[vertex]) + glEnd() + + def _draw_cylinder(self, radius, height): + quadric = gluNewQuadric() + gluCylinder(quadric, radius, radius, height, 16, 1) + + glPushMatrix() + glTranslatef(0, 0, height) + gluDisk(quadric, 0, radius, 16, 1) + glPopMatrix() + + glPushMatrix() + glRotatef(180, 1, 0, 0) + gluDisk(quadric, 0, radius, 16, 1) + glPopMatrix() + + def _draw_sphere(self, radius): + quadric = gluNewQuadric() + gluSphere(quadric, radius, 16, 16) + + def _draw_propeller(self, radius): + glBegin(GL_TRIANGLES) + for i in range(4): + angle = i * math.pi / 2 + glVertex3f(0, 0, 0.01) + glVertex3f(radius * math.cos(angle), radius * math.sin(angle), 0.01) + glVertex3f(radius * 0.7 * math.cos(angle + 0.3), radius * 0.7 * math.sin(angle + 0.3), 0.01) + glEnd() + + def reset(self): + self.x = 0.0 + self.y = 0.0 + self.z = 1.0 + self.yaw = 0.0 + self.pitch = 0.0 + self.roll = 0.0 + print("无人机位置已重置") + + +# === 无人机查看器类 === +class DroneViewer: + def __init__(self, width=800, height=600): + pygame.init() + self.width = width + self.height = height + + try: + self.screen = pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL) + except pygame.error as e: + print(f"无法创建OpenGL窗口: {e}") + print("尝试使用标准窗口模式...") + self.screen = pygame.display.set_mode((width, height)) + + pygame.display.set_caption("3D无人机查看器") + + glEnable(GL_DEPTH_TEST) + glEnable(GL_LIGHTING) + glEnable(GL_LIGHT0) + glEnable(GL_COLOR_MATERIAL) + + glLightfv(GL_LIGHT0, GL_POSITION, [5.0, 5.0, 5.0, 1.0]) + glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) + glLightfv(GL_LIGHT0, GL_AMBIENT, [0.2, 0.2, 0.2, 1.0]) + + self.drone = Drone() + self.camera_distance = 8.0 + self.camera_angle_x = 30.0 + self.camera_angle_y = -45.0 + self.mouse_dragging = False + self.last_mouse_x = 0 + self.last_mouse_y = 0 + self.running = True + self.path = [] + self.max_path_length = 200 + + print("=" * 50) + print("3D无人机查看器已启动") + print("控制说明:") + print(" 空格键: 起飞/降落") + print(" W键: 前进 (朝黄色箭头方向)") # W显示为前进 + print(" S键: 后退") # S显示为后退 + print(" A键: 向左平移") + print(" D键: 向右平移") + print(" Q键: 左转") + print(" E键: 右转") + print(" R键: 重置位置") + print(" ESC键: 退出") + print(" 鼠标左键拖拽: 旋转视角") + print(" 鼠标滚轮: 缩放") + print("=" * 50) + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.running = False + + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_ESCAPE: + self.running = False + elif event.key == pygame.K_SPACE: + self.drone.is_flying = not self.drone.is_flying + status = "起飞" if self.drone.is_flying else "降落" + print(f"无人机: {status}") + elif event.key == pygame.K_r: + self.drone.reset() + self.path = [] + print("位置和路径已重置") + elif event.key == pygame.K_s: # S键 - 物理逻辑向前 + self.drone.move_forward = True + print("S键按下: 后退") # 只改打印信息:显示W键前进 + elif event.key == pygame.K_w: # W键 - 物理逻辑向后 + self.drone.move_backward = True + print("W键按下: 前进") # 只改打印信息:显示S键后退 + elif event.key == pygame.K_a: + self.drone.move_left = True + print("A键按下: 向左平移") + elif event.key == pygame.K_d: + self.drone.move_right = True + print("D键按下: 向右平移") + elif event.key == pygame.K_q: + self.drone.rotate_left = True + print("Q键按下: 左转") + elif event.key == pygame.K_e: + self.drone.rotate_right = True + print("E键按下: 右转") + + elif event.type == pygame.KEYUP: + if event.key == pygame.K_s: # S键释放 + self.drone.move_forward = False + elif event.key == pygame.K_w: # W键释放 + self.drone.move_backward = False + elif event.key == pygame.K_a: + self.drone.move_left = False + elif event.key == pygame.K_d: + self.drone.move_right = False + elif event.key == pygame.K_q: + self.drone.rotate_left = False + elif event.key == pygame.K_e: + self.drone.rotate_right = False + + elif event.type == pygame.MOUSEBUTTONDOWN: + if event.button == 1: + self.mouse_dragging = True + self.last_mouse_x, self.last_mouse_y = event.pos + elif event.button == 4: + self.camera_distance = max(3.0, self.camera_distance - 0.5) + elif event.button == 5: + self.camera_distance = min(20.0, self.camera_distance + 0.5) + + elif event.type == pygame.MOUSEBUTTONUP: + if event.button == 1: + self.mouse_dragging = False + + elif event.type == pygame.MOUSEMOTION: + if self.mouse_dragging: + x, y = event.pos + dx = x - self.last_mouse_x + dy = y - self.last_mouse_y + + self.camera_angle_y += dx * 0.5 + self.camera_angle_x -= dy * 0.5 + self.camera_angle_x = max(-85.0, min(85.0, self.camera_angle_x)) + + self.last_mouse_x, self.last_mouse_y = x, y + + def update(self, delta_time): + self.drone.update(delta_time) + + if self.drone.is_flying: + self.path.append((self.drone.x, self.drone.y, self.drone.z)) + if len(self.path) > self.max_path_length: + self.path.pop(0) + + def _draw_grid(self): + glDisable(GL_LIGHTING) + glColor3f(0.4, 0.4, 0.4) + + glBegin(GL_LINES) + for i in range(-15, 16): + glVertex3f(i, -15, 0) + glVertex3f(i, 15, 0) + glVertex3f(-15, i, 0) + glVertex3f(15, i, 0) + glEnd() + + glEnable(GL_LIGHTING) + + def _draw_path(self): + if len(self.path) > 1: + glDisable(GL_LIGHTING) + glColor3f(1.0, 0.5, 0.0) + glLineWidth(2.0) + + glBegin(GL_LINE_STRIP) + for point in self.path: + glVertex3f(point[0], point[1], point[2] + 0.05) + glEnd() + + glEnable(GL_LIGHTING) + + def _draw_axes(self): + glDisable(GL_LIGHTING) + glLineWidth(2.0) + + glBegin(GL_LINES) + glColor3f(1.0, 0.0, 0.0) + glVertex3f(0, 0, 0) + glVertex3f(3, 0, 0) + + glColor3f(0.0, 1.0, 0.0) + glVertex3f(0, 0, 0) + glVertex3f(0, 3, 0) + + glColor3f(0.0, 0.0, 1.0) + glVertex3f(0, 0, 0) + glVertex3f(0, 0, 3) + glEnd() + + glEnable(GL_LIGHTING) + + def render(self): + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + glClearColor(0.08, 0.12, 0.18, 1.0) + + glMatrixMode(GL_PROJECTION) + glLoadIdentity() + gluPerspective(45, self.width / self.height, 0.1, 100.0) + + glMatrixMode(GL_MODELVIEW) + glLoadIdentity() + + cam_x = self.camera_distance * math.sin(math.radians(self.camera_angle_y)) * math.cos( + math.radians(self.camera_angle_x)) + cam_y = self.camera_distance * math.cos(math.radians(self.camera_angle_y)) * math.cos( + math.radians(self.camera_angle_x)) + cam_z = self.camera_distance * math.sin(math.radians(self.camera_angle_x)) + + gluLookAt(cam_x, cam_y, cam_z, 0, 0, 0, 0, 0, 1) + + self._draw_grid() + self._draw_axes() + self._draw_path() + self.drone.render() + + pygame.display.flip() + + def run(self): + clock = pygame.time.Clock() + last_time = pygame.time.get_ticks() + + print("\n操作流程:") + print("1. 按空格键起飞") + print("2. 按W键前进 (朝黄色箭头方向)") # W显示为前进 + print("3. 按S键后退") # S显示为后退 + print("4. 按A/D键左右平移") + print("5. 按Q/E键旋转") + print("6. 鼠标拖拽旋转视角") + print("7. 滚轮缩放\n") + + try: + while self.running: + current_time = pygame.time.get_ticks() + delta_time = (current_time - last_time) / 1000.0 + last_time = current_time + + self.handle_events() + self.update(delta_time) + self.render() + + clock.tick(60) + except KeyboardInterrupt: + print("\n用户中断程序") + except Exception as e: + print(f"运行时错误: {e}") + import traceback + traceback.print_exc() + finally: + pygame.quit() + print("程序已退出") + + +# === 主程序 === +def main(): + print("启动3D无人机查看器...") + + viewer = DroneViewer() + viewer.run() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/face_detector.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/face_detector.py new file mode 100644 index 0000000000..0c7d0fa7db --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/face_detector.py @@ -0,0 +1,193 @@ +import cv2 +import os +import time +import numpy as np +import threading + + +class FaceDetector: + def __init__(self): + # 加载多个级联分类器(提高检测率) + self.cascade_paths = [ + cv2.data.haarcascades + 'haarcascade_frontalface_default.xml', + cv2.data.haarcascades + 'haarcascade_frontalface_alt.xml', + cv2.data.haarcascades + 'haarcascade_frontalface_alt2.xml' + ] + + self.cascades = [] + for path in self.cascade_paths: + cascade = cv2.CascadeClassifier(path) + if not cascade.empty(): + self.cascades.append(cascade) + print(f"✅ 加载分类器: {os.path.basename(path)}") + + if not self.cascades: + raise Exception("❌ 所有人脸检测模型加载失败") + + # 检测参数(优化性能) + self.scale_factor = 1.1 + self.min_neighbors = 3 # 降低以提高召回率 + self.min_size = (30, 30) + self.max_size = (300, 300) + + # 检测缓存 + self.last_detection = [] + self.last_frame_hash = None + self.cache_lock = threading.Lock() + + # 性能统计 + self.detection_count = 0 + self.detection_times = [] + + def detect_faces(self, frame): + """检测人脸(带缓存优化)""" + start_time = time.time() + + # 生成帧哈希(用于缓存) + frame_small = cv2.resize(frame, (160, 120)) + frame_gray = cv2.cvtColor(frame_small, cv2.COLOR_BGR2GRAY) + frame_hash = hash(frame_gray.tobytes()) + + # 检查缓存 + with self.cache_lock: + if self.last_frame_hash == frame_hash and self.last_detection: + detection_time = time.time() - start_time + self.detection_times.append(detection_time) + self.detection_count += 1 + return self.last_detection.copy() + + # 转换为灰度图 + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + + # 均衡化(提高检测率) + gray = cv2.equalizeHist(gray) + + all_faces = [] + + # 使用多个分类器检测 + for cascade in self.cascades: + try: + faces = cascade.detectMultiScale( + gray, + scaleFactor=self.scale_factor, + minNeighbors=self.min_neighbors, + minSize=self.min_size, + maxSize=self.max_size, + flags=cv2.CASCADE_SCALE_IMAGE + ) + + # 合并检测结果 + for (x, y, w, h) in faces: + # 非极大值抑制(避免重复框) + overlap = False + for (fx, fy, fw, fh) in all_faces: + # 计算IoU + ix1 = max(x, fx) + iy1 = max(y, fy) + ix2 = min(x + w, fx + fw) + iy2 = min(y + h, fy + fh) + + if ix2 > ix1 and iy2 > iy1: + area_i = (ix2 - ix1) * (iy2 - iy1) + area_a = w * h + area_b = fw * fh + iou = area_i / (area_a + area_b - area_i) + + if iou > 0.5: # 重叠度超过50% + overlap = True + break + + if not overlap: + all_faces.append((x, y, w, h)) + + except Exception as e: + print(f"分类器检测错误: {e}") + continue + + # 更新缓存 + with self.cache_lock: + self.last_detection = all_faces.copy() + self.last_frame_hash = frame_hash + + # 记录性能 + detection_time = time.time() - start_time + self.detection_times.append(detection_time) + self.detection_count += 1 + + # 保持最近100次记录 + if len(self.detection_times) > 100: + self.detection_times = self.detection_times[-100:] + + return all_faces + + def draw_faces(self, frame, faces): + """绘制人脸框""" + for (x, y, w, h) in faces: + # 绘制框 + cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) + + # 绘制标签 + cv2.putText(frame, "Face", (x, y - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) + + # 显示检测统计 + if len(self.detection_times) > 0: + avg_time = np.mean(self.detection_times[-10:]) * 1000 + stats_text = f"FaceDet: {avg_time:.1f}ms" + cv2.putText(frame, stats_text, (10, 90), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) + + return frame + + def get_detection_stats(self): + """获取检测统计""" + if len(self.detection_times) == 0: + return {"avg_time": 0, "fps": 0} + + avg_time = np.mean(self.detection_times) * 1000 + fps = 1.0 / np.mean(self.detection_times) if np.mean(self.detection_times) > 0 else 0 + + return { + "avg_time_ms": avg_time, + "fps": fps, + "total_detections": self.detection_count, + "cascade_count": len(self.cascades) + } + + def detect_from_camera(self, save_path=None): + """摄像头实时检测(测试用)""" + cap = cv2.VideoCapture(0) + if not cap.isOpened(): + raise Exception("❌ 摄像头打开失败") + + print("📷 人脸检测中(按q退出)") + + while True: + ret, frame = cap.read() + if not ret: + break + + faces = self.detect_faces(frame) + frame = self.draw_faces(frame, faces) + + # 显示检测数量 + cv2.putText(frame, f"Faces: {len(faces)}", (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + + cv2.imshow("Face Detection", frame) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + cap.release() + cv2.destroyAllWindows() + + +def test_face_detector(): + """测试人脸检测""" + detector = FaceDetector() + detector.detect_from_camera() + + +if __name__ == "__main__": + test_face_detector() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/face_recognizer.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/face_recognizer.py new file mode 100644 index 0000000000..411c8722bb --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/face_recognizer.py @@ -0,0 +1,63 @@ +import cv2 +import numpy as np +import os + + +class FaceRecognizer: + def __init__(self, face_db_path="faces"): + self.face_db_path = face_db_path + print(f"✅ 人脸识别器初始化,数据库路径: {face_db_path}") + + def recognize_face(self, frame, threshold=0.6): + """识别人脸""" + # 模拟识别 + import random + + # 模拟检测一些人脸 + h, w = frame.shape[:2] + results = [] + + # 随机生成1-2个识别结果 + num_faces = random.randint(0, 2) + + for i in range(num_faces): + size = random.randint(60, 100) + x = random.randint(0, w - size) + y = random.randint(0, h - size) + + # 随机选择是否识别成功 + if random.random() > 0.5: + names = ["张三", "李四", "王五", "赵六"] + name = random.choice(names) + confidence = random.uniform(0.7, 0.95) + else: + name = "Unknown" + confidence = random.uniform(0.3, 0.5) + + results.append({ + "bbox": (x, y, size, size), + "name": name, + "confidence": confidence + }) + + return results + + def draw_recognition(self, frame, results): + """绘制识别结果""" + for res in results: + x, y, w, h = res["bbox"] + name = res["name"] + conf = res["confidence"] + + if name != "Unknown": + color = (0, 255, 255) # 黄色表示识别成功 + label = f"{name} ({conf:.2f})" + else: + color = (0, 0, 255) # 红色表示未知 + label = "Unknown" + + cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2) + cv2.putText(frame, label, (x, y - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2) + + return frame \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/fix_camera.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/fix_camera.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/install_deps.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/install_deps.py new file mode 100644 index 0000000000..f3728cc371 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/install_deps.py @@ -0,0 +1,32 @@ +# install_deps.py +# !/usr/bin/env python3 + +import subprocess +import sys + + +def install_packages(): + """安装所需依赖包""" + packages = [ + "pygame==2.5.2", + "opencv-python==4.9.0.80", + "numpy==1.24.4", + "pyttsx3==2.90", + ] + + print("正在安装依赖包...") + + for package in packages: + try: + print(f"安装 {package}...") + subprocess.check_call([sys.executable, "-m", "pip", "install", package]) + print(f"✅ {package} 安装成功") + except subprocess.CalledProcessError as e: + print(f"❌ {package} 安装失败: {e}") + + print("\n所有依赖安装完成!") + print("现在可以运行: python main_final.py") + + +if __name__ == "__main__": + install_packages() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/person_detector.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/person_detector.py new file mode 100644 index 0000000000..f8e6a68e1d --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/person_detector.py @@ -0,0 +1,40 @@ +import cv2 +import numpy as np +import random + + +class PersonDetector: + def __init__(self): + print("✅ 人物检测器初始化") + # 这里可以加载YOLO模型,但为了简化先使用模拟 + + def detect_persons(self, frame): + """检测人物""" + h, w = frame.shape[:2] + persons = [] + + # 模拟检测1-4个人物 + num_persons = random.randint(0, 4) + + for i in range(num_persons): + width = random.randint(40, 120) + height = random.randint(80, 200) + x = random.randint(0, w - width) + y = random.randint(0, h - height) + + persons.append({ + "bbox": (x, y, x + width, y + height), + "confidence": random.uniform(0.6, 0.95) + }) + + return persons + + def draw_persons(self, frame, persons): + """绘制人物框""" + for person in persons: + x1, y1, x2, y2 = person["bbox"] + cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) + label = f"Person {person['confidence']:.2f}" + cv2.putText(frame, label, (x1, y1 - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2) + return frame \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/test_compat.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/test_compat.py new file mode 100644 index 0000000000..a8f6492600 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/test_compat.py @@ -0,0 +1,11 @@ +# 创建 test_compat.py +import numpy as np +import cv2 + +print(f"NumPy 版本: {np.__version__}") +print(f"OpenCV 版本: {cv2.__version__}") + +# 测试关键功能 +img = np.zeros((100, 100, 3), dtype=np.uint8) +gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) +print("OpenCV 与 NumPy 2.x 兼容性测试通过!") \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/test_display.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/test_display.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/ui_controller.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/ui_controller.py new file mode 100644 index 0000000000..a2aac8b746 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/ui_controller.py @@ -0,0 +1,427 @@ +# modules/ui_controller.py +import pygame +import sys +import numpy as np +import cv2 +import time +import math + + +class UIController: + def __init__(self): + # 初始化Pygame + pygame.init() + + # 窗口配置 + self.screen_width = 1000 # 稍微加宽以显示更多信息 + self.screen_height = 750 + + # 创建窗口 + try: + self.screen = pygame.display.set_mode( + (self.screen_width, self.screen_height), + pygame.HWSURFACE | pygame.DOUBLEBUF + ) + except: + self.screen = pygame.display.set_mode((self.screen_width, self.screen_height)) + + pygame.display.set_caption("AI无人机人脸跟踪系统 - 多窗口版") + self.clock = pygame.time.Clock() + + # 字体 + self.font_small = pygame.font.SysFont(None, 18) + self.font_normal = pygame.font.SysFont(None, 22) + self.font_large = pygame.font.SysFont(None, 28) + self.font_title = pygame.font.SysFont(None, 32, bold=True) + + # 颜色 + self.COLORS = { + 'bg': (15, 20, 30), + 'panel_bg': (25, 30, 40), + 'panel_border': (60, 80, 100), + 'text': (220, 230, 240), + 'text_secondary': (160, 180, 200), + 'success': (0, 200, 100), + 'warning': (255, 180, 0), + 'error': (255, 80, 80), + 'drone': (0, 180, 255), + 'detection': (255, 100, 100), + 'tracking': (255, 200, 0), + 'camera': (100, 200, 255), + 'voice': (200, 100, 255), + 'data': (100, 255, 200), + } + + # 显示区域 + self.display_width = 640 + self.display_height = 480 + self.display_x = (self.screen_width - self.display_width) // 2 + self.display_y = 50 + + # 状态 + self.state = { + "running": True, + "drone_status": "未连接", + "drone_position": (0.0, 0.0, 1.0), + "drone_yaw": 0.0, + "detected_faces": 0, + "detected_persons": 0, + "recognized_person": "Unknown", + "fps": 0, + "camera_status": "初始化中...", + "tracking_mode": "手动", + "is_flying": False, + "detection_active": True, + "recognition_active": False, + "voice_enabled": False, + "data_logging": False, + "flight_time": 0, + "total_distance": 0.0, + } + + # 3D窗口和语音状态 + self.drone_3d_window_open = False + self.drone_3d_status = "未连接" + self.voice_status = "未启用" + self.data_status = "未记录" + + # 性能监控 + self.frame_count = 0 + self.last_fps_update = time.time() + self.fps_history = [] + + # 错误信息 + self.error_messages = [] + + # 动画 + self.animation_time = time.time() + + # 飞行路径 + self.flight_path = [] + self.max_path_points = 100 + + # 语音消息队列 + self.voice_messages = [] + + # 数据记录 + self.data_records = [] + + print("✅ UI控制器初始化完成") + + def add_voice_message(self, message): + """添加语音消息""" + self.voice_messages.append({ + 'message': message, + 'time': time.time(), + 'played': False + }) + print(f"🗣️ 语音: {message}") + + def add_data_record(self, record_type, data): + """添加数据记录""" + record = { + 'timestamp': time.time(), + 'type': record_type, + 'data': data + } + self.data_records.append(record) + + # 保持记录数量 + if len(self.data_records) > 1000: + self.data_records = self.data_records[-1000:] + + def update_drone_state(self, state): + """更新无人机状态""" + old_position = self.state['drone_position'] + self.state.update(state) + + # 计算飞行距离 + if self.state['is_flying']: + new_position = self.state['drone_position'] + dx = new_position[0] - old_position[0] + dy = new_position[1] - old_position[1] + dz = new_position[2] - old_position[2] + distance = math.sqrt(dx * dx + dy * dy + dz * dz) + self.state['total_distance'] += distance + + # 更新飞行路径 + self.flight_path.append(self.state['drone_position']) + if len(self.flight_path) > self.max_path_points: + self.flight_path = self.flight_path[-self.max_path_points:] + + def update_3d_window_status(self, is_open, status="已连接"): + """更新3D窗口状态""" + self.drone_3d_window_open = is_open + self.drone_3d_status = status + self.state['drone_3d_status'] = status + + # 添加语音提示 + if is_open and self.state['voice_enabled']: + self.add_voice_message(f"3D窗口{status}") + + def update_voice_status(self, enabled, status="已启用"): + """更新语音状态""" + self.state['voice_enabled'] = enabled + self.voice_status = status + + def update_data_status(self, logging, status="记录中"): + """更新数据记录状态""" + self.state['data_logging'] = logging + self.data_status = status + + # 添加语音提示 + if logging and self.state['voice_enabled']: + self.add_voice_message(f"数据记录{status}") + + # ...(handle_events、update_frame_display等方法保持不变,只需在相应位置添加新功能调用)... + + def _draw_status_panel(self): + """绘制状态面板""" + panel_x = 20 + panel_y = self.display_y + self.display_height + 20 + panel_width = self.screen_width - 40 + panel_height = 160 + + # 背景 + pygame.draw.rect( + self.screen, + self.COLORS['panel_bg'], + (panel_x, panel_y, panel_width, panel_height), + 0, 10 + ) + + # 边框 + border_color = self.COLORS['panel_border'] + if self.state['is_flying']: + current_time = time.time() + pulse = (math.sin(current_time * 3) + 1) / 2 + border_color = ( + int(border_color[0] * (0.7 + 0.3 * pulse)), + int(border_color[1] * (0.7 + 0.3 * pulse)), + int(border_color[2] * (0.7 + 0.3 * pulse)) + ) + + pygame.draw.rect( + self.screen, + border_color, + (panel_x, panel_y, panel_width, panel_height), + 2, 10 + ) + + # 三列显示 + col_width = panel_width // 3 - 20 + + # 第一列:无人机状态 + left_col_x = panel_x + 15 + drone_status = [ + ("无人机状态:", self.state['drone_status'], + self.COLORS['success'] if "已连接" in self.state['drone_status'] else self.COLORS['warning']), + ("飞行状态:", "飞行中" if self.state['is_flying'] else "地面", + self.COLORS['drone'] if self.state['is_flying'] else self.COLORS['text_secondary']), + ("控制模式:", self.state['tracking_mode'], + self.COLORS['tracking'] if self.state['tracking_mode'] == "追踪" else self.COLORS['text']), + ("3D窗口:", self.drone_3d_status, + self.COLORS['success'] if "已连接" in self.drone_3d_status else self.COLORS['text_secondary']), + ] + + line_height = 22 + for i, (label, value, color) in enumerate(drone_status): + label_surf = self.font_small.render(label, True, self.COLORS['text_secondary']) + self.screen.blit(label_surf, (left_col_x, panel_y + 15 + i * line_height)) + value_surf = self.font_normal.render(value, True, color) + self.screen.blit(value_surf, (left_col_x + 80, panel_y + 15 + i * line_height)) + + # 第二列:检测状态 + middle_col_x = panel_x + col_width + 25 + detection_status = [ + ("检测状态:", "活跃" if self.state['detection_active'] else "暂停", + self.COLORS['success'] if self.state['detection_active'] else self.COLORS['warning']), + ("人脸检测:", f"{self.state['detected_faces']} 个", + self.COLORS['detection'] if self.state['detected_faces'] > 0 else self.COLORS['text_secondary']), + ("行人检测:", f"{self.state['detected_persons']} 个", + self.COLORS['detection'] if self.state['detected_persons'] > 0 else self.COLORS['text_secondary']), + ("识别结果:", self.state['recognized_person'], + self.COLORS['success'] if self.state['recognized_person'] != "Unknown" else self.COLORS['text_secondary']), + ] + + for i, (label, value, color) in enumerate(detection_status): + label_surf = self.font_small.render(label, True, self.COLORS['text_secondary']) + self.screen.blit(label_surf, (middle_col_x, panel_y + 15 + i * line_height)) + value_surf = self.font_normal.render(value, True, color) + self.screen.blit(value_surf, (middle_col_x + 80, panel_y + 15 + i * line_height)) + + # 第三列:系统功能 + right_col_x = panel_x + 2 * col_width + 35 + system_status = [ + ("语音播报:", "启用" if self.state['voice_enabled'] else "禁用", + self.COLORS['voice'] if self.state['voice_enabled'] else self.COLORS['text_secondary']), + ("数据记录:", "进行中" if self.state['data_logging'] else "停止", + self.COLORS['data'] if self.state['data_logging'] else self.COLORS['text_secondary']), + ("飞行时间:", f"{self.state['flight_time']:.0f}秒", self.COLORS['text']), + ("飞行距离:", f"{self.state['total_distance']:.1f}米", self.COLORS['text']), + ] + + for i, (label, value, color) in enumerate(system_status): + label_surf = self.font_small.render(label, True, self.COLORS['text_secondary']) + self.screen.blit(label_surf, (right_col_x, panel_y + 15 + i * line_height)) + value_surf = self.font_normal.render(value, True, color) + self.screen.blit(value_surf, (right_col_x + 80, panel_y + 15 + i * line_height)) + + def _draw_system_info(self): + """绘制系统信息""" + info_x = self.screen_width - 220 + info_y = 20 + + # 背景 + pygame.draw.rect( + self.screen, + (30, 35, 45, 200), + (info_x - 10, info_y - 10, 210, 120), + 0, 5 + ) + + # FPS + fps_color = self.COLORS['success'] if self.state['fps'] >= 25 else \ + self.COLORS['warning'] if self.state['fps'] >= 15 else \ + self.COLORS['error'] + fps_surf = self.font_normal.render(f"FPS: {self.state['fps']}", True, fps_color) + self.screen.blit(fps_surf, (info_x, info_y)) + + # 3D窗口状态 + drone_color = self.COLORS['success'] if "已连接" in self.drone_3d_status else self.COLORS['text_secondary'] + drone_surf = self.font_small.render(f"3D窗口: {self.drone_3d_status}", True, drone_color) + self.screen.blit(drone_surf, (info_x, info_y + 25)) + + # 语音状态 + voice_color = self.COLORS['voice'] if self.state['voice_enabled'] else self.COLORS['text_secondary'] + voice_surf = self.font_small.render(f"语音: {'启用' if self.state['voice_enabled'] else '禁用'}", True, + voice_color) + self.screen.blit(voice_surf, (info_x, info_y + 45)) + + # 数据记录状态 + data_color = self.COLORS['data'] if self.state['data_logging'] else self.COLORS['text_secondary'] + data_surf = self.font_small.render(f"数据: {'记录中' if self.state['data_logging'] else '停止'}", True, + data_color) + self.screen.blit(data_surf, (info_x, info_y + 65)) + + def _draw_flight_path(self): + """绘制飞行路径预览""" + if len(self.flight_path) < 2: + return + + # 在画面右下角绘制小地图 + map_size = 120 + map_x = self.screen_width - map_size - 20 + map_y = self.screen_height - map_size - 20 + + # 地图背景 + pygame.draw.rect( + self.screen, (20, 25, 35), + (map_x, map_y, map_size, map_size), 0, 5 + ) + + pygame.draw.rect( + self.screen, self.COLORS['panel_border'], + (map_x, map_y, map_size, map_size), 1, 5 + ) + + # 地图标题 + map_title = "飞行路径" + title_surf = self.font_small.render(map_title, True, self.COLORS['text_secondary']) + self.screen.blit(title_surf, (map_x + 5, map_y + 5)) + + # 转换坐标到地图尺寸 + scale = map_size * 0.7 / 10 # 假设飞行范围是10x10单位 + + # 绘制路径 + if len(self.flight_path) > 1: + path_points = [] + for x, y, z in self.flight_path: + # 转换为地图坐标 + map_px = map_x + map_size // 2 + x * scale + map_py = map_y + map_size // 2 + y * scale + path_points.append((map_px, map_py)) + + # 绘制路径线 + if len(path_points) > 1: + pygame.draw.lines( + self.screen, self.COLORS['drone'], + False, path_points, 2 + ) + + # 绘制当前位置 + if path_points: + last_point = path_points[-1] + pygame.draw.circle( + self.screen, self.COLORS['tracking'], + (int(last_point[0]), int(last_point[1])), 3 + ) + + def _draw_voice_queue(self): + """绘制语音消息队列""" + if not self.voice_messages: + return + + queue_x = 20 + queue_y = self.screen_height - 60 + + # 显示最新的语音消息 + latest_msg = self.voice_messages[-1] + if time.time() - latest_msg['time'] < 3: # 显示最近3秒的消息 + msg_text = f"🗣️ {latest_msg['message']}" + msg_surf = self.font_small.render(msg_text, True, self.COLORS['voice']) + self.screen.blit(msg_surf, (queue_x, queue_y)) + + def update_lightweight(self, frame): + """更新UI""" + try: + # 清除过期错误 + self.clear_old_errors() + + # 填充背景 + self.screen.fill(self.COLORS['bg']) + + # 更新画面显示 + self.update_frame_display(frame) + + # 更新状态面板 + self._draw_status_panel() + + # 绘制控制面板 + self._draw_control_panel() + + # 绘制系统信息 + self._draw_system_info() + + # 绘制飞行路径 + self._draw_flight_path() + + # 绘制语音队列 + self._draw_voice_queue() + + # 更新FPS + self.update_fps() + + # 更新显示 + pygame.display.flip() + + except Exception as e: + self.set_error(f"UI更新异常: {e}", "error") + + def save_data_records(self, filename=None): + """保存数据记录""" + if not filename: + import datetime + filename = f"flight_data_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.json" + + try: + import json + with open(filename, 'w', encoding='utf-8') as f: + json.dump(self.data_records, f, ensure_ascii=False, indent=2) + print(f"✅ 数据已保存到: {filename}") + return True + except Exception as e: + print(f"❌ 数据保存失败: {e}") + return False + + # ...(其他方法保持不变)... \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/modules/voice_synthesizer.py b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/voice_synthesizer.py new file mode 100644 index 0000000000..f81e559113 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/modules/voice_synthesizer.py @@ -0,0 +1,158 @@ +# modules/voice_synthesizer.py +import threading +import queue +import time +import os + + +class VoiceSynthesizer: + def __init__(self, enabled=True, lang="zh-CN"): + self.enabled = enabled + self.lang = lang + self.message_queue = queue.Queue() + self.currently_speaking = False + self.voice_engine = None + self.worker_thread = None + self.running = False + + if self.enabled: + self._init_voice_engine() + self.start() + + def _init_voice_engine(self): + """初始化语音引擎""" + try: + import pyttsx3 + self.voice_engine = pyttsx3.init() + + # 设置语音参数 + self.voice_engine.setProperty('rate', 150) # 语速 + self.voice_engine.setProperty('volume', 0.8) # 音量 + + # 尝试设置中文语音 + voices = self.voice_engine.getProperty('voices') + for voice in voices: + if "zh" in voice.language or "chinese" in voice.name.lower(): + self.voice_engine.setProperty('voice', voice.id) + break + + print("✅ 语音引擎初始化成功") + return True + except ImportError: + print("⚠️ pyttsx3未安装,语音功能不可用") + print(" 请运行: pip install pyttsx3") + except Exception as e: + print(f"⚠️ 语音引擎初始化失败: {e}") + + self.enabled = False + return False + + def speak(self, text, priority=1): + """语音播报""" + if not self.enabled or not text: + return False + + try: + self.message_queue.put((text, priority, time.time())) + return True + except: + return False + + def _voice_worker(self): + """语音工作线程""" + while self.running: + try: + # 从队列获取消息(最多等待1秒) + text, priority, timestamp = self.message_queue.get(timeout=1) + + if self.voice_engine: + self.currently_speaking = True + self.voice_engine.say(text) + self.voice_engine.runAndWait() + self.currently_speaking = False + + # 标记任务完成 + self.message_queue.task_done() + + # 短暂暂停,避免连续播报 + time.sleep(0.5) + + except queue.Empty: + continue + except Exception as e: + print(f"语音播报错误: {e}") + self.currently_speaking = False + time.sleep(1) + + def start(self): + """启动语音服务""" + if not self.enabled or self.running: + return + + self.running = True + self.worker_thread = threading.Thread(target=self._voice_worker, daemon=True) + self.worker_thread.start() + print("✅ 语音服务已启动") + + def stop(self): + """停止语音服务""" + self.running = False + if self.worker_thread: + self.worker_thread.join(timeout=2) + if self.voice_engine: + self.voice_engine.stop() + print("✅ 语音服务已停止") + + def speak_detection_result(self, face_count, person_count): + """播报检测结果""" + if face_count == 0 and person_count == 0: + text = "未检测到目标" + else: + text = f"检测到{face_count}个人脸,{person_count}个行人" + return self.speak(text) + + def speak_recognition_result(self, name): + """播报识别结果""" + if name == "Unknown": + text = "未识别到已知人员" + else: + text = f"识别到{name}" + return self.speak(text) + + def speak_drone_status(self, status, is_flying=False): + """播报无人机状态""" + if is_flying: + text = f"无人机正在飞行,状态:{status}" + else: + text = f"无人机状态:{status}" + return self.speak(text) + + def speak_tracking_status(self, tracking_mode, target_count=0): + """播报追踪状态""" + if tracking_mode == "追踪": + text = f"追踪模式已开启,正在追踪{target_count}个目标" + else: + text = "手动控制模式" + return self.speak(text) + + def speak_system_status(self, status): + """播报系统状态""" + text = f"系统状态:{status}" + return self.speak(text) + + def is_speaking(self): + """是否正在播报""" + return self.currently_speaking + + def get_queue_size(self): + """获取消息队列大小""" + return self.message_queue.qsize() + + def clear_queue(self): + """清空消息队列""" + while not self.message_queue.empty(): + try: + self.message_queue.get_nowait() + self.message_queue.task_done() + except: + break \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/patch_pkgutil.py b/src/AI_Drone_Face_Recognition_Human_Tracking/patch_pkgutil.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/run.py b/src/AI_Drone_Face_Recognition_Human_Tracking/run.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/run_complete.py b/src/AI_Drone_Face_Recognition_Human_Tracking/run_complete.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/run_integrated.py b/src/AI_Drone_Face_Recognition_Human_Tracking/run_integrated.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/start_simple.py b/src/AI_Drone_Face_Recognition_Human_Tracking/start_simple.py new file mode 100644 index 0000000000..0c51be3546 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/start_simple.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python3 +""" +简化启动脚本 - 测试UI是否正常 +""" +import pygame +import sys +import os + +# 设置路径 +current_dir = os.path.dirname(os.path.abspath(__file__)) +modules_dir = os.path.join(current_dir, 'modules') + +if modules_dir not in sys.path: + sys.path.insert(0, modules_dir) + +print("=" * 60) +print("🚀 测试UI控制器") +print("=" * 60) + + +def test_ui(): + """测试UI控制器""" + try: + from ui_controller import UIController + print("导入UI控制器成功") + + # 创建UI实例 + ui = UIController() + print("UI实例创建成功") + + # 运行简单的测试循环 + print("开始UI测试循环...") + print("按Q或ESC退出") + + clock = pygame.time.Clock() + running = True + + while running: + # 处理事件 + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.KEYDOWN: + if event.key in (pygame.K_q, pygame.K_ESCAPE): + running = False + + # 创建一个测试图像 + import numpy as np + test_image = np.zeros((480, 640, 3), dtype=np.uint8) + test_image[200:280, 280:360] = [255, 0, 0] # 红色方块 + test_image[100:180, 100:180] = [0, 255, 0] # 绿色方块 + + # 更新UI + ui.update_lightweight(test_image) + + # 更新无人机状态(模拟) + ui.update_drone_state({ + 'drone_status': '已连接', + 'is_flying': True, + 'drone_position': (1.5, 2.3, 1.8), + 'drone_yaw': 45.0, + 'tracking_mode': '手动', + 'camera_status': '640x480 @ 30fps', + 'detected_faces': 2, + 'detected_persons': 3, + 'recognized_person': '张三', + 'fps': 30, + }) + + clock.tick(30) + + ui.quit() + print("✅ UI测试完成") + + except Exception as e: + print(f"❌ UI测试失败: {e}") + import traceback + traceback.print_exc() + + +def test_main(): + """测试主程序""" + try: + print("\n测试主程序导入...") + # 尝试导入各个模块 + from drone_controller import DroneController + print(" ✅ drone_controller") + + from face_detector import FaceDetector + print(" ✅ face_detector") + + from person_detector import PersonDetector + print(" ✅ person_detector") + + from face_recognizer import FaceRecognizer + print(" ✅ face_recognizer") + + print("✅ 所有模块导入成功") + return True + + except ImportError as e: + print(f"❌ 模块导入失败: {e}") + return False + + +if __name__ == "__main__": + print("\n选择测试模式:") + print("1. 测试UI控制器") + print("2. 测试模块导入") + print("3. 退出") + + choice = input("请选择 (1-3): ").strip() + + if choice == "1": + test_ui() + elif choice == "2": + test_main() + elif choice == "3": + print("退出") + else: + print("无效选择") + + print("\n程序结束") \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/test_camera.py b/src/AI_Drone_Face_Recognition_Human_Tracking/test_camera.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000000..e69de29bb2