roboflow / supervision

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

call from_ultralytics except: 'Results' object has no attribute 'names' #1646

Closed Mao-Sky closed 3 weeks ago

Mao-Sky commented 3 weeks ago

Search before asking

Question

Console output

ultralytics version: 8.0.20 supervision version: 0.20.0 Ultralytics YOLOv8.0.20 Python-3.8.20 torch-2.4.1+cu121 CPU YOLOv8n summary (fused): 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs results: [Ultralytics YOLO <class 'ultralytics.yolo.engine.results.Boxes'> masks type: <class 'torch.Tensor'> shape: torch.Size([15, 6]) dtype: torch.float32

results[0]: tensor([[2.81000e+02, 3.83000e+02, 3.89000e+02, 4.52000e+02, 7.25721e-01, 2.00000e+00], [6.51000e+02, 2.82000e+02, 6.82000e+02, 3.08000e+02, 7.25578e-01, 2.00000e+00],

Traceback (most recent call last): File "d:/moyy/work/yao/yolov/hello_yolov/01.supervision_hello.py", line 18, in detections = sv.Detections.from_ultralytics(results) File "E:\Users\ProgramData\miniconda3\envs\yolo8\lib\site-packages\supervision\detection\core.py", line 261, in from_ultralytics class_names = np.array([ultralytics_results.names[i] for i in class_id]) File "E:\Users\ProgramData\miniconda3\envs\yolo8\lib\site-packages\supervision\detection\core.py", line 261, in class_names = np.array([ultralytics_results.names[i] for i in class_id]) File "E:\Users\ProgramData\miniconda3\envs\yolo8\lib\site-packages\ultralytics\yolo\engine\results.py", line 103, in getattr raise AttributeError(f""" AttributeError: 'Results' object has no attribute 'names'. Valid 'Results' object attributes and properties are:

        Attributes:
            boxes (Boxes, optional): A Boxes object containing the detection bounding boxes.
            masks (Masks, optional): A Masks object containing the detection masks.
            probs (torch.Tensor, optional): A tensor containing the detection class probabilities.
            orig_shape (tuple, optional): Original image size.

Python Code

import cv2
import supervision as sv

import ultralytics
from ultralytics import YOLO

print("ultralytics version: ", ultralytics.__version__)
print("supervision version: ", sv.__version__)

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

model = YOLO("yolov8n.pt")
results = model(image, device="cpu")
print("results: ", results)

results = results[0]
print("results[0]: ", results)
detections = sv.Detections.from_ultralytics(results)

Additional

No response