跳至内容

使用Ultralytics YOLOv8 🚀 进行实例分割和跟踪

什么是实例分割?

Ultralytics YOLOv8实例分割包括识别和勾勒图像中的单个物体,提供对空间分布的详细了解。与语义分割不同的是,它对每个物体进行唯一标记和精确划分,这对物体检测和医学成像等任务至关重要。

Ultralytics 软件包中有两种类型的实例分割跟踪:

  • 使用类对象进行实例分割:每个类对象都有一种独特的颜色,以便进行清晰的视觉区分。

  • 使用对象轨迹进行实例分割:每个轨迹都用不同的颜色表示,便于识别和跟踪。



观看: 使用对象跟踪技术进行实例分割Ultralytics YOLOv8

样品

实例分割 实例分割 + 物体跟踪
Ultralytics 实例分割 Ultralytics 利用对象跟踪进行实例分割
Ultralytics 实例分割 😍 Ultralytics 利用对象跟踪进行实例分割 🔥

实例分割和跟踪

import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

model = YOLO("yolov8n-seg.pt")  # segmentation model
names = model.model.names
cap = cv2.VideoCapture("path/to/video/file.mp4")
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

out = cv2.VideoWriter("instance-segmentation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        print("Video frame is empty or video processing has been successfully completed.")
        break

    results = model.predict(im0)
    annotator = Annotator(im0, line_width=2)

    if results[0].masks is not None:
        clss = results[0].boxes.cls.cpu().tolist()
        masks = results[0].masks.xy
        for mask, cls in zip(masks, clss):
            annotator.seg_bbox(mask=mask, mask_color=colors(int(cls), True), det_label=names[int(cls)])

    out.write(im0)
    cv2.imshow("instance-segmentation", im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

out.release()
cap.release()
cv2.destroyAllWindows()
from collections import defaultdict

import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

track_history = defaultdict(lambda: [])

model = YOLO("yolov8n-seg.pt")  # segmentation model
cap = cv2.VideoCapture("path/to/video/file.mp4")
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))

out = cv2.VideoWriter("instance-segmentation-object-tracking.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        print("Video frame is empty or video processing has been successfully completed.")
        break

    annotator = Annotator(im0, line_width=2)

    results = model.track(im0, persist=True)

    if results[0].boxes.id is not None and results[0].masks is not None:
        masks = results[0].masks.xy
        track_ids = results[0].boxes.id.int().cpu().tolist()

        for mask, track_id in zip(masks, track_ids):
            annotator.seg_bbox(mask=mask, mask_color=colors(track_id, True), track_label=str(track_id))

    out.write(im0)
    cv2.imshow("instance-segmentation-object-tracking", im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

out.release()
cap.release()
cv2.destroyAllWindows()

seg_bbox 论据

名称 类型 默认值 说明
mask array None 分割掩码坐标
mask_color tuple (255, 0, 255) 每个分割框的屏蔽颜色
det_label str None 分段对象的标签
track_label str None 被分割和跟踪物体的标签

备注

如有任何疑问,请随时在Ultralytics 问题板块或下面提到的讨论板块发布您的问题。



创建于 2023-12-18,更新于 2024-05-18
作者:glenn-jocher(7)、RizwanMunawar(2)

评论