Reference for ultralytics/engine/results.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/results.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.engine.results.BaseTensor
Bases: SimpleClass
Base tensor class with additional methods for easy manipulation and device handling.
Source code in ultralytics/engine/results.py
shape
property
Return the shape of the data tensor.
__getitem__(idx)
__init__(data, orig_shape)
Initialize BaseTensor with data and original shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Tensor | ndarray
|
Predictions, such as bboxes, masks and keypoints. |
required |
orig_shape |
tuple
|
Original shape of image. |
required |
Source code in ultralytics/engine/results.py
__len__()
cpu()
cuda()
numpy()
to(*args, **kwargs)
Return a copy of the tensor with the specified device and dtype.
ultralytics.engine.results.Results
Bases: SimpleClass
A class for storing and manipulating inference results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
orig_img |
ndarray
|
The original image as a numpy array. |
required |
path |
str
|
The path to the image file. |
required |
names |
dict
|
A dictionary of class names. |
required |
boxes |
tensor
|
A 2D tensor of bounding box coordinates for each detection. |
None
|
masks |
tensor
|
A 3D tensor of detection masks, where each mask is a binary image. |
None
|
probs |
tensor
|
A 1D tensor of probabilities of each class for classification task. |
None
|
keypoints |
List[List[float]]
|
A list of detected keypoints for each object. |
None
|
Attributes:
Name | Type | Description |
---|---|---|
orig_img |
ndarray
|
The original image as a numpy array. |
orig_shape |
tuple
|
The original image shape in (height, width) format. |
boxes |
Boxes
|
A Boxes object containing the detection bounding boxes. |
masks |
Masks
|
A Masks object containing the detection masks. |
probs |
Probs
|
A Probs object containing probabilities of each class for classification task. |
keypoints |
Keypoints
|
A Keypoints object containing detected keypoints for each object. |
speed |
dict
|
A dictionary of preprocess, inference, and postprocess speeds in milliseconds per image. |
names |
dict
|
A dictionary of class names. |
path |
str
|
The path to the image file. |
_keys |
tuple
|
A tuple of attribute names for non-empty attributes. |
Source code in ultralytics/engine/results.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
|
__getitem__(idx)
__init__(orig_img, path, names, boxes=None, masks=None, probs=None, keypoints=None)
Initialize the Results class.
Source code in ultralytics/engine/results.py
__len__()
cpu()
cuda()
new()
numpy()
plot(conf=True, line_width=None, font_size=None, font='Arial.ttf', pil=False, img=None, im_gpu=None, kpt_radius=5, kpt_line=True, labels=True, boxes=True, masks=True, probs=True)
Plots the detection results on an input RGB image. Accepts a numpy array (cv2) or a PIL Image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
conf |
bool
|
Whether to plot the detection confidence score. |
True
|
line_width |
float
|
The line width of the bounding boxes. If None, it is scaled to the image size. |
None
|
font_size |
float
|
The font size of the text. If None, it is scaled to the image size. |
None
|
font |
str
|
The font to use for the text. |
'Arial.ttf'
|
pil |
bool
|
Whether to return the image as a PIL Image. |
False
|
img |
ndarray
|
Plot to another image. if not, plot to original image. |
None
|
im_gpu |
Tensor
|
Normalized image in gpu with shape (1, 3, 640, 640), for faster mask plotting. |
None
|
kpt_radius |
int
|
Radius of the drawn keypoints. Default is 5. |
5
|
kpt_line |
bool
|
Whether to draw lines connecting keypoints. |
True
|
labels |
bool
|
Whether to plot the label of bounding boxes. |
True
|
boxes |
bool
|
Whether to plot the bounding boxes. |
True
|
masks |
bool
|
Whether to plot the masks. |
True
|
probs |
bool
|
Whether to plot classification probability |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
A numpy array of the annotated image. |
Example
from PIL import Image
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
results = model('bus.jpg') # results list
for r in results:
im_array = r.plot() # plot a BGR numpy array of predictions
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
im.show() # show image
im.save('results.jpg') # save image
Source code in ultralytics/engine/results.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
save_crop(save_dir, file_name=Path('im.jpg'))
Save cropped predictions to save_dir/cls/file_name.jpg
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save_dir |
str | Path
|
Save path. |
required |
file_name |
str | Path
|
File name. |
Path('im.jpg')
|
Source code in ultralytics/engine/results.py
save_txt(txt_file, save_conf=False)
Save predictions into txt file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
txt_file |
str
|
txt file path. |
required |
save_conf |
bool
|
save confidence score or not. |
False
|
Source code in ultralytics/engine/results.py
to(*args, **kwargs)
Return a copy of the Results object with tensors on the specified device and dtype.
tojson(normalize=False)
Convert the object to JSON format.
Source code in ultralytics/engine/results.py
update(boxes=None, masks=None, probs=None)
Update the boxes, masks, and probs attributes of the Results object.
Source code in ultralytics/engine/results.py
verbose()
Return log string for each task.
Source code in ultralytics/engine/results.py
ultralytics.engine.results.Boxes
Bases: BaseTensor
A class for storing and manipulating detection boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes |
Tensor | ndarray
|
A tensor or numpy array containing the detection boxes, with shape (num_boxes, 6) or (num_boxes, 7). The last two columns contain confidence and class values. If present, the third last column contains track IDs. |
required |
orig_shape |
tuple
|
Original image size, in the format (height, width). |
required |
Attributes:
Name | Type | Description |
---|---|---|
xyxy |
Tensor | ndarray
|
The boxes in xyxy format. |
conf |
Tensor | ndarray
|
The confidence values of the boxes. |
cls |
Tensor | ndarray
|
The class values of the boxes. |
id |
Tensor | ndarray
|
The track IDs of the boxes (if available). |
xywh |
Tensor | ndarray
|
The boxes in xywh format. |
xyxyn |
Tensor | ndarray
|
The boxes in xyxy format normalized by original image size. |
xywhn |
Tensor | ndarray
|
The boxes in xywh format normalized by original image size. |
data |
Tensor
|
The raw bboxes tensor (alias for |
Methods:
Name | Description |
---|---|
cpu |
Move the object to CPU memory. |
numpy |
Convert the object to a numpy array. |
cuda |
Move the object to CUDA memory. |
to |
Move the object to the specified device. |
Source code in ultralytics/engine/results.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
|
boxes
property
Return the raw bboxes tensor (deprecated).
cls
property
Return the class values of the boxes.
conf
property
Return the confidence values of the boxes.
id
property
Return the track IDs of the boxes (if available).
xywh
cached
property
Return the boxes in xywh format.
xywhn
cached
property
Return the boxes in xywh format normalized by original image size.
xyxy
property
Return the boxes in xyxy format.
xyxyn
cached
property
Return the boxes in xyxy format normalized by original image size.
__init__(boxes, orig_shape)
Initialize the Boxes class.
Source code in ultralytics/engine/results.py
ultralytics.engine.results.Masks
Bases: BaseTensor
A class for storing and manipulating detection masks.
Attributes:
Name | Type | Description |
---|---|---|
segments |
list
|
Deprecated property for segments (normalized). |
xy |
list
|
A list of segments in pixel coordinates. |
xyn |
list
|
A list of normalized segments. |
Methods:
Name | Description |
---|---|
cpu |
Returns the masks tensor on CPU memory. |
numpy |
Returns the masks tensor as a numpy array. |
cuda |
Returns the masks tensor on GPU memory. |
to |
Returns the masks tensor with the specified device and dtype. |
Source code in ultralytics/engine/results.py
masks
property
Return the raw masks tensor. Deprecated; use data attribute instead.
segments
cached
property
Return segments (normalized). Deprecated; use xyn property instead.
xy
cached
property
Return segments in pixel coordinates.
xyn
cached
property
Return normalized segments.
__init__(masks, orig_shape)
Initialize the Masks class with the given masks tensor and original image shape.
ultralytics.engine.results.Keypoints
Bases: BaseTensor
A class for storing and manipulating detection keypoints.
Attributes:
Name | Type | Description |
---|---|---|
xy |
Tensor
|
A collection of keypoints containing x, y coordinates for each detection. |
xyn |
Tensor
|
A normalized version of xy with coordinates in the range [0, 1]. |
conf |
Tensor
|
Confidence values associated with keypoints if available, otherwise None. |
Methods:
Name | Description |
---|---|
cpu |
Returns a copy of the keypoints tensor on CPU memory. |
numpy |
Returns a copy of the keypoints tensor as a numpy array. |
cuda |
Returns a copy of the keypoints tensor on GPU memory. |
to |
Returns a copy of the keypoints tensor with the specified device and dtype. |
Source code in ultralytics/engine/results.py
conf
cached
property
Returns confidence values of keypoints if available, else None.
xy
cached
property
Returns x, y coordinates of keypoints.
xyn
cached
property
Returns normalized x, y coordinates of keypoints.
__init__(keypoints, orig_shape)
Initializes the Keypoints object with detection keypoints and original image size.
Source code in ultralytics/engine/results.py
ultralytics.engine.results.Probs
Bases: BaseTensor
A class for storing and manipulating classification predictions.
Attributes:
Name | Type | Description |
---|---|---|
top1 |
int
|
Index of the top 1 class. |
top5 |
list[int]
|
Indices of the top 5 classes. |
top1conf |
Tensor
|
Confidence of the top 1 class. |
top5conf |
Tensor
|
Confidences of the top 5 classes. |
Methods:
Name | Description |
---|---|
cpu |
Returns a copy of the probs tensor on CPU memory. |
numpy |
Returns a copy of the probs tensor as a numpy array. |
cuda |
Returns a copy of the probs tensor on GPU memory. |
to |
Returns a copy of the probs tensor with the specified device and dtype. |
Source code in ultralytics/engine/results.py
top1
cached
property
Return the index of top 1.
top1conf
cached
property
Return the confidence of top 1.
top5
cached
property
Return the indices of top 5.
top5conf
cached
property
Return the confidences of top 5.