YOLOv8 Real-time Instance Segmentation with Python
Instance segmentation goes a step further than object detection and involves identifying individual objects in an image and segmenting them from the rest of the image. The output of an instance segmentation model is a set of masks or contours that outline each object in the image, along with class labels and confidence scores for each object. Instance segmentation is useful when you need to know not only where objects are in an image, but also what their exact shape is.
YOLOv8 Variants for Instance Segmentation
YYOLOv8 Segmentation models have seg suffix, i.e. (yolov8n-seg.pt, yolov8s-seg.pt, yolov8m-seg.pt, yolov8l-seg.pt, yolov8x-seg.pt) and are pretrained on COCO dataset with the following Classes.
[‘person’, ‘bicycle’, ‘car’, ‘motorcycle’, ‘airplane’, ‘bus’, ‘train’, ‘truck’, ‘boat’, ‘traffic light’, ‘fire hydrant’, ‘stop sign’, ‘parking meter’, ‘bench’, ‘bird’, ‘cat’, ‘dog’, ‘horse’, ‘sheep’, ‘cow’, ‘elephant’, ‘bear’, ‘zebra’, ‘giraffe’, ‘backpack’, ‘umbrella’, ‘handbag’, ‘tie’, ‘suitcase’, ‘frisbee’, ‘skis’, ‘snowboard’, ‘sports ball’, ‘kite’, ‘baseball bat’, ‘baseball glove’, ‘skateboard’, ‘surfboard’, ‘tennis racket’, ‘bottle’, ‘wine glass’, ‘cup’, ‘fork’, ‘knife’, ‘spoon’, ‘bowl’, ‘banana’, ‘apple’, ‘sandwich’, ‘orange’, ‘broccoli’, ‘carrot’, ‘hot dog’, ‘pizza’, ‘donut’, ‘cake’, ‘chair’, ‘couch’, ‘potted plant’, ‘bed’, ‘dining table’, ‘toilet’, ‘tv’, ‘laptop’, ‘mouse’, ‘remote’, ‘keyboard’, ‘cell phone’, ‘microwave’, ‘oven’, ‘toaster’, ‘sink’, ‘refrigerator’, ‘book’, ‘clock’, ‘vase’, ‘scissors’, ‘teddy bear’, ‘hair drier’, ‘toothbrush’]
YOLOv8 pretrained Segmentation models (nano, small, medium, large and extra large based on number of parameters) are shown in the table below:
Setup UltraLytics for YOLOv8
%pip install ultralytics
import ultralytics
ultralytics.checks()
Load YOLOv8 for Segmentation
from ultralytics import YOLO
# Load a model
# You can use different YOLOv8 variants (yolov8n-seg, yolov8s-seg, yolov8m-seg, yolov8l-seg, yolov8nx-seg)
model = YOLO('yolov8n-seg.pt') # load a pretrained model
# Use the model
results = model('https://ultralytics.com/images/zidane.jpg') # predict on an image
# Save the output image after segmentation
results[0].save('/content/output.jpg')
# Print the COCO dataset classes on which model is trained.
print(model.names.values())
Input Image
Display the Output Image after Segmentation
from IPython.display import Image
# Display the image
Image(filename='/content/output.jpg')
Output Predicted Image
Print Objects Masks
for r in results:
print(r.masks)
Discover the secrets to mastering Video and Image Object Detection and Segmentation with Python, in the following featured courses.
Video Segmentation with Python using Deep Learning for Real-Time
YOLOv8: Video Object Detection with Python on Custom Dataset
If you like reading, Buy me a Cofee !
Follow to Stay Tuned and Never Miss a Story! Thanks!!