Drobotics logo DROBOTICSTechnical Library

Drone Guide

YOLO 无界面人体检测:检测到人后保存图片

All Drone

YOLO Vision Code

YOLO 无界面人体检测:检测到人后保存图片

适合无人机或机载计算机无显示器运行,检测到人体后自动保存带框图片。

这些脚本用于 DRF450 或机载计算机上的 OpenCV + YOLOv8 视觉识别测试。建议先在桌面或树莓派本地验证摄像头索引、模型路径和性能,再把检测结果接入 MAVLink、自主飞行或任务触发逻辑。

脚本目标

后台读取摄像头并运行 YOLOv8,检测到 person 后按冷却时间保存标注图片到 detections 目录。

运行依赖: Python、OpenCV、Ultralytics YOLO。检测脚本默认模型路径为 /home/drobotics/yolov8n.pt,部署时请确认模型文件存在。

学习重点

  • 适合 SSH 后台运行和外场记录
  • SAVE_COOLDOWN 防止连续保存过多图片
  • 保存前绘制检测框,方便回看验证

关键参数

参数当前设置
CAMERA_INDEX0
CONF_THRESHOLD0.5
SAVE_DIR"detections"
FRAME_SKIP2 # increase for more speed
SAVE_COOLDOWN2 # seconds between saves (avoid spam)
PERSON_CLASS_ID0

运行前检查

  1. 安装依赖:pip install opencv-python ultralytics
  2. 确认摄像头编号,必要时修改 CAMERA_INDEXCAMERA_INDEXES
  3. 确认 YOLO 模型路径,例如 /home/drobotics/yolov8n.pt
  4. 在无人机上运行前,先单独验证摄像头、推理速度、保存路径和散热。

完整代码:yolo_detect_human.py

import cv2
import os
import time
from datetime import datetime
from ultralytics import YOLO

# =========================
# SETTINGS
# =========================
CAMERA_INDEX = 0
CONF_THRESHOLD = 0.5
SAVE_DIR = "detections"

FRAME_SKIP = 2 # increase for more speed
SAVE_COOLDOWN = 2 # seconds between saves (avoid spam)

# =========================
# INIT
# =========================
os.makedirs(SAVE_DIR, exist_ok=True)

print(" Loading YOLOv8...")
model = YOLO("/home/drobotics/yolov8n.pt")

PERSON_CLASS_ID = 0

cap = cv2.VideoCapture(CAMERA_INDEX)

if not cap.isOpened():
 print(" Camera not opened")
 exit()

print(" Running headless detection... saving only")

counter = 0
last_save_time = 0

# =========================
# LOOP
# =========================
while True:
 ret, frame = cap.read()
 if not ret:
 print(" Frame error")
 break

 counter += 1
 if counter % FRAME_SKIP != 0:
 continue

 # Resize for speed
 frame = cv2.resize(frame, (640, 360))

 results = model(frame, imgsz=320, verbose=False)[0]

 person_detected = False
 boxes_to_save = []

 # =========================
 # CHECK DETECTIONS
 # =========================
 for box in results.boxes:
 cls_id = int(box.cls[0])
 conf = float(box.conf[0])

 if cls_id == PERSON_CLASS_ID and conf > CONF_THRESHOLD:
 person_detected = True

 x1, y1, x2, y2 = map(int, box.xyxy[0])
 boxes_to_save.append((x1, y1, x2, y2, conf))

 # =========================
 # SAVE IMAGE IF PERSON FOUND
 # =========================
 current_time = time.time()

 if person_detected and (current_time - last_save_time > SAVE_COOLDOWN):

 timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
 filename = os.path.join(SAVE_DIR, f"person_{timestamp}.jpg")

 # draw boxes before saving
 for (x1, y1, x2, y2, conf) in boxes_to_save:
 cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
 cv2.putText(frame, f"Person {conf:.2f}",
 (x1, y1 - 10),
 cv2.FONT_HERSHEY_SIMPLEX,
 0.5, (0, 255, 0), 2)

 cv2.imwrite(filename, frame)

 print(f" Saved detection: {filename}")

 last_save_time = current_time

# =========================
# CLEANUP
# =========================
cap.release()
print(" Stopped")

下一步

先完成摄像头采集,再运行实时检测或无界面检测。后续可以把人体检测结果接入 MAVLink 任务逻辑,实现识别触发、悬停、返航、录像或地面站告警。

返回 DRF450 YOLO 章节