Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 118 additions & 73 deletions src/AI_Drone_Face_Recognition_Human_Tracking/main.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,132 @@
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 = 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(
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)

# ===================== 模拟初始化逻辑 =====================
# 加载人脸库(注释,待实现FaceDatabase后启用)
# face_db.load_all_faces()
# print(f"人脸库加载完成,共{len(face_db.get_face_names())}个人脸")
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}")
print(f"摄像头状态 | ID:{args.camera_id} | 已成功打开")
print("=" * 60)
print("🎮 操作说明:")
print(" ESC → 退出 | T → 起飞 | L → 降落 | Z → 保存截图")
print(" W/A/S/D → 前后左右 | ↑/↓ → 上升/下降 | Q/E → 左转/右转")
print("=" * 60)

# 创建可视化窗口
cv2.namedWindow("AI Drone + Camera System", cv2.WINDOW_NORMAL)
cv2.resizeWindow("AI Drone + Camera System", 1280, 720) # 适配摄像头分辨率

# 检测项解释映射
detection_explain = {
"状态检测": "State Detection",
"电量检测": "Battery Detection",
"位置检测": "Position Detection",
"障碍物检测": "Obstacle Detection",
"碰撞预警": "Collision Warning"
}

# 虚拟无人机无需真实连接,模拟启动视频流
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. 执行无人机状态检测(替代真实人脸/人体检测)
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)
# ===================== 读取摄像头画面(核心新增) =====================
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), 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 += 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, (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, 680), (255, 255, 255), 0.6
)

# 显示摄像头+无人机状态叠加画面
cv2.imshow("AI Drone + Camera System", frame)

# ===================== 键盘控制逻辑 =====================
key = cv2.waitKey(1) & 0xFF
if key == ord('q'): # 退出
if key == 27: # ESC退出
print("\n👋 程序退出中...")
break
elif key == ord('t'): # 起飞
elif key == ord('t'):
drone.takeoff()
elif key == ord('l'): # 降落
elif key == ord('l'):
drone.land()
elif key == ord('s'): # 保存画面
cv2.imwrite(f"drone_capture_{cv2.getTickCount()}.jpg", frame)
print("✅ 画面已保存")
# 无人机移动控制
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'):
drone.move("forward")
elif key == ord('s'):
Expand All @@ -92,24 +135,26 @@ def main():
drone.move("left")
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'): # 左转
elif key == ord('q'):
drone.rotate("left")
elif key == ord('e'): # 右转
elif key == ord('e'):
drone.rotate("right")

except Exception as e:
print(f"❌ 程序异常:{str(e)}")
print(f"\n❌ 程序异常:{str(e)}")
print("💡 建议检查:摄像头是否可用 | OpenCV版本(pip install opencv-python --upgrade)")
finally:
# ===================== 资源释放(适配虚拟无人机) =====================
print("\n🔄 程序退出,释放资源...")
if drone.state.value == "FLYING":
drone.land() # 紧急降落
# ===================== 资源释放(核心:关闭摄像头) =====================
cap.release() # 关闭摄像头
if drone.state.value == "Flying":
drone.land()
print("✅ 无人机已自动降落")
cv2.destroyAllWindows()
print("✅ 程序正常退出")
print("✅ 摄像头已关闭,程序正常退出")


if __name__ == "__main__":
Expand Down
Loading