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 |
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):
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()
import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
from collections import defaultdict
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
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 |
tuple |
(255, 0, 255) |
Màu mặt nạ cho mỗi hộp được phân đoạn |
det_label |
str |
None |
Nhãn cho đối tượng được phân đoạn |
track_label |
str |
None |
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.