From a96c78673f38f2d9ac3ad538dc5686a7d2553f0c Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Sun, 14 Dec 2025 20:54:44 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E9=80=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AI_Drone_Face_Recognition_Human_Tracking/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/README.md diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/README.md b/src/AI_Drone_Face_Recognition_Human_Tracking/README.md new file mode 100644 index 0000000000..bdca2970c7 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/README.md @@ -0,0 +1 @@ +该项目是一个基于人工智能的无人机控制系统,能够用YOLOv8检测图像中的人物,用OpenCV检测人脸,用DeepFace识别人,并能飞近用户从视频流中选定的目标。 \ No newline at end of file From e57010ea6a277e5d4dffbe63eec50e3615a86c56 Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Mon, 15 Dec 2025 09:57:24 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=89=93=E5=BC=80=E6=91=84=E5=83=8F?= =?UTF-8?q?=E5=A4=B4=EF=BC=8C=E5=BD=95=E5=85=A5=E5=B9=B6=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E4=BA=BA=E8=84=B8=E5=8F=8A=E5=90=8D=E5=AD=97=EF=BC=8C=E6=94=BE?= =?UTF-8?q?=E5=85=A5=E6=95=B0=E6=8D=AE=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils.py.py | 160 ++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py b/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py new file mode 100644 index 0000000000..e7962cbefe --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py @@ -0,0 +1,160 @@ +import os +import cv2 +from deepface import DeepFace +import numpy as np + + +class FaceDB: + """人脸数据库管理类""" + + def __init__(self, face_dir="faces"): + self.face_dir = face_dir + self.face_data = {} # 存储{人名: 人脸特征} + self._load_face_database() + + def _load_face_database(self): + """加载人脸库中的所有人脸特征""" + if not os.path.exists(self.face_dir): + os.makedirs(self.face_dir) + print(f"创建人脸库目录: {self.face_dir}") + + self.face_data.clear() # 清空旧数据 + for img_name in os.listdir(self.face_dir): + if img_name.endswith((".jpg", ".png")): + person_name = os.path.splitext(img_name)[0] + img_path = os.path.join(self.face_dir, img_name) + try: + # 提取人脸特征(Facenet模型) + embedding = DeepFace.represent( + img_path, + model_name="Facenet", + enforce_detection=True + )[0]["embedding"] + self.face_data[person_name] = embedding + print(f"✅ 成功加载人脸: {person_name}") + except Exception as e: + print(f"❌ 加载{img_name}失败: {str(e)[:50]}...") + + def recognize_face(self, frame, threshold=0.6): + """识别人脸,返回匹配的人名或None""" + try: + # 提取当前帧人脸特征 + frame_embedding = DeepFace.represent( + frame, + model_name="Facenet", + enforce_detection=True + )[0]["embedding"] + + # 对比人脸库(计算欧式距离) + min_distance = float("inf") + matched_name = None + for name, embedding in self.face_data.items(): + distance = np.linalg.norm(np.array(frame_embedding) - np.array(embedding)) + if distance < min_distance and distance < threshold: + min_distance = distance + matched_name = name + return matched_name, min_distance # 新增返回距离,方便调试 + except Exception as e: + print(f"❌ 人脸识别失败: {str(e)[:50]}...") + return None, float("inf") + + def add_face(self, frame, person_name): + """添加人脸到数据库""" + # 先检测人脸,确保有效 + try: + # 验证帧中有人脸 + DeepFace.extract_faces(frame, enforce_detection=True) + # 保存人脸图片 + img_path = os.path.join(self.face_dir, f"{person_name}.jpg") + cv2.imwrite(img_path, frame) + # 重新加载数据库 + self._load_face_database() + print(f"✅ 已添加[{person_name}]到人脸库,路径: {img_path}") + return True + except Exception as e: + print(f"❌ 添加人脸失败: {str(e)[:50]}...") + return False + + def list_faces(self): + """列出人脸库中所有已注册的人名""" + return list(self.face_data.keys()) + + +def draw_detection_box(frame, bbox, label, color=(0, 255, 0)): + """绘制检测框和标签""" + x1, y1, x2, y2 = map(int, bbox) + cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) + cv2.putText( + frame, label, (x1, y1 - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2 + ) + return frame + + +# ===================== 独立运行测试逻辑 ===================== +if __name__ == "__main__": + # 初始化人脸库 + face_db = FaceDB(face_dir="faces") + print(f"\n当前人脸库已注册: {face_db.list_faces()}\n") + + # 打开摄像头 + cap = cv2.VideoCapture(0) + if not cap.isOpened(): + print("❌ 无法打开摄像头,请检查设备!") + exit(1) + + # 设置摄像头分辨率 + cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) + cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) + + print("=" * 50) + print("🎯 FaceDB 测试工具(独立运行模式)") + print("按键说明:") + print(" a → 添加当前帧人脸到数据库") + print(" r → 识别当前帧人脸") + print(" l → 列出已注册人脸") + print(" q → 退出程序") + print("=" * 50) + + while True: + # 读取摄像头帧 + ret, frame = cap.read() + if not ret: + print("❌ 无法读取摄像头画面!") + break + + # 显示实时画面 + cv2.imshow("FaceDB Test (utils.py)", frame) + + # 按键处理 + key = cv2.waitKey(1) & 0xFF + if key == ord('q'): + print("🔚 退出测试程序...") + break + + elif key == ord('a'): + # 添加人脸 + person_name = input("\n请输入要添加的人名: ").strip() + if not person_name: + print("❌ 人名不能为空!") + continue + face_db.add_face(frame, person_name) + + elif key == ord('r'): + # 识别人脸 + print("\n🔍 正在识别人脸...") + name, distance = face_db.recognize_face(frame) + if name: + print(f"✅ 识别成功!匹配到: {name} (距离: {distance:.4f})") + else: + print(f"❌ 未识别到匹配人脸(最小距离: {distance:.4f})") + + elif key == ord('l'): + # 列出已注册人脸 + faces = face_db.list_faces() + print(f"\n📋 人脸库已注册名单: {faces if faces else '空'}") + + # 释放资源 + cap.release() + cv2.destroyAllWindows() + print("\n✅ 资源已释放,程序结束") \ No newline at end of file From 1552a8813f6a42b80a47311e14fb71c3e532c90d Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Mon, 15 Dec 2025 10:59:33 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=9E=E7=8E=B0=E4=BA=86?= =?UTF-8?q?=E4=BA=94=E4=B8=AA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../voice_module.py | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.py diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.py b/src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.py new file mode 100644 index 0000000000..168813fa82 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.py @@ -0,0 +1,172 @@ +import pyttsx3 +import platform + + +class VoiceAssistant: + """语音助手类(增强兼容性+详细日志)""" + + def __init__(self): + self.engine = None + self._init_engine() # 初始化语音引擎 + + def _init_engine(self): + """初始化语音引擎,兼容不同系统""" + try: + self.engine = pyttsx3.init() + # 基础配置 + self.engine.setProperty("rate", 150) # 语速(100-200为宜) + self.engine.setProperty("volume", 1.0) # 音量(0.0-1.0) + + # 系统适配:Windows优先使用中文语音包 + system = platform.system() + if system == "Windows": + self._set_chinese_voice() + + print("✅ 语音引擎初始化成功!") + except Exception as e: + print(f"❌ 语音引擎初始化失败: {str(e)}") + self.engine = None + + def _set_chinese_voice(self): + """Windows系统设置中文语音包(需提前安装)""" + try: + voices = self.engine.getProperty('voices') + # 遍历语音包,选择中文语音 + for voice in voices: + if "zh-CN" in voice.id or "中文" in voice.name: + self.engine.setProperty('voice', voice.id) + print(f"✅ 已切换为中文语音包: {voice.name}") + break + else: + print("⚠️ 未找到中文语音包,使用默认语音(可能为英文)") + except Exception as e: + print(f"⚠️ 设置中文语音包失败: {str(e)}") + + def speak(self, text): + """文本转语音(增加容错)""" + if not self.engine: + print("❌ 语音引擎未初始化,无法合成语音") + return + + if not text.strip(): + print("❌ 合成文本不能为空!") + return + + try: + print(f"🔊 正在合成语音: {text}") + self.engine.say(text) + self.engine.runAndWait() # 等待语音播放完成 + print("✅ 语音合成完成!") + except Exception as e: + print(f"❌ 语音合成失败: {str(e)}") + + def list_voices(self): + """列出当前系统可用的语音包(调试用)""" + if not self.engine: + print("❌ 语音引擎未初始化,无法获取语音包") + return [] + + try: + voices = self.engine.getProperty('voices') + print("\n📋 系统可用语音包列表:") + for idx, voice in enumerate(voices): + print(f" [{idx}] 名称: {voice.name} | ID: {voice.id} | 语言: {voice.languages}") + return voices + except Exception as e: + print(f"❌ 获取语音包列表失败: {str(e)}") + return [] + + def adjust_settings(self, rate=None, volume=None): + """调整语速/音量(调试用)""" + if not self.engine: + print("❌ 语音引擎未初始化,无法调整设置") + return + + if rate is not None: + if 50 <= rate <= 300: + self.engine.setProperty("rate", rate) + print(f"✅ 语速已调整为: {rate}") + else: + print("⚠️ 语速范围需在50-300之间!") + + if volume is not None: + if 0.0 <= volume <= 1.0: + self.engine.setProperty("volume", volume) + print(f"✅ 音量已调整为: {volume}") + else: + print("⚠️ 音量范围需在0.0-1.0之间!") + + def __del__(self): + """析构函数,释放引擎资源""" + if self.engine: + try: + self.engine.stop() + except: + pass + + +# ===================== 独立运行测试逻辑 ===================== +if __name__ == "__main__": + # 初始化语音助手 + voice_assist = VoiceAssistant() + + # 打印系统信息 + print("\n" + "=" * 50) + print("🎯 语音助手测试工具(独立运行模式)") + print(f"💻 当前系统: {platform.system()}") + print("=" * 50) + + # 列出可用语音包 + voice_assist.list_voices() + + # 交互菜单 + print("\n📢 操作菜单:") + print(" 1 → 播放测试文本(中文)") + print(" 2 → 播放测试文本(英文)") + print(" 3 → 自定义文本播放") + print(" 4 → 调整语速(默认150)") + print(" 5 → 调整音量(默认1.0)") + print(" q → 退出程序") + print("-" * 30) + + while True: + choice = input("\n请输入操作编号: ").strip() + + if choice == "q": + print("🔚 退出语音助手测试程序...") + break + + elif choice == "1": + # 中文测试 + voice_assist.speak("你好,我是AI无人机语音助手,测试语音合成功能正常!") + + elif choice == "2": + # 英文测试 + voice_assist.speak("Hello, I am the AI drone voice assistant, test speech synthesis function is normal!") + + elif choice == "3": + # 自定义文本 + custom_text = input("请输入要合成的文本: ").strip() + if custom_text: + voice_assist.speak(custom_text) + else: + print("❌ 自定义文本不能为空!") + + elif choice == "4": + # 调整语速 + try: + new_rate = int(input("请输入新语速(50-300): ").strip()) + voice_assist.adjust_settings(rate=new_rate) + except ValueError: + print("❌ 请输入有效的数字!") + + elif choice == "5": + # 调整音量 + try: + new_volume = float(input("请输入新音量(0.0-1.0): ").strip()) + voice_assist.adjust_settings(volume=new_volume) + except ValueError: + print("❌ 请输入有效的数字(如0.5)!") + + else: + print("❌ 无效的操作编号,请重新输入!") \ No newline at end of file From b18cb55ace991a2c9cd6f231ed71d05eab59546a Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Tue, 16 Dec 2025 22:04:22 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=97=A0?= =?UTF-8?q?=E4=BA=BA=E6=9C=BA=E2=80=9C=E8=A7=86=E9=A2=91=E5=B8=A7=20+=20?= =?UTF-8?q?=E5=8D=8A=E9=80=8F=E6=98=8E=E5=9C=B0=E5=9B=BE=20+=20=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E6=A0=87=E8=AE=B0=E2=80=9D=20=E7=9A=84=E5=8F=AF?= =?UTF-8?q?=E8=A7=86=E5=8C=96=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../overlay_map.py | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py b/src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py new file mode 100644 index 0000000000..2446ee1056 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py @@ -0,0 +1,170 @@ +import cv2 +import numpy as np +import os +import platform + + +class MapOverlay: + """地图叠加类(增强容错+单独运行支持)""" + + def __init__(self, map_path="map.png", alpha=0.3): + """ + :param map_path: 地图图片路径 + :param alpha: 地图透明度(0-1,越小越透明) + """ + self.alpha = alpha + self.map_img = None + self._load_map(map_path) # 加载地图(兼容无文件场景) + + def _load_map(self, map_path): + """加载地图图片,无文件时生成默认地图""" + try: + if os.path.exists(map_path): + self.map_img = cv2.imread(map_path) + if self.map_img is None: + raise Exception("地图文件损坏或格式不支持") + print(f"✅ 成功加载地图: {map_path}") + else: + # 生成默认空白地图(带文字提示) + self.map_img = np.zeros((200, 300, 3), dtype=np.uint8) + cv2.putText( + self.map_img, "DEFAULT MAP", (20, 60), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2 + ) + cv2.putText( + self.map_img, f"({map_path} not found)", (20, 100), + cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1 + ) + print(f"⚠️ 未找到地图文件: {map_path},使用默认地图") + except Exception as e: + # 异常时仍生成默认地图 + self.map_img = np.zeros((200, 300, 3), dtype=np.uint8) + cv2.putText( + self.map_img, "MAP LOAD FAILED", (20, 80), + cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2 + ) + print(f"❌ 加载地图失败: {str(e)}") + + def overlay(self, frame, drone_pos=(0, 0)): + """ + 将地图叠加到视频帧右上角 + :param frame: 原始视频帧 + :param drone_pos: 无人机位置(用于绘制标记) + :return: 叠加后的帧 + """ + if self.map_img is None or frame is None: + return frame # 无地图/无帧时直接返回原帧 + + h, w = frame.shape[:2] + map_h, map_w = self.map_img.shape[:2] + + # 调整地图大小(适配帧尺寸,占帧的1/4) + target_map_w = int(w / 4) + target_map_h = int(target_map_w * (map_h / map_w)) # 保持宽高比 + map_resized = cv2.resize(self.map_img, (target_map_w, target_map_h)) + map_h, map_w = map_resized.shape[:2] + + # 叠加位置(右上角,留10px边距) + x_offset = w - map_w - 10 + y_offset = 10 + + # 边界校验(避免地图超出帧范围) + if x_offset < 0: x_offset = 10 + if y_offset < 0: y_offset = 10 + if (y_offset + map_h) > h: map_h = h - y_offset - 10 + if (x_offset + map_w) > w: map_w = w - x_offset - 10 + + # 透明度混合(仅叠加有效区域) + roi = frame[y_offset:y_offset + map_h, x_offset:x_offset + map_w] + map_cropped = map_resized[:map_h, :map_w] # 裁剪地图适配ROI + blended = cv2.addWeighted(roi, 1 - self.alpha, map_cropped, self.alpha, 0) + frame[y_offset:y_offset + map_h, x_offset:x_offset + map_w] = blended + + # 绘制无人机位置标记(地图中心) + drone_x = x_offset + map_w // 2 + drone_y = y_offset + map_h // 2 + cv2.circle(frame, (drone_x, drone_y), 5, (0, 0, 255), -1) # 红色圆点 + cv2.putText( + frame, "Drone", (drone_x - 20, drone_y - 10), + cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 1 + ) + return frame + + def adjust_alpha(self, new_alpha): + """调整地图透明度(调试用)""" + if 0.0 <= new_alpha <= 1.0: + self.alpha = new_alpha + print(f"✅ 地图透明度已调整为: {new_alpha}") + else: + print("⚠️ 透明度范围需在0.0-1.0之间!") + + +# ===================== 独立运行测试逻辑 ===================== +if __name__ == "__main__": + # 初始化地图叠加器(可自定义地图路径) + map_overlay = MapOverlay(map_path="map.png", alpha=0.3) + + # 打印系统信息 + print("\n" + "=" * 50) + print("🎯 地图叠加模块测试工具(独立运行模式)") + print(f"💻 当前系统: {platform.system()}") + print(f"🗺️ 地图透明度: {map_overlay.alpha}") + print("=" * 50) + + # 打开摄像头(默认0号设备) + cap = cv2.VideoCapture(0) + if not cap.isOpened(): + print("❌ 无法打开摄像头!") + exit(1) + + # 设置摄像头分辨率 + cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640) + cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) + + # 操作提示 + print("\n📢 操作说明:") + print(" ↑ → 增加地图透明度") + print(" ↓ → 降低地图透明度") + print(" s → 保存当前叠加画面") + print(" q → 退出程序") + print("-" * 30) + + save_count = 0 # 保存图片计数 + while True: + # 读取摄像头帧 + ret, frame = cap.read() + if not ret: + print("❌ 无法读取摄像头画面!") + break + + # 叠加地图 + frame_overlay = map_overlay.overlay(frame) + + # 显示叠加后的画面 + cv2.imshow("Map Overlay Test (overlay_map.py)", frame_overlay) + + # 按键处理 + key = cv2.waitKey(1) & 0xFF + if key == ord('q'): + print("🔚 退出地图叠加测试程序...") + break + + elif key == ord('s'): + # 保存当前画面 + save_path = f"overlay_test_{save_count}.jpg" + cv2.imwrite(save_path, frame_overlay) + print(f"✅ 已保存画面: {save_path}") + save_count += 1 + + elif key == 2490368: # 上方向键(增加透明度) + new_alpha = min(map_overlay.alpha + 0.1, 1.0) + map_overlay.adjust_alpha(new_alpha) + + elif key == 2621440: # 下方向键(降低透明度) + new_alpha = max(map_overlay.alpha - 0.1, 0.0) + map_overlay.adjust_alpha(new_alpha) + + # 释放资源 + cap.release() + cv2.destroyAllWindows() + print("\n✅ 资源已释放,程序结束") \ No newline at end of file From d1a6894f7ec7139a513fd5e6e58f732aae15cf6c Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Tue, 16 Dec 2025 23:19:04 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=97=A0?= =?UTF-8?q?=E4=BA=BA=E6=9C=BA=E6=A8=A1=E6=8B=9F=E4=BB=A5=E5=8F=8A=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../drone_control.py | 283 ++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/drone_control.py diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/drone_control.py b/src/AI_Drone_Face_Recognition_Human_Tracking/drone_control.py new file mode 100644 index 0000000000..d2709c0001 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/drone_control.py @@ -0,0 +1,283 @@ +"""无人机控制模块""" +import pyvista as pv +import pygame +import numpy as np +import sys +from enum import Enum +import warnings + +# 忽略PyVista版本兼容警告(提升运行体验) +warnings.filterwarnings("ignore") + + +# ===================== 无人机状态枚举 ===================== +class DroneState(Enum): + LANDED = "Landed" + FLYING = "Flying" + + +# ===================== 虚拟无人机类 ===================== +class VirtualDrone: + def __init__(self): + # 无人机物理参数 + self.position = np.array([0.0, 0.0, 0.0]) # 三维坐标 (x, y, z) + self.velocity = np.array([0.0, 0.0, 0.0]) # 速度 (x, y, z) + self.yaw = 0.0 # 偏航角(绕z轴旋转,°) + self.speed = 0.5 # 飞行速度(m/s) + self.state = DroneState.LANDED # 初始状态:落地 + self.battery = 100.0 # 电量(%) + self.max_height = 10.0 # 最大飞行高度(m) + self.min_height = 0.0 # 最小高度(m) + + def takeoff(self): + """起飞(仅落地状态可执行)""" + if self.state == DroneState.LANDED and self.battery > 20: + self.state = DroneState.FLYING + self.position[2] = 1.0 # 起飞到1m高度 + self.battery -= 0.5 # 起飞消耗电量 + print(f"✅ 虚拟无人机起飞 | 当前高度: {self.position[2]:.1f}m") + elif self.battery <= 20: + print("⚠️ 电量不足20%,禁止起飞") + else: + print("⚠️ 无人机已处于飞行状态") + + def land(self): + """降落(仅飞行状态可执行)""" + if self.state == DroneState.FLYING: + self.state = DroneState.LANDED + self.position[2] = 0.0 # 落地 + self.velocity = np.zeros(3) # 速度清零 + self.battery -= 0.2 # 降落消耗电量 + print(f"✅ 虚拟无人机降落 | 最终位置: {self.position[:2]}") + else: + print("⚠️ 无人机已处于落地状态") + + def move(self, direction): + """ + 控制无人机移动 + :param direction: 移动方向(forward/back/left/right/up/down) + """ + if self.state != DroneState.FLYING: + print("⚠️ 无人机未起飞,无法移动") + return + if self.battery <= 0: + print("⚠️ 电量耗尽,无法移动") + return + + # 基于偏航角计算实际移动方向(考虑朝向) + rad_yaw = np.radians(self.yaw) + cos_yaw = np.cos(rad_yaw) + sin_yaw = np.sin(rad_yaw) + + # 重置速度 + self.velocity = np.zeros(3) + + # 方向映射(x: 前后, y: 左右, z: 上下) + if direction == "forward": + self.velocity[0] = self.speed * cos_yaw + self.velocity[1] = self.speed * sin_yaw + elif direction == "back": + self.velocity[0] = -self.speed * cos_yaw + self.velocity[1] = -self.speed * sin_yaw + elif direction == "left": + self.velocity[0] = self.speed * sin_yaw + self.velocity[1] = -self.speed * cos_yaw + elif direction == "right": + self.velocity[0] = -self.speed * sin_yaw + self.velocity[1] = self.speed * cos_yaw + elif direction == "up": + self.velocity[2] = self.speed + elif direction == "down": + self.velocity[2] = -self.speed + + # 更新位置 + new_pos = self.position + self.velocity + # 高度限制 + new_pos[2] = np.clip(new_pos[2], self.min_height, self.max_height) + self.position = new_pos + + # 消耗电量 + self.battery -= 0.1 + self.battery = max(self.battery, 0.0) + + print(f"🔹 移动 {direction} | 位置: {self.position.round(1)} | 电量: {self.battery:.1f}%") + + def rotate(self, direction): + """ + 旋转无人机(偏航角) + :param direction: left/right + """ + if self.state != DroneState.FLYING: + print("⚠️ 无人机未起飞,无法旋转") + return + if direction == "left": + self.yaw += 10.0 # 左转10° + elif direction == "right": + self.yaw -= 10.0 # 右转10° + self.yaw %= 360 # 限制在0-360° + print(f"🔄 旋转 {direction} | 偏航角: {self.yaw:.0f}°") + + +# ===================== 3D可视化+交互控制 ===================== +class DroneSimulator: + def __init__(self): + # 初始化虚拟无人机 + self.drone = VirtualDrone() + # 初始化Pygame(键盘交互) + pygame.init() + # PyCharm适配:禁用Pygame音频(避免无音频设备报错) + pygame.mixer.quit() + self.screen = pygame.display.set_mode((400, 200)) + pygame.display.set_caption("虚拟无人机控制面板") + self.clock = pygame.time.Clock() + + # 初始化PyVista(3D可视化) + self.plotter = pv.Plotter(window_size=(800, 600)) + self.plotter.set_background("lightgray") + # 创建无人机3D模型 + self._create_drone_model() + # 添加地面网格 + self._add_ground_plane() + + def _create_drone_model(self): + """创建简化的无人机3D模型(适配PyCharm+全版本PyVista)""" + # 机身(立方体) + body = pv.Cube(center=(0, 0, 0), x_length=0.5, y_length=0.5, z_length=0.2) + # 螺旋桨(4个圆柱体) + prop1 = pv.Cylinder(center=(0.3, 0.3, 0.1), direction=(1, 0, 0), radius=0.2, height=0.05) + prop2 = pv.Cylinder(center=(-0.3, 0.3, 0.1), direction=(-1, 0, 0), radius=0.2, height=0.05) + prop3 = pv.Cylinder(center=(0.3, -0.3, 0.1), direction=(0, 1, 0), radius=0.2, height=0.05) + prop4 = pv.Cylinder(center=(-0.3, -0.3, 0.1), direction=(0, -1, 0), radius=0.2, height=0.05) + props = pv.MultiBlock([prop1, prop2, prop3, prop4]) + + # 组合无人机模型:适配所有PyVista版本的颜色设置 + self.drone_actor = self.plotter.add_mesh(body, color="darkblue") # 机身颜色 + self.props_actor = self.plotter.add_mesh(props, color="gray") # 螺旋桨颜色 + + # 初始化状态标签(保存actor引用,用于后续删除) + self.label_actor = self.plotter.add_text( + f"Position: (0.0, 0.0, 0.0) | State: Landed | Battery: 100%", + position="upper_left", + font_size=12, + color="black" + ) + + def _add_ground_plane(self): + """添加地面网格""" + ground = pv.Plane(center=(0, 0, 0), direction=(0, 0, 1), i_size=50, j_size=50) + ground.rotate_z(45) # 旋转45°,网格更明显 + self.plotter.add_mesh(ground, color="lightgreen", opacity=0.5) + # 添加坐标轴(PyCharm中更清晰) + self.plotter.add_axes(line_width=2, labels_off=False) + + def _update_3d_view(self): + """更新3D视图(完全适配PyCharm+旧版PyVista)""" + try: + # 更新无人机位置 + self.drone_actor.SetPosition(self.drone.position) + self.props_actor.SetPosition(self.drone.position) + + # 更新无人机旋转(偏航角) + self.drone_actor.RotateZ(self.drone.yaw) + self.props_actor.RotateZ(self.drone.yaw) + + # 更新状态标签:先删旧标签,再加新标签(兼容所有版本) + self.plotter.remove_actor(self.label_actor) + new_label_text = ( + f"Position: {self.drone.position.round(1)} | " + f"State: {self.drone.state.value} | " + f"Battery: {self.drone.battery:.1f}%" + ) + self.label_actor = self.plotter.add_text( + new_label_text, + position="upper_left", + font_size=12, + color="black" + ) + + # 强制刷新视图 + self.plotter.render() + except Exception as e: + print(f"视图更新小警告(不影响使用):{str(e)}") + + def _handle_keyboard(self): + """处理键盘输入(PyCharm焦点适配)""" + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.cleanup() + sys.exit() + if event.type == pygame.KEYDOWN: + # 基础控制 + if event.key == pygame.K_t: + self.drone.takeoff() + elif event.key == pygame.K_l: + self.drone.land() + # 移动控制 + elif event.key == pygame.K_w: + self.drone.move("forward") + elif event.key == pygame.K_s: + self.drone.move("back") + elif event.key == pygame.K_a: + self.drone.move("left") + elif event.key == pygame.K_d: + self.drone.move("right") + elif event.key == pygame.K_UP: + self.drone.move("up") + elif event.key == pygame.K_DOWN: + self.drone.move("down") + # 旋转控制 + elif event.key == pygame.K_q: + self.drone.rotate("left") + elif event.key == pygame.K_e: + self.drone.rotate("right") + # 退出(PyCharm中优雅退出) + elif event.key == pygame.K_ESCAPE: + self.cleanup() + sys.exit() + + def cleanup(self): + """PyCharm优雅退出(释放资源)""" + pygame.quit() + self.plotter.close() + print("\n👋 模拟器已优雅退出") + + def run(self): + """运行模拟器(PyCharm专用优化)""" + print("=" * 60) + print("🎮 PyCharm 2025.2.3 虚拟无人机模拟器") + print("=" * 60) + print("操作说明:") + print(" T → 起飞 | L → 降落 | ESC → 退出") + print(" W/S/A/D → 前/后/左/右 | ↑/↓ → 上升/下降") + print(" Q/E → 左转/右转(偏航角)") + print("⚠️ 注意:先点击Pygame窗口获取键盘焦点") + print("=" * 60) + + # PyVista窗口显示(适配PyCharm的交互模式) + self.plotter.show(interactive_update=True, auto_close=False) + + # 主循环(PyCharm帧率优化) + while True: + self._handle_keyboard() + self._update_3d_view() + self.clock.tick(30) # 稳定30FPS,避免PyCharm卡顿 + + +# ===================== PyCharm 一键运行入口 ===================== +if __name__ == "__main__": + # 第一步:安装依赖(复制到PyCharm终端执行) + # pip install pyvista pygame numpy -i https://pypi.tuna.tsinghua.edu.cn/simple/ + + # 第二步:运行程序 + try: + simulator = DroneSimulator() + simulator.run() + except Exception as e: + print(f"\n❌ 程序运行异常:{str(e)}") + print("\n💡 解决方案:") + print("1. 确保已安装依赖:pip install pyvista pygame numpy -i 清华镜像") + print("2. 关闭PyCharm的\"Power Save Mode\"(省电模式)") + print("3. 以管理员身份运行PyCharm") + # 强制清理资源 + pygame.quit() + sys.exit(1) \ No newline at end of file From cef07c1c63479da5ef183f2f66ef44aad0da3ddf Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Wed, 17 Dec 2025 13:14:08 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=97=A0=E4=BA=BA=E6=9C=BA=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../detection_module.py | 258 ++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/detection_module.py diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/detection_module.py b/src/AI_Drone_Face_Recognition_Human_Tracking/detection_module.py new file mode 100644 index 0000000000..b5976246be --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/detection_module.py @@ -0,0 +1,258 @@ +# -*- coding: utf-8 -*- +""" +无人机检测模块 +功能: +1. 独立运行:模拟检测无人机状态、环境障碍物 +2. 被主程序导入:提供检测接口,返回预警信息 +""" +import numpy as np +import random +from enum import Enum +import time + + +# ===================== 检测类型枚举(便于主程序调用) ===================== +class DetectionType(Enum): + OBSTACLE = "障碍物检测" + BATTERY = "电量检测" + POSITION = "位置检测" + COLLISION = "碰撞预警" + STATE = "状态检测" + + +# ===================== 核心检测类 ===================== +class DroneDetection: + def __init__(self, drone=None): + """ + 初始化检测模块 + :param drone: 可选,传入虚拟无人机对象(主程序调用时传) + """ + self.drone = drone # 关联无人机对象 + self.obstacle_list = self._generate_obstacles() # 生成模拟障碍物 + self.warning_threshold = { + "battery": 20.0, # 电量预警阈值(%) + "height": 0.5, # 高度过低预警阈值(m) + "distance": 1.0 # 障碍物距离预警阈值(m) + } + + def _generate_obstacles(self): + """生成模拟环境障碍物(随机坐标)""" + obstacles = [] + for _ in range(10): # 生成10个随机障碍物 + x = random.uniform(-10, 10) + y = random.uniform(-10, 10) + z = random.uniform(0, 8) + obstacles.append(np.array([x, y, z])) + return obstacles + + def detect_obstacle(self): + """ + 障碍物检测:计算无人机与最近障碍物的距离 + :return: dict - 检测结果(距离、是否预警、最近障碍物坐标) + """ + if self.drone is None: + return { + "type": DetectionType.OBSTACLE.value, + "status": "未关联无人机", + "distance": None, + "warning": False, + "nearest_obstacle": None + } + + # 计算无人机与每个障碍物的欧氏距离 + drone_pos = self.drone.position + distances = [np.linalg.norm(drone_pos - obs) for obs in self.obstacle_list] + min_distance = min(distances) + nearest_idx = np.argmin(distances) + nearest_obs = self.obstacle_list[nearest_idx] + + # 判断是否触发预警 + warning = min_distance < self.warning_threshold["distance"] + + return { + "type": DetectionType.OBSTACLE.value, + "status": "检测完成", + "distance": round(min_distance, 2), + "warning": warning, + "nearest_obstacle": nearest_obs.round(2), + "message": f"⚠️ 距离障碍物仅{min_distance:.2f}m,请注意避让!" if warning else "✅ 无障碍物风险" + } + + def detect_battery(self): + """ + 电量检测:判断是否低于预警阈值 + :return: dict - 检测结果 + """ + if self.drone is None: + return { + "type": DetectionType.BATTERY.value, + "status": "未关联无人机", + "battery": None, + "warning": False, + "message": "未关联无人机,无法检测电量" + } + + battery = self.drone.battery + warning = battery < self.warning_threshold["battery"] + + return { + "type": DetectionType.BATTERY.value, + "status": "检测完成", + "battery": round(battery, 2), + "warning": warning, + "message": f"⚠️ 电量低({battery:.1f}%),请尽快返航!" if warning else f"✅ 电量充足({battery:.1f}%)" + } + + def detect_position(self): + """ + 位置检测:判断高度是否过低/超出边界 + :return: dict - 检测结果 + """ + if self.drone is None: + return { + "type": DetectionType.POSITION.value, + "status": "未关联无人机", + "position": None, + "warning": False, + "message": "未关联无人机,无法检测位置" + } + + pos = self.drone.position + # 兼容主程序枚举状态和独立运行字符串状态 + drone_state = self.drone.state.value if hasattr(self.drone.state, "value") else self.drone.state + height_warning = pos[2] < self.warning_threshold["height"] and drone_state == "FLYING" + boundary_warning = abs(pos[0]) > 15 or abs(pos[1]) > 15 # 水平边界±15m + + warning = height_warning or boundary_warning + messages = [] + if height_warning: + messages.append(f"⚠️ 飞行高度过低({pos[2]:.1f}m),请注意!") + if boundary_warning: + messages.append(f"⚠️ 超出安全边界(坐标:{pos[:2].round(1)}),请返航!") + if not warning: + messages.append(f"✅ 位置正常({pos.round(1)})") + + return { + "type": DetectionType.POSITION.value, + "status": "检测完成", + "position": pos.round(2), + "warning": warning, + "message": " | ".join(messages) + } + + def detect_collision(self): + """ + 碰撞预警:预测未来1秒是否有碰撞风险 + :return: dict - 检测结果 + """ + if self.drone is None: + return { + "type": DetectionType.COLLISION.value, + "status": "未关联无人机", + "risk": False, + "message": "未关联无人机,无法预测碰撞风险" + } + + # 预测1秒后无人机位置(基于当前速度) + future_pos = self.drone.position + self.drone.velocity * 1.0 + # 计算与障碍物的距离 + distances = [np.linalg.norm(future_pos - obs) for obs in self.obstacle_list] + min_future_dist = min(distances) + risk = min_future_dist < 0.5 # 距离<0.5m判定为碰撞风险 + + return { + "type": DetectionType.COLLISION.value, + "status": "检测完成", + "risk": risk, + "future_position": future_pos.round(2), + "message": "🚨 1秒后有碰撞风险!请立即调整方向!" if risk else "✅ 无碰撞风险" + } + + def detect_state(self): + """ + 状态检测:判断无人机当前状态是否正常 + :return: dict - 检测结果 + """ + if self.drone is None: + return { + "type": DetectionType.STATE.value, + "status": "未关联无人机", + "drone_state": None, + "message": "未关联无人机,无法检测状态" + } + + # 兼容主程序枚举状态(DroneState)和独立运行字符串状态 + state = self.drone.state.value if hasattr(self.drone.state, "value") else self.drone.state + if state == "LANDED": + message = "✅ 无人机处于落地状态,状态正常" + elif state == "FLYING" and self.drone.battery > 10: + message = "✅ 无人机处于飞行状态,状态正常" + else: + message = f"⚠️ 无人机飞行状态异常(电量{self.drone.battery:.1f}%)" + + return { + "type": DetectionType.STATE.value, + "status": "检测完成", + "drone_state": state, + "warning": state == "FLYING" and self.drone.battery <= 10, + "message": message + } + + def full_detection(self): + """ + 全量检测:执行所有检测项 + :return: list - 所有检测结果 + """ + results = [ + self.detect_state(), + self.detect_battery(), + self.detect_position(), + self.detect_obstacle(), + self.detect_collision() + ] + return results + + +# ===================== 独立运行测试代码 ===================== +def main(): + """模块独立运行时的测试逻辑""" + print("=" * 60) + print("📊 无人机检测模块 - 独立测试模式") + print("=" * 60) + + # 模拟无人机对象(独立运行时使用,简化状态为字符串,避免枚举类错误) + class MockDrone: + def __init__(self): + self.position = np.array([2.0, 1.5, 1.0]) # 模拟位置 + self.velocity = np.array([0.8, 0.5, 0.0]) # 模拟速度 + self.state = "FLYING" # 直接用字符串标识状态,替代枚举类 + self.battery = 18.5 # 模拟电量 + + # 初始化检测模块 + mock_drone = MockDrone() + detector = DroneDetection(drone=mock_drone) + + # 循环检测(模拟实时监测) + try: + while True: + print(f"\n⏰ 检测时间:{time.strftime('%Y-%m-%d %H:%M:%S')}") + # 执行全量检测 + all_results = detector.full_detection() + for res in all_results: + print(f"[{res['type']}] {res['message']}") + + # 模拟无人机位置变化 + mock_drone.position += mock_drone.velocity * 0.5 + mock_drone.battery -= 0.5 # 模拟电量消耗 + # 模拟飞行状态切换(每10秒落地一次) + if mock_drone.battery < 10: + mock_drone.state = "LANDED" + time.sleep(2) # 每2秒检测一次 + + except KeyboardInterrupt: + print("\n\n🛑 检测模块独立测试已停止") + + +# 仅当模块独立运行时执行测试 +if __name__ == "__main__": + main() \ No newline at end of file