From 5914479862b2cc11469306f68be22fcc876e5da6 Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Thu, 18 Dec 2025 17:08:27 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=92=8C=E8=80=81=E5=B8=88=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E5=90=8E=EF=BC=8C=E5=89=8D=E9=9D=A2?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A2=AB=E5=88=A0=E4=BA=86=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=90=8C=E6=AD=A5=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../detection_module.py | 259 ++++++++++++++ .../drone_one.py | 0 .../face_database.py | 336 ++++++++++++++++++ .../main.py | 90 +++++ .../map_overlay.py | 150 ++++++++ .../utils.py | 34 ++ 6 files changed, 869 insertions(+) create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/detection_module.py create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/drone_one.py create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/face_database.py create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/main.py create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/map_overlay.py create mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/utils.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..73199c593c --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/detection_module.py @@ -0,0 +1,259 @@ +'''检测模块''' +# -*- 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 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/drone_one.py b/src/AI_Drone_Face_Recognition_Human_Tracking/drone_one.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/face_database.py b/src/AI_Drone_Face_Recognition_Human_Tracking/face_database.py new file mode 100644 index 0000000000..51ff11fe20 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/face_database.py @@ -0,0 +1,336 @@ +from ultralytics import YOLO +import cv2 +import numpy as np +import os +import json +from typing import Optional, Tuple, List, Dict + + +class FaceDatabase: + # 【复用之前的FaceDatabase类代码,此处省略(保持不变)】 + def __init__(self, data_dir: str = "face_database", threshold: float = 0.6): + self.data_dir = data_dir + self.feat_dir = os.path.join(data_dir, "features") + self.meta_dir = os.path.join(data_dir, "metadata") + self.threshold = threshold + os.makedirs(self.data_dir, exist_ok=True) + os.makedirs(self.feat_dir, exist_ok=True) + os.makedirs(self.meta_dir, exist_ok=True) + self.face_features: Dict[str, np.ndarray] = {} + self.face_metadata: Dict[str, dict] = {} + + @staticmethod + def preprocess_face(face_roi: np.ndarray) -> Optional[np.ndarray]: + if face_roi.size == 0: + return None + gray = cv2.cvtColor(face_roi, cv2.COLOR_BGR2GRAY) + gray = cv2.resize(gray, (128, 128)) + gray = cv2.equalizeHist(gray) + feature = gray.flatten() / 255.0 + return feature + + def save_face(self, name: str, face_roi: np.ndarray, remark: str = "", overwrite: bool = True) -> bool: + feature = self.preprocess_face(face_roi) + if feature is None: + print(f"❌ 人脸预处理失败,无法保存{name}") + return False + feat_path = os.path.join(self.feat_dir, f"{name}.npy") + meta_path = os.path.join(self.meta_dir, f"{name}.json") + if os.path.exists(feat_path) and not overwrite: + print(f"⚠️ {name}已存在,跳过保存(如需覆盖请设置overwrite=True)") + return False + np.save(feat_path, feature) + metadata = { + "name": name, + "remark": remark, + "feature_shape": feature.shape, + "add_time": str(np.datetime64('now')), + "version": "1.0" + } + with open(meta_path, "w", encoding="utf-8") as f: + json.dump(metadata, f, ensure_ascii=False, indent=2) + self.face_features[name] = feature + self.face_metadata[name] = metadata + print(f"✅ {name}人脸数据保存成功") + return True + + def load_face(self, name: str) -> bool: + feat_path = os.path.join(self.feat_dir, f"{name}.npy") + meta_path = os.path.join(self.meta_dir, f"{name}.json") + if not os.path.exists(feat_path) or not os.path.exists(meta_path): + print(f"❌ {name}人脸数据不存在") + return False + feature = np.load(feat_path) + with open(meta_path, "r", encoding="utf-8") as f: + metadata = json.load(f) + self.face_features[name] = feature + self.face_metadata[name] = metadata + print(f"✅ 加载{name}人脸数据成功") + return True + + def load_all_faces(self) -> int: + loaded_count = 0 + for feat_file in os.listdir(self.feat_dir): + if not feat_file.endswith(".npy"): + continue + name = os.path.splitext(feat_file)[0] + if self.load_face(name): + loaded_count += 1 + print(f"📊 批量加载完成,共加载{loaded_count}个人脸数据") + return loaded_count + + def delete_face(self, name: str) -> bool: + feat_path = os.path.join(self.feat_dir, f"{name}.npy") + meta_path = os.path.join(self.meta_dir, f"{name}.json") + for path in [feat_path, meta_path]: + if os.path.exists(path): + os.remove(path) + if name in self.face_features: + del self.face_features[name] + if name in self.face_metadata: + del self.face_metadata[name] + print(f"🗑️ {name}人脸数据已删除") + return True + + def list_faces(self) -> List[str]: + face_list = [os.path.splitext(f)[0] for f in os.listdir(self.feat_dir) if f.endswith(".npy")] + print(f"📋 已保存的人脸列表:{face_list}") + return face_list + + @staticmethod + def calculate_similarity(feature1: np.ndarray, feature2: np.ndarray) -> float: + if len(feature1) != len(feature2): + return 0.0 + dot = np.dot(feature1, feature2) + norm1 = np.linalg.norm(feature1) + norm2 = np.linalg.norm(feature2) + return dot / (norm1 * norm2) if (norm1 * norm2) != 0 else 0.0 + + def match_face(self, face_roi: np.ndarray) -> Optional[str]: + query_feat = self.preprocess_face(face_roi) + if query_feat is None or not self.face_features: + return None + max_sim = 0.0 + matched_name = None + for name, feat in self.face_features.items(): + sim = self.calculate_similarity(query_feat, feat) + if sim > max_sim and sim > self.threshold: + max_sim = sim + matched_name = name + return matched_name + + +class DetectionEngine: + # 【复用之前的DetectionEngine类代码,此处省略(保持不变)】 + def __init__(self, + model_path: str = "yolov8n.pt", + conf_thres: float = 0.5, + track_thres: float = 0.4, + is_face_model: bool = False): + self.model = YOLO(model_path) + self.conf_thres = conf_thres + self.track_thres = track_thres + self.class_names = self.model.names + self.human_class_id = 0 + self.face_class_id = 0 if is_face_model else None + + def detect(self, frame: np.ndarray) -> List: + if frame is None or frame.size == 0: + return [] + results = self.model( + frame, + conf=self.conf_thres, + iou=self.track_thres, + show=False, + verbose=False + ) + return results + + def get_largest_human(self, results: List) -> Optional[Tuple[int, int, int, int]]: + largest_bbox = None + max_area = 0 + for r in results: + if not hasattr(r, 'boxes') or r.boxes is None: + continue + for box in r.boxes: + cls = int(box.cls[0]) + if cls == self.human_class_id: + x1, y1, x2, y2 = map(int, box.xyxy[0]) + x1 = max(0, x1) + y1 = max(0, y1) + x2 = min(frame.shape[1], x2) + y2 = min(frame.shape[0], y2) + area = (x2 - x1) * (y2 - y1) + if area > max_area and area > 100: + max_area = area + largest_bbox = (x1, y1, x2, y2) + return largest_bbox + + def match_faces(self, frame: np.ndarray, results: List, face_db: FaceDatabase) -> np.ndarray: + frame_copy = frame.copy() + h, w = frame_copy.shape[:2] + for r in results: + if not hasattr(r, 'boxes') or r.boxes is None: + continue + for box in r.boxes: + cls = int(box.cls[0]) + conf = float(box.conf[0]) + x1, y1, x2, y2 = map(int, box.xyxy[0]) + x1 = max(0, x1) + y1 = max(0, y1) + x2 = min(w, x2) + y2 = min(h, y2) + if self.face_class_id is not None and cls == self.face_class_id: + face_roi = frame_copy[y1:y2, x1:x2] + name = face_db.match_face(face_roi) or "未知人脸" + frame_copy = self.draw_detection_box(frame_copy, (x1, y1, x2, y2), name, conf) + elif cls == self.human_class_id: + frame_copy = self.draw_detection_box(frame_copy, (x1, y1, x2, y2), "人体", conf) + return frame_copy + + @staticmethod + def draw_detection_box(frame: np.ndarray, + bbox: Tuple[int, int, int, int], + label: str, + confidence: float) -> np.ndarray: + x1, y1, x2, y2 = bbox + cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) + label_text = f"{label} ({confidence:.2f})" + font = cv2.FONT_HERSHEY_SIMPLEX + text_size, _ = cv2.getTextSize(label_text, font, 0.6, 1) + text_w, text_h = text_size + bg_x1, bg_y1 = x1, max(y1 - text_h - 10, 0) + bg_x2, bg_y2 = x1 + text_w, y1 + cv2.rectangle(frame, (bg_x1, bg_y1), (bg_x2, bg_y2), (0, 255, 0), -1) + cv2.putText(frame, label_text, (x1, bg_y1 + text_h + 2), + font, 0.6, (255, 255, 255), 1) + return frame + + +# ------------------------------ +# 主程序(集成按键操作) +# ------------------------------ +if __name__ == "__main__": + # 1. 初始化组件 + engine = DetectionEngine(model_path="yolov8n.pt", is_face_model=False) # 如需人脸检测,切换为yolov8n-face.pt + face_db = FaceDatabase(data_dir="my_face_db", threshold=0.6) + face_db.load_all_faces() # 启动时加载所有已保存的人脸 + + # 2. 初始化状态变量 + cap = cv2.VideoCapture(0) + is_paused = False # 暂停状态 + show_visual = True # 可视化检测框 + screenshot_dir = "screenshots" + os.makedirs(screenshot_dir, exist_ok=True) + + # 3. 打印操作提示 + print("=" * 50) + print("📱 操作按键说明:") + print(" q - 退出程序") + print(" s - 保存当前最大人脸到库(需先检测到人脸)") + print(" d - 删除指定姓名的人脸数据") + print(" l - 重新加载所有人脸数据") + print(" v - 切换检测框可视化(显示/隐藏)") + print(" p - 暂停/继续实时检测") + print(" f - 保存当前帧截图到screenshots目录") + print(" t - 调整人脸匹配阈值(0~1)") + print("=" * 50) + + while cap.isOpened(): + # 暂停状态下只处理按键,不读取帧 + if not is_paused: + ret, frame = cap.read() + if not ret: + print("❌ 无法读取摄像头画面,程序退出") + break + + # 执行检测 + results = engine.detect(frame) + largest_human_bbox = engine.get_largest_human(results) + if largest_human_bbox: + print(f"🔍 检测到最大人体:{largest_human_bbox}", end="\r") # 实时打印(覆盖行) + + # 绘制检测框(根据可视化状态) + if show_visual: + frame_display = engine.match_faces(frame, results, face_db) + else: + frame_display = frame.copy() + else: + frame_display = frame.copy() # 暂停时保持最后一帧 + + # 显示画面 + cv2.imshow("🤖 人体/人脸检测系统", frame_display) + + # 按键响应(非阻塞,等待1ms) + key = cv2.waitKey(1) & 0xFF + if key == ord('q'): + # 退出程序 + print("\n👋 程序正常退出") + break + elif key == ord('p'): + # 暂停/继续 + is_paused = not is_paused + status = "暂停" if is_paused else "继续" + print(f"\n⏸️ 检测已{status}") + elif key == ord('v'): + # 切换可视化 + show_visual = not show_visual + status = "显示" if show_visual else "隐藏" + print(f"\n🎨 检测框已{status}") + elif key == ord('f'): + # 保存截图 + screenshot_path = os.path.join(screenshot_dir, f"screenshot_{np.datetime64('now').astype(str)}.png") + cv2.imwrite(screenshot_path, frame_display) + print(f"\n📸 截图已保存:{screenshot_path}") + elif key == ord('l'): + # 重新加载人脸 + face_db.load_all_faces() + elif key == ord('d'): + # 删除人脸(控制台输入姓名) + del_name = input("\n🗑️ 请输入要删除的人脸姓名:") + face_db.delete_face(del_name) + elif key == ord('t'): + # 调整阈值 + try: + new_thresh = float(input("\n🎛️ 请输入新的人脸匹配阈值(0~1):")) + if 0 <= new_thresh <= 1: + face_db.threshold = new_thresh + print(f"✅ 阈值已更新为:{new_thresh}") + else: + print("❌ 阈值需在0~1之间") + except ValueError: + print("❌ 输入无效,请输入数字") + elif key == ord('s'): + # 保存当前最大人脸(优先人脸检测,无则取人体框内的人脸区域) + save_name = input("\n💾 请输入要保存的人脸姓名:") + save_remark = input("📝 请输入备注(可选):") + + # 尝试获取人脸ROI(优先检测到的人脸,无则取最大人体的中间区域) + face_roi = None + results = engine.detect(frame) + # 方式1:如果用了人脸模型,提取第一个检测到的人脸 + if engine.face_class_id is not None: + for r in results: + if hasattr(r, 'boxes') and r.boxes is not None: + for box in r.boxes: + if int(box.cls[0]) == engine.face_class_id: + x1, y1, x2, y2 = map(int, box.xyxy[0]) + face_roi = frame[y1:y2, x1:x2] + break + # 方式2:无检测人脸时,取最大人体的上半部分(人脸区域) + if face_roi is None: + largest_human = engine.get_largest_human(results) + if largest_human: + x1, y1, x2, y2 = largest_human + # 截取人体上半部分作为人脸ROI(需手动调整比例) + face_h = int((y2 - y1) * 0.3) + face_roi = frame[y1:y1 + face_h, x1:x2] + + if face_roi is not None and face_roi.size > 0: + face_db.save_face(save_name, face_roi, save_remark, overwrite=True) + else: + print("❌ 未检测到有效人脸区域,保存失败") + + # 释放资源 + cap.release() + cv2.destroyAllWindows() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/main.py b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py new file mode 100644 index 0000000000..52bf16264f --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py @@ -0,0 +1,90 @@ +import cv2 +import argparse +from drone_control import TelloDrone +from detection_module import DetectionEngine +from map_overlay import MapOverlay +from face_database import FaceDatabase + + +def parse_args(): + """解析命令行参数""" + parser = argparse.ArgumentParser(description="AI无人机面部识别与人体追踪") + parser.add_argument("--yolo-model", default="yolov8n.pt", help="YOLO模型路径") + parser.add_argument("--conf-thres", type=float, default=0.5, help="检测置信度阈值") + parser.add_argument("--track-thres", type=float, default=0.4, help="追踪IOU阈值") + parser.add_argument("--map-path", default="map.png", help="地图图片路径") + parser.add_argument("--map-alpha", type=float, default=0.3, help="地图透明度") + return parser.parse_args() + + +def main(): + args = parse_args() + + # 初始化核心模块 + drone = TelloDrone() # 无人机控制 + detector = DetectionEngine( + model_path=args.yolo_model, + conf_thres=args.conf_thres, + track_thres=args.track_thres + ) # 检测引擎 + face_db = FaceDatabase(db_path="face_database/") # 人脸数据库 + map_overlay = MapOverlay(map_path=args.map_path, alpha=args.map_alpha) # 地图叠加 + + # 加载人脸库 + face_db.load_all_faces() + print(f"人脸库加载完成,共{len(face_db.get_face_names())}个人脸") + + # 无人机连接与视频流启动 + if not drone.connect(): + print("无人机连接失败!") + return + drone.start_video_stream() + print(f"无人机电量:{drone.get_battery()}%") + + try: + while True: + # 获取无人机视频帧 + frame = drone.get_frame() + if frame is None: + continue + + # 1. 检测人脸/人体 + results = detector.detect(frame) + # 2. 人脸匹配 + frame = detector.match_faces(frame, results, face_db) + # 3. 人体追踪(可选:追踪最大的人体目标) + track_target = detector.get_largest_human(results) + if track_target: + # 无人机追踪逻辑(简化版:根据目标位置调整云台/飞行) + drone.track_target(frame, track_target) + + # 4. 叠加地图 + frame = map_overlay.overlay(frame) + + # 显示画面 + cv2.imshow("AI Drone Face & Human Tracking", frame) + + # 按键控制 + key = cv2.waitKey(1) & 0xFF + if key == ord('q'): # 退出 + break + elif key == ord('t'): # 起飞 + drone.takeoff() + elif key == ord('l'): # 降落 + drone.land() + elif key == ord('s'): # 保存画面 + cv2.imwrite(f"drone_capture_{cv2.getTickCount()}.jpg", frame) + print("画面已保存") + + except Exception as e: + print(f"程序异常:{str(e)}") + finally: + # 资源释放 + drone.stop_video_stream() + drone.land() # 紧急降落 + cv2.destroyAllWindows() + print("程序正常退出") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/map_overlay.py b/src/AI_Drone_Face_Recognition_Human_Tracking/map_overlay.py new file mode 100644 index 0000000000..37bdfc34f9 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/map_overlay.py @@ -0,0 +1,150 @@ +'''地图叠加模块''' +import cv2 +import numpy as np + + +class MapOverlay: + def __init__(self, map_path="map.png", alpha=0.3): + self.map_path = map_path + self.alpha = np.clip(alpha, 0.0, 1.0) + self.map_img = self.load_map() + + def load_map(self): + """加载地图,失败则生成默认地图""" + try: + map_img = cv2.imread(self.map_path) + if map_img is None: + raise ValueError("地图图片读取失败") + return map_img + except Exception as e: + print(f"加载地图失败:{e},使用默认地图") + # 生成默认地图(带网格的简易地图) + default_map = np.ones((200, 300, 3), dtype=np.uint8) * 240 + # 绘制网格 + for x in range(0, 300, 30): + cv2.line(default_map, (x, 0), (x, 200), (200, 200, 200), 1) + for y in range(0, 200, 20): + cv2.line(default_map, (0, y), (300, y), (200, 200, 200), 1) + cv2.putText(default_map, "DEFAULT MAP", (20, 100), + cv2.FONT_HERSHEY_SIMPLEX, 0.8, (50, 50, 50), 2) + return default_map + + def overlay(self, frame): + """将地图叠加到帧的右上角""" + if frame is None or self.map_img is None: + return frame + + h, w = frame.shape[:2] + map_h, map_w = self.map_img.shape[:2] + + # 缩放地图(占视频宽度的25%) + target_w = int(w * 0.25) + scale = target_w / map_w + target_h = int(map_h * scale) + resized_map = cv2.resize(self.map_img, (target_w, target_h)) + + # 计算叠加位置(右上角,留10像素边距) + x_start = w - target_w - 10 + y_start = 10 + x_end = x_start + target_w + y_end = y_start + target_h + + # 边界校验(防止地图超出视频帧范围) + x_end = min(x_end, w) + y_end = min(y_end, h) + resized_map = resized_map[:y_end - y_start, :x_end - x_start] + + # 透明度混合 + roi = frame[y_start:y_end, x_start:x_end] + blended = cv2.addWeighted(resized_map, self.alpha, roi, 1 - self.alpha, 0) + frame[y_start:y_end, x_start:x_end] = blended + + # 绘制无人机位置标记(地图中心) + map_center_x = x_start + (x_end - x_start) // 2 + map_center_y = y_start + (y_end - y_start) // 2 + cv2.circle(frame, (map_center_x, map_center_y), 5, (0, 0, 255), -1) + cv2.putText(frame, "Drone", (map_center_x + 10, map_center_y), + cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) + + return frame + + def adjust_alpha(self, delta): + """调整地图透明度""" + old_alpha = self.alpha + self.alpha = np.clip(self.alpha + delta, 0.0, 1.0) + if old_alpha != self.alpha: # 仅在透明度变化时打印 + print(f"✅ 地图透明度调整为:{self.alpha:.1f}") + else: + print(f"⚠️ 透明度已达极限({self.alpha:.1f}),无法继续调整") + + +def main(): + # 初始化地图叠加器 + map_overlay = MapOverlay(alpha=0.4) + + # 选择视频源:0=摄像头,也可以替换为视频文件路径(如"test.mp4") + video_source = 0 + cap = cv2.VideoCapture(video_source) + + # 检查视频源是否打开 + if not cap.isOpened(): + print(f"❌ 无法打开视频源:{video_source}") + return + + # 设置窗口可调整大小(方便操作) + cv2.namedWindow("Map Overlay Demo", cv2.WINDOW_NORMAL) + + print("=" * 60) + print("📢 操作说明:") + print(" W / 上方向键:增加地图透明度(+0.1)") + print(" S / 下方向键:降低地图透明度(-0.1)") + print(" Q / ESC键 :退出程序") + print("=" * 60) + + # 主循环 + while True: + # 读取视频帧 + ret, frame = cap.read() + if not ret: + print("❌ 视频流已结束或读取失败") + break + + # 叠加地图 + frame_with_map = map_overlay.overlay(frame) + + # 显示操作提示和当前透明度 + cv2.putText(frame_with_map, "W/S: Adjust Alpha | Q/ESC: Quit", (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) + cv2.putText(frame_with_map, f"Alpha: {map_overlay.alpha:.1f}", (10, 70), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 0), 2) + + # 显示结果 + cv2.imshow("Map Overlay Demo", frame_with_map) + + # 键盘交互(优化跨平台按键检测,等待时间调整为30ms提高响应性) + key = cv2.waitKey(30) & 0xFF + + # 退出逻辑(Q键 / ESC键) + if key == ord('q') or key == ord('Q') or key == 27: + print("📤 退出程序") + break + + # 增加透明度(W键 / 上方向键) + elif key == ord('w') or key == ord('W') or key == 82 or key == 104: + map_overlay.adjust_alpha(0.1) + + # 降低透明度(S键 / 下方向键) + elif key == ord('s') or key == ord('S') or key == 84 or key == 101: + map_overlay.adjust_alpha(-0.1) + + # 调试:打印未知按键编码(方便排查问题) + elif key != 255: + print(f"🔍 检测到未映射按键,编码:{key}") + + # 释放资源 + cap.release() + cv2.destroyAllWindows() + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py b/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py new file mode 100644 index 0000000000..1dca43f414 --- /dev/null +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py @@ -0,0 +1,34 @@ +'''工具模块''' +import cv2 +import numpy as np + +def preprocess_face(face_img): + """人脸预处理:灰度化+均衡化+归一化""" + # 灰度化 + if len(face_img.shape) == 3: + face_gray = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY) + else: + face_gray = face_img + # 直方图均衡化 + face_eq = cv2.equalizeHist(face_gray) + # 归一化尺寸 + face_resized = cv2.resize(face_eq, (100, 100)) + # 归一化像素值 + face_normalized = face_resized / 255.0 + return face_normalized + +def calculate_similarity(face1, face2): + """计算人脸相似度(余弦相似度)""" + try: + # 展平为一维向量 + vec1 = face1.flatten() + vec2 = face2.flatten() + # 余弦相似度 + dot = np.dot(vec1, vec2) + norm1 = np.linalg.norm(vec1) + norm2 = np.linalg.norm(vec2) + sim = dot / (norm1 * norm2) if (norm1 * norm2) != 0 else 0.0 + return sim + except Exception as e: + print(f"计算相似度失败:{e}") + return 0.0 \ No newline at end of file From 278db8071a4cfb046e04380703781fa7ca57a08b Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Thu, 18 Dec 2025 21:23:30 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86readme=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/README.md b/src/AI_Drone_Face_Recognition_Human_Tracking/README.md index bdca2970c7..56707563fe 100644 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/README.md +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/README.md @@ -1 +1,14 @@ -该项目是一个基于人工智能的无人机控制系统,能够用YOLOv8检测图像中的人物,用OpenCV检测人脸,用DeepFace识别人,并能飞近用户从视频流中选定的目标。 \ No newline at end of file +该项目是一个基于人工智能的无人机控制系统,能够用YOLOv8检测图像中的人物,用OpenCV检测人脸,用DeepFace识别人,并能飞近用户从视频流中选定的目标。 + +AI_Drone_Face_Tracking/ +├── main.py # 主程序(无人机控制+检测+追踪) +├── drone_control.py # 无人机底层控制封装 +├── detection_module.py # YOLO检测+人脸匹配模块 +├── map_overlay.py # 地图叠加模块(优化版) +├── face_database.py # 人脸数据库管理 +├── utils.py # 通用工具函数 +├── requirements.txt # 依赖清单 +├── map.png # 地图图片(自行放入) +└── face_database/ # 人脸库文件夹 + ├── person1.jpg + └── person2.jpg \ No newline at end of file From 06ee0da9645189e868168000f84db0ee233d3680 Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Fri, 19 Dec 2025 10:41:54 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E4=B8=BB?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=9A=84=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../drone_control.py | 15 +- .../drone_one.py | 0 .../main.py | 132 ++++++++------ .../overlay_map.py | 170 ----------------- .../utils.py.py | 160 ---------------- .../voice_module.py | 172 ------------------ 6 files changed, 93 insertions(+), 556 deletions(-) delete mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/drone_one.py delete mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py delete mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py delete mode 100644 src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.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 index d2709c0001..6f01baec22 100644 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/drone_control.py +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/drone_control.py @@ -1,4 +1,4 @@ -"""无人机控制模块""" +'''无人机控制模块''' import pyvista as pv import pygame import numpy as np @@ -29,6 +29,10 @@ def __init__(self): self.max_height = 10.0 # 最大飞行高度(m) self.min_height = 0.0 # 最小高度(m) + def get_battery(self): + """新增:获取当前电量(适配主程序调用)""" + return self.battery + def takeoff(self): """起飞(仅落地状态可执行)""" if self.state == DroneState.LANDED and self.battery > 20: @@ -117,6 +121,15 @@ def rotate(self, direction): self.yaw %= 360 # 限制在0-360° print(f"🔄 旋转 {direction} | 偏航角: {self.yaw:.0f}°") + # 可选:补充主程序可能用到的扩展方法 + def get_position(self): + """获取当前位置(方便主程序调用)""" + return self.position + + def get_state(self): + """获取当前状态(返回字符串,适配主程序)""" + return self.state.value + # ===================== 3D可视化+交互控制 ===================== class DroneSimulator: diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/drone_one.py b/src/AI_Drone_Face_Recognition_Human_Tracking/drone_one.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/main.py b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py index 52bf16264f..fcba515a92 100644 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/main.py +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py @@ -1,18 +1,20 @@ import cv2 import argparse -from drone_control import TelloDrone -from detection_module import DetectionEngine -from map_overlay import MapOverlay -from face_database import FaceDatabase +import numpy as np +from drone_control import VirtualDrone # 导入正确的无人机类 +from detection_module import DroneDetection # 导入正确的检测类 + + +# 注:MapOverlay/FaceDatabase为自定义模块,若未实现先注释,避免运行报错 +# from map_overlay import MapOverlay +# from face_database import FaceDatabase def parse_args(): """解析命令行参数""" - parser = argparse.ArgumentParser(description="AI无人机面部识别与人体追踪") - parser.add_argument("--yolo-model", default="yolov8n.pt", help="YOLO模型路径") + parser = argparse.ArgumentParser(description="AI无人机面部识别与人体追踪(虚拟版)") parser.add_argument("--conf-thres", type=float, default=0.5, help="检测置信度阈值") parser.add_argument("--track-thres", type=float, default=0.4, help="追踪IOU阈值") - parser.add_argument("--map-path", default="map.png", help="地图图片路径") parser.add_argument("--map-alpha", type=float, default=0.3, help="地图透明度") return parser.parse_args() @@ -20,51 +22,57 @@ def parse_args(): def main(): args = parse_args() - # 初始化核心模块 - drone = TelloDrone() # 无人机控制 - detector = DetectionEngine( - model_path=args.yolo_model, - conf_thres=args.conf_thres, - track_thres=args.track_thres - ) # 检测引擎 - face_db = FaceDatabase(db_path="face_database/") # 人脸数据库 - map_overlay = MapOverlay(map_path=args.map_path, alpha=args.map_alpha) # 地图叠加 - - # 加载人脸库 - face_db.load_all_faces() - print(f"人脸库加载完成,共{len(face_db.get_face_names())}个人脸") - - # 无人机连接与视频流启动 - if not drone.connect(): - print("无人机连接失败!") - return - drone.start_video_stream() - print(f"无人机电量:{drone.get_battery()}%") + # ===================== 初始化核心模块(适配虚拟无人机) ===================== + # 1. 初始化虚拟无人机(替换原TelloDrone) + drone = VirtualDrone() + # 2. 初始化检测模块(关联无人机,替换原DetectionEngine) + detector = DroneDetection(drone=drone) + # 3. 人脸数据库/地图叠加(未实现则注释,后续可补充) + # face_db = FaceDatabase(db_path="face_database/") + # map_overlay = MapOverlay(map_path=args.map_path, alpha=args.map_alpha) + + # ===================== 模拟初始化逻辑 ===================== + # 加载人脸库(注释,待实现FaceDatabase后启用) + # face_db.load_all_faces() + # print(f"人脸库加载完成,共{len(face_db.get_face_names())}个人脸") + + # 虚拟无人机无需真实连接,模拟启动视频流 + print("✅ 虚拟无人机初始化完成") + print(f"初始电量:{drone.get_battery()}% | 初始状态:{drone.state.value}") + # ===================== 主循环(适配虚拟无人机逻辑) ===================== try: + # 创建虚拟视频窗口(模拟无人机视频流) + cv2.namedWindow("AI Drone Face & Human Tracking", cv2.WINDOW_NORMAL) + while True: - # 获取无人机视频帧 - frame = drone.get_frame() - if frame is None: - continue - - # 1. 检测人脸/人体 - results = detector.detect(frame) - # 2. 人脸匹配 - frame = detector.match_faces(frame, results, face_db) - # 3. 人体追踪(可选:追踪最大的人体目标) - track_target = detector.get_largest_human(results) - if track_target: - # 无人机追踪逻辑(简化版:根据目标位置调整云台/飞行) - drone.track_target(frame, track_target) - - # 4. 叠加地图 - frame = map_overlay.overlay(frame) - - # 显示画面 + # 1. 生成虚拟帧(替代真实无人机视频帧) + frame = np.zeros((480, 640, 3), dtype=np.uint8) + # 在虚拟帧上绘制无人机状态 + cv2.putText(frame, f"Battery: {drone.get_battery():.1f}%", (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + cv2.putText(frame, f"Position: {drone.position.round(1)}", (10, 70), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + cv2.putText(frame, f"State: {drone.state.value}", (10, 110), + cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) + + # 2. 执行无人机状态检测(替代真实人脸/人体检测) + detection_results = detector.full_detection() + # 在帧上绘制检测预警信息 + y_offset = 150 + for res in detection_results: + color = (0, 0, 255) if res.get("warning") or res.get("risk") else (0, 255, 0) + cv2.putText(frame, res["message"], (10, y_offset), + cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2) + y_offset += 30 + + # 3. 地图叠加(注释,待实现MapOverlay后启用) + # frame = map_overlay.overlay(frame) + + # 4. 显示画面 cv2.imshow("AI Drone Face & Human Tracking", frame) - # 按键控制 + # ===================== 按键控制(适配虚拟无人机) ===================== key = cv2.waitKey(1) & 0xFF if key == ord('q'): # 退出 break @@ -74,16 +82,34 @@ def main(): drone.land() elif key == ord('s'): # 保存画面 cv2.imwrite(f"drone_capture_{cv2.getTickCount()}.jpg", frame) - print("画面已保存") + print("✅ 画面已保存") + # 无人机移动控制 + elif key == ord('w'): + drone.move("forward") + elif key == ord('s'): + drone.move("back") + elif key == ord('a'): + drone.move("left") + elif key == ord('d'): + drone.move("right") + elif key == 2490368: # 上方向键:上升 + drone.move("up") + elif key == 2621440: # 下方向键:下降 + drone.move("down") + elif key == ord('q'): # 左转 + drone.rotate("left") + elif key == ord('e'): # 右转 + drone.rotate("right") except Exception as e: - print(f"程序异常:{str(e)}") + print(f"❌ 程序异常:{str(e)}") finally: - # 资源释放 - drone.stop_video_stream() - drone.land() # 紧急降落 + # ===================== 资源释放(适配虚拟无人机) ===================== + print("\n🔄 程序退出,释放资源...") + if drone.state.value == "FLYING": + drone.land() # 紧急降落 cv2.destroyAllWindows() - print("程序正常退出") + print("✅ 程序正常退出") if __name__ == "__main__": diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py b/src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py deleted file mode 100644 index 2446ee1056..0000000000 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/overlay_map.py +++ /dev/null @@ -1,170 +0,0 @@ -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 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py b/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py deleted file mode 100644 index e7962cbefe..0000000000 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/utils.py.py +++ /dev/null @@ -1,160 +0,0 @@ -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 diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.py b/src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.py deleted file mode 100644 index 168813fa82..0000000000 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/voice_module.py +++ /dev/null @@ -1,172 +0,0 @@ -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 6c506ed5b8be0616263a26b031f165c84a5ceee8 Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Fri, 19 Dec 2025 14:35:53 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=8A=9F=E8=83=BD=E6=8C=89=E9=94=AE=E9=87=8D=E5=90=88?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main.py | 178 +++++++++++------- 1 file changed, 105 insertions(+), 73 deletions(-) diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/main.py b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py index fcba515a92..08f63d4553 100644 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/main.py +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py @@ -1,17 +1,12 @@ import cv2 import argparse import numpy as np -from drone_control import VirtualDrone # 导入正确的无人机类 -from detection_module import DroneDetection # 导入正确的检测类 - - -# 注:MapOverlay/FaceDatabase为自定义模块,若未实现先注释,避免运行报错 -# from map_overlay import MapOverlay -# from face_database import FaceDatabase +import os # 新增:用于创建截图目录 +from drone_control import VirtualDrone +from detection_module import DroneDetection def parse_args(): - """解析命令行参数""" parser = argparse.ArgumentParser(description="AI无人机面部识别与人体追踪(虚拟版)") parser.add_argument("--conf-thres", type=float, default=0.5, help="检测置信度阈值") parser.add_argument("--track-thres", type=float, default=0.4, help="追踪IOU阈值") @@ -19,95 +14,132 @@ def parse_args(): return parser.parse_args() +def draw_clean_text(img, text, pos, color=(0, 255, 0), font_size=0.6): + """过滤乱码字符,保证文本正常显示""" + valid_chars = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :.-()%") + clean_text = ''.join([c for c in text if c in valid_chars]) + cv2.putText( + img, clean_text, pos, + cv2.FONT_HERSHEY_SIMPLEX, font_size, + color, 1, lineType=cv2.LINE_AA + ) + + +def init_screenshot_dir(): + """初始化截图保存目录,避免保存失败""" + if not os.path.exists("drone_screenshots"): + os.makedirs("drone_screenshots") + return "drone_screenshots" + + def main(): args = parse_args() - - # ===================== 初始化核心模块(适配虚拟无人机) ===================== - # 1. 初始化虚拟无人机(替换原TelloDrone) drone = VirtualDrone() - # 2. 初始化检测模块(关联无人机,替换原DetectionEngine) detector = DroneDetection(drone=drone) - # 3. 人脸数据库/地图叠加(未实现则注释,后续可补充) - # face_db = FaceDatabase(db_path="face_database/") - # map_overlay = MapOverlay(map_path=args.map_path, alpha=args.map_alpha) + screenshot_dir = init_screenshot_dir() # 初始化截图目录 + + # 打印初始化信息(明确截图键为Z) + print("=" * 60) + print("✅ 虚拟无人机系统初始化完成") + print(f"初始状态 | 电量:{drone.get_battery():.1f}% | 状态:{drone.state.value} | 位置:{drone.position}") + print("=" * 60) + print("🎮 操作说明:") + print(" ESC → 退出程序 | T → 起飞 | L → 降落 | Z → 保存截图(保存至drone_screenshots目录)") + print(" W/A/S/D → 前/后/左/右 | ↑/↓ → 上升/下降 | Q/E → 左转/右转") + print("=" * 60) + + # 创建可视化窗口 + cv2.namedWindow("AI Drone Control System", cv2.WINDOW_NORMAL) + cv2.resizeWindow("AI Drone Control System", 800, 600) + + # 检测项解释映射 + detection_explain = { + "状态检测": "State Detection", + "电量检测": "Battery Detection", + "位置检测": "Position Detection", + "障碍物检测": "Obstacle Detection", + "碰撞预警": "Collision Warning" + } - # ===================== 模拟初始化逻辑 ===================== - # 加载人脸库(注释,待实现FaceDatabase后启用) - # face_db.load_all_faces() - # print(f"人脸库加载完成,共{len(face_db.get_face_names())}个人脸") - - # 虚拟无人机无需真实连接,模拟启动视频流 - print("✅ 虚拟无人机初始化完成") - print(f"初始电量:{drone.get_battery()}% | 初始状态:{drone.state.value}") - - # ===================== 主循环(适配虚拟无人机逻辑) ===================== try: - # 创建虚拟视频窗口(模拟无人机视频流) - cv2.namedWindow("AI Drone Face & Human Tracking", cv2.WINDOW_NORMAL) - while True: - # 1. 生成虚拟帧(替代真实无人机视频帧) - frame = np.zeros((480, 640, 3), dtype=np.uint8) - # 在虚拟帧上绘制无人机状态 - cv2.putText(frame, f"Battery: {drone.get_battery():.1f}%", (10, 30), - cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) - cv2.putText(frame, f"Position: {drone.position.round(1)}", (10, 70), - cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) - cv2.putText(frame, f"State: {drone.state.value}", (10, 110), - cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) - - # 2. 执行无人机状态检测(替代真实人脸/人体检测) + # 创建黑色背景帧 + frame = np.zeros((600, 800, 3), dtype=np.uint8) + + # 绘制无人机基础状态 + status_y = 30 + draw_clean_text(frame, f"Battery: {drone.get_battery():.1f}%", (10, status_y), (0, 255, 0)) + draw_clean_text(frame, f"Position: {drone.position.round(1)}", (10, status_y + 30), (0, 255, 0)) + draw_clean_text(frame, f"State: {drone.state.value}", (10, status_y + 60), (0, 255, 0)) + draw_clean_text(frame, f"Yaw Angle: {drone.yaw:.0f}°", (10, status_y + 90), (0, 255, 0)) + + # 绘制检测结果(带解释) + detection_y = 150 + draw_clean_text(frame, "=== Detection Results (检测结果解释) ===", (10, detection_y), (255, 255, 0)) + draw_clean_text(frame, "【State:状态 | Battery:电量 | Position:位置 | Obstacle:障碍物 | Collision:碰撞】", + (10, detection_y + 20), (255, 255, 255), 0.4) + detection_y += 40 + detection_results = detector.full_detection() - # 在帧上绘制检测预警信息 - y_offset = 150 - for res in detection_results: + for idx, res in enumerate(detection_results): + detection_y += 30 color = (0, 0, 255) if res.get("warning") or res.get("risk") else (0, 255, 0) - cv2.putText(frame, res["message"], (10, y_offset), - cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2) - y_offset += 30 - - # 3. 地图叠加(注释,待实现MapOverlay后启用) - # frame = map_overlay.overlay(frame) - - # 4. 显示画面 - cv2.imshow("AI Drone Face & Human Tracking", frame) - - # ===================== 按键控制(适配虚拟无人机) ===================== + core_msg = res['message'].split("|")[0].strip() + explain = detection_explain.get(res['type'], "Unknown") + display_text = f"{explain}: {core_msg}" + draw_clean_text(frame, display_text, (10, detection_y), color, 0.5) + + # 绘制操作提示(明确截图键) + draw_clean_text( + frame, + "Operation: ESC(Exit) | T(Takeoff) | L(Land) | Z(Save) | Q/E(Rotate) | W/A/S/D(Move)", + (10, 550), (255, 255, 255), 0.45 + ) + + # 显示画面 + cv2.imshow("AI Drone Control System", frame) + + # 键盘控制逻辑(核心:截图键为Z,S仅后退) key = cv2.waitKey(1) & 0xFF - if key == ord('q'): # 退出 + if key == 27: # ESC退出 + print("\n👋 程序退出中...") break - elif key == ord('t'): # 起飞 + elif key == ord('t'): # T起飞 drone.takeoff() - elif key == ord('l'): # 降落 + elif key == ord('l'): # L降落 drone.land() - elif key == ord('s'): # 保存画面 - cv2.imwrite(f"drone_capture_{cv2.getTickCount()}.jpg", frame) - print("✅ 画面已保存") - # 无人机移动控制 - elif key == ord('w'): + elif key == ord('z'): # Z截图(独立键,无冲突) + # 生成唯一截图文件名(时间戳+状态) + screenshot_name = f"drone_{drone.state.value}_{cv2.getTickCount()}.jpg" + screenshot_path = os.path.join(screenshot_dir, screenshot_name) + # 保存截图 + cv2.imwrite(screenshot_path, frame) + print(f"✅ 截图已保存:{screenshot_path}") + elif key == ord('w'): # W前进 drone.move("forward") - elif key == ord('s'): + elif key == ord('s'): # S后退(仅后退,无截图) drone.move("back") - elif key == ord('a'): + elif key == ord('a'): # A左移 drone.move("left") - elif key == ord('d'): + elif key == ord('d'): # D右移 drone.move("right") - elif key == 2490368: # 上方向键:上升 + elif key == 2490368: # 上方向键上升 drone.move("up") - elif key == 2621440: # 下方向键:下降 + elif key == 2621440: # 下方向键下降 drone.move("down") - elif key == ord('q'): # 左转 + elif key == ord('q'): # Q左转 drone.rotate("left") - elif key == ord('e'): # 右转 + elif key == ord('e'): # E右转 drone.rotate("right") except Exception as e: - print(f"❌ 程序异常:{str(e)}") + print(f"\n❌ 程序异常:{str(e)}") + print("💡 请检查依赖:pip install opencv-python numpy") finally: - # ===================== 资源释放(适配虚拟无人机) ===================== - print("\n🔄 程序退出,释放资源...") - if drone.state.value == "FLYING": - drone.land() # 紧急降落 + # 退出前收尾 + if drone.state.value == "Flying": + drone.land() + print("✅ 无人机已自动降落") cv2.destroyAllWindows() print("✅ 程序正常退出") From 4ac3491cc5ca0031bf91fea92899a78971a1cb91 Mon Sep 17 00:00:00 2001 From: Liu-Kui111 <2072827579@qq.com> Date: Fri, 19 Dec 2025 16:21:00 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BA=86=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main.py | 121 ++++++++++-------- 1 file changed, 67 insertions(+), 54 deletions(-) diff --git a/src/AI_Drone_Face_Recognition_Human_Tracking/main.py b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py index 08f63d4553..8c7e6d9580 100644 --- a/src/AI_Drone_Face_Recognition_Human_Tracking/main.py +++ b/src/AI_Drone_Face_Recognition_Human_Tracking/main.py @@ -1,21 +1,20 @@ import cv2 import argparse import numpy as np -import os # 新增:用于创建截图目录 +import os from drone_control import VirtualDrone from detection_module import DroneDetection def parse_args(): - parser = argparse.ArgumentParser(description="AI无人机面部识别与人体追踪(虚拟版)") + parser = argparse.ArgumentParser(description="AI无人机+真实摄像头版") parser.add_argument("--conf-thres", type=float, default=0.5, help="检测置信度阈值") parser.add_argument("--track-thres", type=float, default=0.4, help="追踪IOU阈值") - parser.add_argument("--map-alpha", type=float, default=0.3, help="地图透明度") + parser.add_argument("--camera-id", type=int, default=0, help="摄像头ID(0为默认摄像头,1为外接)") return parser.parse_args() def draw_clean_text(img, text, pos, color=(0, 255, 0), font_size=0.6): - """过滤乱码字符,保证文本正常显示""" valid_chars = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :.-()%") clean_text = ''.join([c for c in text if c in valid_chars]) cv2.putText( @@ -26,7 +25,6 @@ def draw_clean_text(img, text, pos, color=(0, 255, 0), font_size=0.6): def init_screenshot_dir(): - """初始化截图保存目录,避免保存失败""" if not os.path.exists("drone_screenshots"): os.makedirs("drone_screenshots") return "drone_screenshots" @@ -36,21 +34,31 @@ def main(): args = parse_args() drone = VirtualDrone() detector = DroneDetection(drone=drone) - screenshot_dir = init_screenshot_dir() # 初始化截图目录 - - # 打印初始化信息(明确截图键为Z) + screenshot_dir = init_screenshot_dir() + + # ===================== 新增:初始化摄像头 ===================== + cap = cv2.VideoCapture(args.camera_id) # 打开摄像头(0=默认,1=外接) + if not cap.isOpened(): # 检查摄像头是否打开成功 + print("❌ 无法打开摄像头!请检查:") + print(" 1. 摄像头是否被其他程序占用(如微信、浏览器)") + print(" 2. Python是否有摄像头访问权限(系统设置→隐私)") + print(" 3. 摄像头ID是否正确(尝试修改--camera-id 1)") + return # 打开失败则退出程序 + + # 打印初始化信息 print("=" * 60) - print("✅ 虚拟无人机系统初始化完成") - print(f"初始状态 | 电量:{drone.get_battery():.1f}% | 状态:{drone.state.value} | 位置:{drone.position}") + print("✅ 虚拟无人机+摄像头系统初始化完成") + print(f"初始状态 | 电量:{drone.get_battery():.1f}% | 状态:{drone.state.value}") + print(f"摄像头状态 | ID:{args.camera_id} | 已成功打开") print("=" * 60) print("🎮 操作说明:") - print(" ESC → 退出程序 | T → 起飞 | L → 降落 | Z → 保存截图(保存至drone_screenshots目录)") - print(" W/A/S/D → 前/后/左/右 | ↑/↓ → 上升/下降 | Q/E → 左转/右转") + print(" ESC → 退出 | T → 起飞 | L → 降落 | Z → 保存截图") + print(" W/A/S/D → 前后左右 | ↑/↓ → 上升/下降 | Q/E → 左转/右转") print("=" * 60) # 创建可视化窗口 - cv2.namedWindow("AI Drone Control System", cv2.WINDOW_NORMAL) - cv2.resizeWindow("AI Drone Control System", 800, 600) + cv2.namedWindow("AI Drone + Camera System", cv2.WINDOW_NORMAL) + cv2.resizeWindow("AI Drone + Camera System", 1280, 720) # 适配摄像头分辨率 # 检测项解释映射 detection_explain = { @@ -63,85 +71,90 @@ def main(): try: while True: - # 创建黑色背景帧 - frame = np.zeros((600, 800, 3), dtype=np.uint8) + # ===================== 读取摄像头画面(核心新增) ===================== + ret, frame = cap.read() # 读取摄像头一帧画面 + if not ret: # 摄像头读取失败(如断开) + print("❌ 摄像头画面读取失败!") + break + # 调整画面尺寸适配窗口 + frame = cv2.resize(frame, (1280, 720)) - # 绘制无人机基础状态 + # ===================== 绘制无人机状态(叠加在摄像头画面上) ===================== + # 1. 绘制基础状态(顶部左侧) status_y = 30 - draw_clean_text(frame, f"Battery: {drone.get_battery():.1f}%", (10, status_y), (0, 255, 0)) - draw_clean_text(frame, f"Position: {drone.position.round(1)}", (10, status_y + 30), (0, 255, 0)) - draw_clean_text(frame, f"State: {drone.state.value}", (10, status_y + 60), (0, 255, 0)) - draw_clean_text(frame, f"Yaw Angle: {drone.yaw:.0f}°", (10, status_y + 90), (0, 255, 0)) - - # 绘制检测结果(带解释) - detection_y = 150 - draw_clean_text(frame, "=== Detection Results (检测结果解释) ===", (10, detection_y), (255, 255, 0)) - draw_clean_text(frame, "【State:状态 | Battery:电量 | Position:位置 | Obstacle:障碍物 | Collision:碰撞】", - (10, detection_y + 20), (255, 255, 255), 0.4) - detection_y += 40 + draw_clean_text(frame, f"Battery: {drone.get_battery():.1f}%", (10, status_y), (0, 255, 0), 0.7) + draw_clean_text(frame, f"Position: {drone.position.round(1)}", (10, status_y + 40), (0, 255, 0), 0.7) + draw_clean_text(frame, f"State: {drone.state.value}", (10, status_y + 80), (0, 255, 0), 0.7) + draw_clean_text(frame, f"Yaw Angle: {drone.yaw:.0f}°", (10, status_y + 120), (0, 255, 0), 0.7) + + # 2. 绘制检测结果(顶部右侧,避免遮挡摄像头画面) + detection_y = 30 + draw_clean_text(frame, "=== Detection Results ===", (800, detection_y), (255, 255, 0), 0.7) + draw_clean_text(frame, "【State:状态 | Battery:电量 | Position:位置】", (800, detection_y + 40), + (255, 255, 255), 0.5) + detection_y += 80 detection_results = detector.full_detection() for idx, res in enumerate(detection_results): - detection_y += 30 - color = (0, 0, 255) if res.get("warning") or res.get("risk") else (0, 255, 0) + detection_y += 40 + color = (0, 0, 255) if res.get("warning") else (0, 255, 0) core_msg = res['message'].split("|")[0].strip() explain = detection_explain.get(res['type'], "Unknown") display_text = f"{explain}: {core_msg}" - draw_clean_text(frame, display_text, (10, detection_y), color, 0.5) + draw_clean_text(frame, display_text, (800, detection_y), color, 0.6) - # 绘制操作提示(明确截图键) + # 3. 绘制操作提示(底部) draw_clean_text( frame, "Operation: ESC(Exit) | T(Takeoff) | L(Land) | Z(Save) | Q/E(Rotate) | W/A/S/D(Move)", - (10, 550), (255, 255, 255), 0.45 + (10, 680), (255, 255, 255), 0.6 ) - # 显示画面 - cv2.imshow("AI Drone Control System", frame) + # 显示摄像头+无人机状态叠加画面 + cv2.imshow("AI Drone + Camera System", frame) - # 键盘控制逻辑(核心:截图键为Z,S仅后退) + # ===================== 键盘控制逻辑 ===================== key = cv2.waitKey(1) & 0xFF if key == 27: # ESC退出 print("\n👋 程序退出中...") break - elif key == ord('t'): # T起飞 + elif key == ord('t'): drone.takeoff() - elif key == ord('l'): # L降落 + elif key == ord('l'): drone.land() - elif key == ord('z'): # Z截图(独立键,无冲突) - # 生成唯一截图文件名(时间戳+状态) - screenshot_name = f"drone_{drone.state.value}_{cv2.getTickCount()}.jpg" + elif key == ord('z'): # Z保存截图(摄像头画面+无人机状态) + screenshot_name = f"drone_camera_{drone.state.value}_{cv2.getTickCount()}.jpg" screenshot_path = os.path.join(screenshot_dir, screenshot_name) - # 保存截图 cv2.imwrite(screenshot_path, frame) - print(f"✅ 截图已保存:{screenshot_path}") - elif key == ord('w'): # W前进 + print(f"✅ 摄像头截图已保存:{screenshot_path}") + elif key == ord('w'): drone.move("forward") - elif key == ord('s'): # S后退(仅后退,无截图) + elif key == ord('s'): drone.move("back") - elif key == ord('a'): # A左移 + elif key == ord('a'): drone.move("left") - elif key == ord('d'): # D右移 + elif key == ord('d'): drone.move("right") - elif key == 2490368: # 上方向键上升 + elif key == 2490368: drone.move("up") - elif key == 2621440: # 下方向键下降 + elif key == 2621440: drone.move("down") - elif key == ord('q'): # Q左转 + elif key == ord('q'): drone.rotate("left") - elif key == ord('e'): # E右转 + elif key == ord('e'): drone.rotate("right") except Exception as e: print(f"\n❌ 程序异常:{str(e)}") - print("💡 请检查依赖:pip install opencv-python numpy") + print("💡 建议检查:摄像头是否可用 | OpenCV版本(pip install opencv-python --upgrade)") finally: - # 退出前收尾 + # ===================== 资源释放(核心:关闭摄像头) ===================== + cap.release() # 关闭摄像头 if drone.state.value == "Flying": drone.land() print("✅ 无人机已自动降落") cv2.destroyAllWindows() - print("✅ 程序正常退出") + print("✅ 摄像头已关闭,程序正常退出") if __name__ == "__main__":