YOLO11 目标跟踪
1. 前沿
在《YOLO11 目标检测》 完成了 目标检测
在《YOLO11 图像分割》 完成了 图像分割
在实际应用中,常见的还有根据这两个模型,进行目标跟踪实现功能进阶的应用
2. 目标检测+跟踪
按官方的例程,新建 track_det.py
,使用之前训练的模型,并采用摄像头作为数据源
import cv2
from collections import defaultdict
import numpy as np
from ultralytics import YOLO
# Load the YOLO11 model
model = YOLO("detect/train/weights/best.pt")
# Open the video file
cap = cv2.VideoCapture(0)
# Store the track history
track_history = defaultdict(lambda: [])
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLO11 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Get the boxes and track IDs
if results[0].boxes.is_track:
boxes = results[0].boxes.xywh.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist()
# Plot the tracks
for box, track_id in zip(boxes, track_ids):
x, y, w, h = box
track = track_history[track_id]
track.append((float(x), float(y))) # x, y center point
if len(track) > 30: # retain 90 tracks for 90 frames
track.pop(0)
# Draw the tracking lines
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
cv2.polylines(annotated_frame, [points], isClosed=False, color=(255, 168, 0), thickness=5)
# Display the annotated frame
cv2.imshow("YOLO11 Tracking", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
可以看到出来目标检测,还有 运动轨迹 和 ID编号
3. 图像分割+跟踪
按官方的例程,新建 track_seg.py
,使用之前训练的模型,并采用摄像头作为数据源
import cv2
from collections import defaultdict
import numpy as np
from ultralytics import YOLO
# Load the YOLO11 model
model = YOLO("segment/train/weights/best.pt")
# Open the video file
cap = cv2.VideoCapture(0)
# Store the track history
track_history = defaultdict(lambda: [])
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLO11 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Get the boxes and track IDs
if results[0].boxes.is_track:
boxes = results[0].boxes.xywh.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist()
# Plot the tracks
for box, track_id in zip(boxes, track_ids):
x, y, w, h = box
track = track_history[track_id]
track.append((float(x), float(y))) # x, y center point
if len(track) > 30: # retain 90 tracks for 90 frames
track.pop(0)
# Draw the tracking lines
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
cv2.polylines(annotated_frame, [points], isClosed=False, color=(255, 168, 0), thickness=5)
# Display the annotated frame
cv2.imshow("YOLO11 Tracking", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
可以看到出来图像分割,还有 运动轨迹 和 ID编号
谢谢