General UNIHIKER

YOLO Issue

userHead Matha.Goram 2024-08-23 18:00:00 1037 Views2 Replies

I am trying to run the first script at How Beginners Can Quickly Run YOLOv8 for Real-Time Recognition on a Laptop with the following statements direct from the article:

 

from ultralytics import YOLO 

# Load a pretrained YOLO model (recommended for training)

model = YOLO("yolov8n.pt") 

# Perform object detection on an image using the model

results = model("https://ultralytics.com/images/bus.jpg") 

# Save results to disk

results[0].save(filename=f"result_bus.jpg")

 

The first three statements run without any issue but the last statement produces the following error:

 

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

/tmp/ipykernel_2417/2241203627.py in <module>

1 # Save results to disk

----> 2 results[0].save(filename=f"result_bus.jpg")


 

/usr/local/lib/python3.7/dist-packages/ultralytics/utils/__init__.py in __getattr__(self, attr)

134 """Custom attribute access error message with helpful information."""

135 name = self.__class__.__name__

--> 136 raise AttributeError(f"'{name}' object has no attribute '{attr}'. See valid attributes below.\n{self.__doc__}")

137

138


 

AttributeError: 'Results' object has no attribute 'save'. See valid attributes below.


 

A class for storing and manipulating inference results.

 

Args:

orig_img (numpy.ndarray): The original image as a numpy array.

path (str): The path to the image file.

names (dict): A dictionary of class names.

boxes (torch.tensor, optional): A 2D tensor of bounding box coordinates for each detection.

masks (torch.tensor, optional): A 3D tensor of detection masks, where each mask is a binary image.

probs (torch.tensor, optional): A 1D tensor of probabilities of each class for classification task.

keypoints (List[List[float]], optional): A list of detected keypoints for each object.


 

Attributes:

orig_img (numpy.ndarray): The original image as a numpy array.

orig_shape (tuple): The original image shape in (height, width) format.

boxes (Boxes, optional): A Boxes object containing the detection bounding boxes.

masks (Masks, optional): A Masks object containing the detection masks.

probs (Probs, optional): A Probs object containing probabilities of each class for classification task.

names (dict): A dictionary of class names.

path (str): The path to the image file.

keypoints (Keypoints, optional): A Keypoints object containing detected keypoints for each object.

speed (dict): A dictionary of preprocess, inference and postprocess speeds in milliseconds per image.

_keys (tuple): A tuple of attribute names for non-empty attributes.

 

What would be the recommended workaround? Thanks.

 

Regards.
 

 

2024-08-23 19:51:15

Please refer to this sample code. It would help if you used Anaconda to create a dedicated environment for yolov8.

https://docs.ultralytics.com/models/yolov8/#performance-metrics

userHeadPic jenna
2024-08-23 18:11:28

When I print(results), I don't see any value for bus (viz. 5) assigned to any attribute:

 

[ultralytics.engine.results.Results object with attributes:


 

boxes: ultralytics.engine.results.Boxes object

keypoints: None

keys: ['boxes']

masks: None

names: {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}

orig_img: array([[[119, 146, 172],

[121, 148, 174],

[122, 152, 177],

...,


 

...,

[ 95, 85, 91],

[ 96, 86, 92],

[ 98, 88, 94]]], dtype=uint8)

orig_shape: (1080, 810)

path: '/root/opencv/bus.jpg'

probs: None

save_dir: None

speed: {'preprocess': 94.56586837768555, 'inference': 3410.3922843933105, 'postprocess': 162.766695022583}]

userHeadPic Matha.Goram