Instance Segmentation and Tracking using Ultralytics YOLO11 ๐
What is Instance Segmentation?
Ultralytics YOLO11 instance segmentation involves identifying and outlining individual objects in an image, providing a detailed understanding of spatial distribution. Unlike semantic segmentation, it uniquely labels and precisely delineates each object, crucial for tasks like object detection and medical imaging.
Ultralytics ํจํค์ง์์ ์ฌ์ฉํ ์ ์๋ ์ธ์คํด์ค ์ธ๋ถํ ์ถ์ ์๋ ๋ ๊ฐ์ง ์ ํ์ด ์์ต๋๋ค:
-
ํด๋์ค ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ ์ธ์คํด์ค ์ธ๋ถํ: ๊ฐ ํด๋์ค ๊ฐ์ฒด์๋ ์๊ฐ์ ์ผ๋ก ๋ช ํํ๊ฒ ๊ตฌ๋ถํ ์ ์๋๋ก ๊ณ ์ ํ ์์์ด ํ ๋น๋ฉ๋๋ค.
-
์ค๋ธ์ ํธ ํธ๋์ ์ฌ์ฉํ ์ธ์คํด์ค ์ธ๋ถํ: ๋ชจ๋ ํธ๋์ ๊ณ ์ ํ ์์์ผ๋ก ํ์๋์ด ์ฝ๊ฒ ์๋ณํ๊ณ ์ถ์ ํ ์ ์์ต๋๋ค.
Watch: Instance Segmentation with Object Tracking using Ultralytics YOLO11
์ํ
์ธ์คํด์ค ์ธ๋ถํ | ์ธ์คํด์ค ์ธ๋ถํ + ์ค๋ธ์ ํธ ์ถ์ |
---|---|
Ultralytics ์ธ์คํด์ค ์ธ๋ถํ ๐ | Ultralytics ์ค๋ธ์ ํธ ์ถ์ ์ ํตํ ์ธ์คํด์ค ์ธ๋ถํ ๐ฅ. |
์ธ์คํด์ค ์ธ๋ถํ ๋ฐ ์ถ์
import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
model = YOLO("yolo11n-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("yolo11n-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
์ธ์
์ด๋ฆ | ์ ํ | ๊ธฐ๋ณธ๊ฐ | ์ค๋ช |
---|---|---|---|
mask |
array |
None |
์ธ๋ถํ ๋ง์คํฌ ์ขํ |
mask_color |
RGB |
(255, 0, 255) |
๋ชจ๋ ์ธ๊ทธ๋จผํธ ์์์ ๋ํ ๋ง์คํฌ ์์ |
label |
str |
None |
์ธ๊ทธ๋จผํธ ๊ฐ์ฒด์ ๋ํ ๋ ์ด๋ธ |
txt_color |
RGB |
None |
์ธ๊ทธ๋จผํธ ๋ฐ ์ถ์ ๋์์ ๋ ์ด๋ธ ์์ |
์ฐธ๊ณ
๋ฌธ์ ์ฌํญ์ด ์์ผ์๋ฉด Ultralytics ์ด์ ์น์ ๋๋ ์๋์ ์ธ๊ธ๋ ํ ๋ก ์น์ ์ ์์ ๋กญ๊ฒ ์ง๋ฌธ์ ๊ฒ์ํด ์ฃผ์ธ์.
์์ฃผ ๋ฌป๋ ์ง๋ฌธ
How do I perform instance segmentation using Ultralytics YOLO11?
To perform instance segmentation using Ultralytics YOLO11, initialize the YOLO model with a segmentation version of YOLO11 and process video frames through it. Here's a simplified code example:
์
import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
model = YOLO("yolo11n-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()
Learn more about instance segmentation in the Ultralytics YOLO11 guide.
What is the difference between instance segmentation and object tracking in Ultralytics YOLO11?
Instance segmentation identifies and outlines individual objects within an image, giving each object a unique label and mask. Object tracking extends this by assigning consistent labels to objects across video frames, facilitating continuous tracking of the same objects over time. Learn more about the distinctions in the Ultralytics YOLO11 documentation.
Why should I use Ultralytics YOLO11 for instance segmentation and tracking over other models like Mask R-CNN or Faster R-CNN?
Ultralytics YOLO11 offers real-time performance, superior accuracy, and ease of use compared to other models like Mask R-CNN or Faster R-CNN. YOLO11 provides a seamless integration with Ultralytics HUB, allowing users to manage models, datasets, and training pipelines efficiently. Discover more about the benefits of YOLO11 in the Ultralytics blog.
How can I implement object tracking using Ultralytics YOLO11?
๊ฐ์ฒด ์ถ์ ์ ๊ตฌํํ๋ ค๋ฉด model.track
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ๊ฐ์ฒด์ ID๊ฐ ํ๋ ์ ๊ฐ์ ์ผ๊ด๋๊ฒ ํ ๋น๋๋๋ก ํฉ๋๋ค. ์๋๋ ๊ฐ๋จํ ์์์
๋๋ค:
์
from collections import defaultdict
import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors
track_history = defaultdict(lambda: [])
model = YOLO("yolo11n-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()
์ธ์คํด์ค ์ธ๋ถํ ๋ฐ ์ถ์ ์น์ ์์ ์์ธํ ์์๋ณด์ธ์.
Are there any datasets provided by Ultralytics suitable for training YOLO11 models for instance segmentation and tracking?
Yes, Ultralytics offers several datasets suitable for training YOLO11 models, including segmentation and tracking datasets. Dataset examples, structures, and instructions for use can be found in the Ultralytics Datasets documentation.