Bỏ để qua phần nội dung

Phân đoạn và theo dõi phiên bản bằng cách sử dụng Ultralytics YOLOv8 🚀

Phân đoạn phiên bản là gì?

Ultralytics YOLOv8 Phân đoạn phiên bản liên quan đến việc xác định và phác thảo các đối tượng riêng lẻ trong một hình ảnh, cung cấp sự hiểu biết chi tiết về phân bố không gian. Không giống như phân đoạn ngữ nghĩa, nó dán nhãn duy nhất và phân định chính xác từng đối tượng, rất quan trọng cho các nhiệm vụ như phát hiện đối tượng và hình ảnh y tế.

Có hai loại theo dõi phân đoạn phiên bản có sẵn trong Ultralytics gói:

  • Phân đoạn phiên bản với các đối tượng lớp: Mỗi đối tượng lớp được gán một màu duy nhất để phân tách trực quan rõ ràng.

  • Phân đoạn phiên bản với Object Tracks: Mỗi bản nhạc được thể hiện bằng một màu sắc riêng biệt, tạo điều kiện dễ dàng xác định và theo dõi.



Xem: Phân đoạn phiên bản với Theo dõi đối tượng bằng cách sử dụng Ultralytics YOLOv8

Mẫu

Phân đoạn phiên bản Phân đoạn phiên bản + Theo dõi đối tượng
Ultralytics Phân đoạn phiên bản Ultralytics Phân đoạn phiên bản với theo dõi đối tượng
Ultralytics Phân đoạn phiên bản 😍 Ultralytics Phân đoạn phiên bản với theo dõi 🔥 đối tượng

Phân đoạn và theo dõi phiên bản

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):
            color = colors(int(cls), True)
            txt_color = annotator.get_txt_color(color)
            annotator.seg_bbox(mask=mask, mask_color=color, label=names[int(cls)], txt_color=txt_color)

    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):
            color = colors(int(track_id), True)
            txt_color = annotator.get_txt_color(color)
            annotator.seg_bbox(mask=mask, mask_color=color, label=str(track_id), txt_color=txt_color)

    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 Lập luận

Tên Kiểu Mặc định Sự miêu tả
mask array None Tọa độ mặt nạ phân đoạn
mask_color RGB (255, 0, 255) Màu mặt nạ cho mỗi hộp được phân đoạn
label str None Nhãn cho đối tượng được phân đoạn
txt_color RGB None Màu nhãn cho đối tượng được phân đoạn và theo dõi

Ghi

Đối với bất kỳ câu hỏi nào, vui lòng gửi câu hỏi của bạn trong Ultralytics Phần vấn đề hoặc phần thảo luận được đề cập bên dưới.

FAQ

Làm thế nào để thực hiện phân đoạn phiên bản bằng cách sử dụng Ultralytics YOLOv8?

Để thực hiện phân đoạn phiên bản bằng cách sử dụng Ultralytics YOLOv8, khởi tạo YOLO Mô hình với phiên bản phân đoạn của YOLOv8 và xử lý khung hình video thông qua nó. Dưới đây là một ví dụ mã đơn giản hóa:

Ví dụ

import cv2

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

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.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        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=model.model.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()

Tìm hiểu thêm về phân đoạn phiên bản trong Ultralytics YOLOv8 hướng dẫn.

Sự khác biệt giữa phân đoạn phiên bản và theo dõi đối tượng trong là gì Ultralytics YOLOv8?

Phân đoạn phiên bản xác định và phác thảo các đối tượng riêng lẻ trong một hình ảnh, cung cấp cho mỗi đối tượng một nhãn và mặt nạ duy nhất. Theo dõi đối tượng mở rộng điều này bằng cách gán nhãn nhất quán cho các đối tượng trên các khung video, tạo điều kiện theo dõi liên tục các đối tượng giống nhau theo thời gian. Tìm hiểu thêm về sự khác biệt trong Ultralytics YOLOv8 tài liệu.

Tại sao tôi nên sử dụng Ultralytics YOLOv8 ví dụ: phân đoạn và theo dõi các mô hình khác như Mask R-CNN hoặc Faster R-CNN?

Ultralytics YOLOv8 cung cấp hiệu suất thời gian thực, độ chính xác vượt trội và dễ sử dụng so với các mẫu khác như Mask R-CNN hay Faster R-CNN. YOLOv8 Cung cấp sự tích hợp liền mạch với Ultralytics HUB, cho phép người dùng quản lý các mô hình, bộ dữ liệu và đường ống đào tạo một cách hiệu quả. Khám phá thêm về lợi ích của YOLOv8 trong Ultralytics Tin tức.

Làm cách nào để triển khai theo dõi đối tượng bằng cách sử dụng Ultralytics YOLOv8?

Để thực hiện theo dõi đối tượng, hãy sử dụng model.track và đảm bảo rằng ID của mỗi đối tượng được gán nhất quán trên các khung. Dưới đây là một ví dụ đơn giản:

Ví dụ

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:
        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()

Tìm hiểu thêm trong phần Phân đoạn và theo dõi phiên bản.

Có bất kỳ bộ dữ liệu nào được cung cấp bởi Ultralytics Thích hợp cho đào tạo YOLOv8 Các mô hình để phân đoạn và theo dõi ví dụ?

Có Ultralytics cung cấp một số bộ dữ liệu phù hợp cho đào tạo YOLOv8 các mô hình, bao gồm bộ dữ liệu phân đoạn và theo dõi. Các ví dụ về tập dữ liệu, cấu trúc và hướng dẫn sử dụng có thể được tìm thấy trong Ultralytics Tài liệu về bộ dữ liệu.



Đã tạo 2023-12-18, Cập nhật 2024-07-14
Tác giả: RizwanMunawar (2), glenn-jocher (10), IvorZhu331 (1)

Ý kiến