roboflow / supervision

We write your reusable computer vision tools. 💜
https://supervision.roboflow.com
MIT License
22.65k stars 1.69k forks source link

`sv.Detections.from_ultralytics()` can't handle segmentation results `ultralytics.engine.results.Results` from ultralytics `SAM()` when using `bboxes` or `points` arguments #1453

Closed xaristeidou closed 1 month ago

xaristeidou commented 1 month ago

Search before asking

Bug

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/christoforos/Documents/supervision/supervision/detection/core.py", line 277, in from_ultralytics
    class_id = ultralytics_results.boxes.cls.cpu().numpy().astype(int)
AttributeError: 'NoneType' object has no attribute 'cls'

Environment

Minimal Reproducible Example

import cv2
import supervision as sv
from ultralytics import SAM

image = cv2.imread("image.jpg")

model = SAM("mobile_sam.pt")
results = model(
    image,
    bboxes=[[100, 100, 200, 200]],
)

detections = sv.Detections.from_ultralytics(results[0])

Additional

When predicting with any model using SAM() class from ultralytics, if we don't specify bboxes or points arguments in the predict() function of SAM() model, it has expected behaviour.

The problem is when we pass bboxes or points arguments, which will results in ultralytics.engine.results.Results to contain None type for boxes, probs attributes (not sure if probs attribute affects the process but boxes does for sure).

As a results, mask annotators (polygon, halo, mask) also can't be used.

We could slightly modify sv.Detection.from_ultralytics() to handle this problem, by making an extra check and fill in required attributes of the class. I have implemented a fix of this issue, and I could submit a PR for review.

Are you willing to submit a PR?

onuralpszr commented 1 month ago

@xaristeidou may I see branch + collab to test ?

onuralpszr commented 1 month ago

Let's fix that out quickly

xaristeidou commented 1 month ago

@onuralpszr PR #1454 with colab

onuralpszr commented 1 month ago

Fixed via https://github.com/roboflow/supervision/pull/1454