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