Instance segmentation YOLO v8 | Opencv with Python tutorial

What is Yolo v8 segmentation for?

In this tutorial, we will see how to use computer vision to apply segmentation to objects with Yolov8 by Ultralitycs. With the segmentation, the object’s shape is identified, allowing the calculation of its size. Another very popular and simple-to-use algorithm for object segmentation is mask r-CNN and also in this case you can Train Mask R-CNN for Image Segmentation

yolov8 rugby mask

Install YOLO v8 by Ultralitycs

Before proceeding with the segmentation of Yolo v8 we need to install the necessary libraries from the terminal by running these commands:

pip install ultralytics
pip install opencv-python

Also, remember to download the file from the link below which contains the YOLOSegmentation module. I wrote this class to simplify the extraction of segmentation data, there are many parameters to consider but for this exercise, we can only consider the essential ones.

How to use YOLO v8 instant segmentation

If all the installations have been completed successfully you can proceed with recalling the libraries and the image to analyze. I remember to put the yolo_segmentation.py file in the project folder.

import cv2
from yolo_segmentation import YOLOSegmentation

img = cv2.imread("images/rugby.jpg")
img = cv2.resize(img, None, fx=0.7, fy=0.7)

We import the Yolo v8 segmentation model by entering the path where the model is located.

# Segmentation detector
ys = YOLOSegmentation("yolov8m-seg.pt")

When the object is identified by the algorithm, it returns as output the points to be used to define the segmentation

bboxes, classes, segmentations, scores = ys.detect(img)
for bbox, class_id, seg, score in zip(bboxes, classes, segmentations, scores):
    # print("bbox:", bbox, "class id:", class_id, "seg:", seg, "score:", score)
    (x, y, x2, y2) = bbox
    
    print(seg)

In the image below the example of the output

yolo v8 segmentation points

and using OpenCV functions cv2.rectangle() cv2.polylines() we can draw the lines on the image

        cv2.rectangle(img, (x, y), (x2, y2), (255, 0, 0), 2)

        cv2.polylines(img, [seg], True, (0, 0, 255), 4)

This is the result representing the object detection bounding box in red and the object segmentation in blue

rugby ball segmentation

Select the right object class

For the identification of the objects, we used the coco dataset which identifies 80 objects. If you want to identify and segment a precise object you have to set conditions otherwise the model will identify everything that corresponds to the list of 80 objects and is present in the image.

In the example below he has segmented people and the ball

people segmentation with yolo v8

With a small correction, I tell the code to identify only class 32 which corresponds to the “sports ball” object

for bbox, class_id, seg, score in zip(bboxes, classes, segmentations, scores):
    # print("bbox:", bbox, "class id:", class_id, "seg:", seg, "score:", score)
    (x, y, x2, y2) = bbox
    if class_id == 32:
        cv2.rectangle(img, (x, y), (x2, y2), (255, 0, 0), 2)

        cv2.polylines(img, [seg], True, (0, 0, 255), 4)

        cv2.putText(img, str(class_id), (x, y - 10), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
basket ball segmentation