Drobotics logo DROBOTICSTechnical Library

Drone Guide

YOLO 快速人体检测:低分辨率显示版

All Drone

YOLO Vision Code

YOLO 快速人体检测:低分辨率显示版

将画面缩放到 640x360,并跳帧推理,提高树莓派或低功耗机载计算机上的实时性。

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

脚本目标

以 640x360 读取画面,每隔指定帧数执行一次 YOLOv8 推理,只显示人体检测框和 FPS。

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

学习重点

  • 适合树莓派、Jetson Nano 等资源受限设备
  • FRAME_SKIP 可在速度和检测连续性之间调节
  • imgsz=320 可降低推理负载

关键参数

参数当前设置
CAMERA_INDEX0
FRAME_WIDTH640
FRAME_HEIGHT360
FRAME_SKIP2 # increase to 3 or 4 for more FPS
CONF_THRESHOLD0.5
PERSON_CLASS_ID0

运行前检查

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

完整代码:yolo_detect_human_show.py

import cv2
import time
from ultralytics import YOLO

# =========================
# SETTINGS (TUNE HERE)
# =========================
CAMERA_INDEX = 0
FRAME_WIDTH = 640
FRAME_HEIGHT = 360

FRAME_SKIP = 2 # increase to 3 or 4 for more FPS
CONF_THRESHOLD = 0.5

# =========================
# LOAD MODEL (FASTEST OPTION)
# =========================
print(" Loading YOLOv8 model...")
model = YOLO("/home/drobotics/yolov8n.pt") # smallest & fastest model

PERSON_CLASS_ID = 0

# =========================
# CAMERA INIT
# =========================
cap = cv2.VideoCapture(CAMERA_INDEX)

if not cap.isOpened():
 print(" Cannot open camera")
 exit()

print(" Camera started... Press 'q' to exit")

counter = 0
prev_time = 0

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

 # =========================
 # RESIZE FRAME (IMPORTANT)
 # =========================
 frame = cv2.resize(frame, (FRAME_WIDTH, FRAME_HEIGHT))

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

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

 # =========================
 # DRAW 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:
 x1, y1, x2, y2 = map(int, box.xyxy[0])

 cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)

 label = f"Person {conf:.2f}"
 cv2.putText(frame, label, (x1, y1 - 10),
 cv2.FONT_HERSHEY_SIMPLEX, 0.5,
 (0, 255, 0), 2)

 # =========================
 # FPS CALCULATION
 # =========================
 curr_time = time.time()
 fps = 1 / (curr_time - prev_time) if prev_time else 0
 prev_time = curr_time

 cv2.putText(frame, f"FPS: {fps:.2f}", (10, 30),
 cv2.FONT_HERSHEY_SIMPLEX, 1,
 (255, 0, 0), 2)

 # =========================
 # DISPLAY
 # =========================
 cv2.imshow("YOLOv8 Human Detection - FAST MODE", frame)

 # press q to quit
 if cv2.waitKey(1) & 0xFF == ord('q'):
 break

# =========================
# CLEANUP
# =========================
cap.release()
cv2.destroyAllWindows()
print(" Stopped cleanly")

下一步

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

返回 DRF450 YOLO 章节