2

My code is as shown below:

import numpy as np
from ultralytics import YOLO
from PIL import Image
import cv2
from google.colab.patches import cv2_imshow


model = YOLO("yolov8n-seg.pt")
results = model.predict("payload.jpeg")

masks = results[0].masks  # Masks object

H,W,_ = cv2.imread("payload.jpeg").shape

x = (results[0].masks.segments[0][:,0]*W).astype("int")
y = (results[0].masks.segments[0][:,1]*H).astype("int")
blk=np.zeros((H,W))
blk[y,x] =255
cv2_imshow(blk.astype("uint8"))

here is: payload.jpeg

this is the output

As you can see tree splits mask into 2 parts but I can only access first part.

cv2_imshow result:

How can I get the second part of the mask?

I tried getting length of masks but always get 1. I think it should be 2 or my approach is wrong.

jawamba
  • 21
  • 4

1 Answers1

0

The actual masks are returned in results[0].masks.data in the form of pixel map. Then this function is called: ops.masks2segments(self.masks) (https://github.com/ultralytics/ultralytics/blob/e7876e1ba9e1c53d24370d9d3f10e0b79c1c4d44/ultralytics/yolo/engine/results.py)

which defaults to the "largest" strategy (https://github.com/ultralytics/ultralytics/blob/edd3ff16693cc364d846d0c9d676f90aa12ee3af/ultralytics/yolo/utils/ops.py)

You'd have to call this function yourself to get the "concatenated" map