Reference for ultralytics/engine/results.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/results.py. If you spot a problem please help fix it by contributing 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.
Attributes:
Name | Type | Description |
---|---|---|
orig_img |
ndarray
|
Original image as a numpy array. |
orig_shape |
tuple
|
Original image shape in (height, width) format. |
boxes |
Boxes
|
Object containing detection bounding boxes. |
masks |
Masks
|
Object containing detection masks. |
probs |
Probs
|
Object containing class probabilities for classification tasks. |
keypoints |
Keypoints
|
Object containing detected keypoints for each object. |
speed |
dict
|
Dictionary of preprocess, inference, and postprocess speeds (ms/image). |
names |
dict
|
Dictionary of class names. |
path |
str
|
Path to the image file. |
Methods:
Name | Description |
---|---|
update |
Updates object attributes with new detection results. |
cpu |
Returns a copy of the Results object with all tensors on CPU memory. |
numpy |
Returns a copy of the Results object with all tensors as numpy arrays. |
cuda |
Returns a copy of the Results object with all tensors on GPU memory. |
to |
Returns a copy of the Results object with tensors on a specified device and dtype. |
new |
Returns a new Results object with the same image, path, and names. |
plot |
Plots detection results on an input image, returning an annotated image. |
show |
Show annotated results to screen. |
save |
Save annotated results to file. |
verbose |
Returns a log string for each task, detailing detections and classifications. |
save_txt |
Saves detection results to a text file. |
save_crop |
Saves cropped detection images. |
tojson |
Converts detection results to JSON format. |
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 350 351 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 |
|
__getitem__(idx)
__init__(orig_img, path, names, boxes=None, masks=None, probs=None, keypoints=None, obb=None)
Initialize the Results class.
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 |
tensor
|
A 2D tensor of keypoint coordinates for each detection. |
None
|
obb |
tensor
|
A 2D tensor of oriented bounding box coordinates for each detection. |
None
|
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, show=False, save=False, filename=None)
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
|
show |
bool
|
Whether to display the annotated image directly. |
False
|
save |
bool
|
Whether to save the annotated image to |
False
|
filename |
str
|
Filename to save image to if save is True. |
None
|
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
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 |
|
save(filename=None, *args, **kwargs)
Save annotated results image.
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
show(*args, **kwargs)
summary(normalize=False, decimals=5)
Convert the results to a summarized format.
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, decimals=5)
update(boxes=None, masks=None, probs=None, obb=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
Manages detection boxes, providing easy access and manipulation of box coordinates, confidence scores, class identifiers, and optional tracking IDs. Supports multiple formats for box coordinates, including both absolute and normalized forms.
Attributes:
Name | Type | Description |
---|---|---|
data |
Tensor
|
The raw tensor containing detection boxes and their associated data. |
orig_shape |
tuple
|
The original image size as a tuple (height, width), used for normalization. |
is_track |
bool
|
Indicates whether tracking IDs are included in the box data. |
Properties
xyxy (torch.Tensor | numpy.ndarray): Boxes in [x1, y1, x2, y2] format.
conf (torch.Tensor | numpy.ndarray): Confidence scores for each box.
cls (torch.Tensor | numpy.ndarray): Class labels for each box.
id (torch.Tensor | numpy.ndarray, optional): Tracking IDs for each box, if available.
xywh (torch.Tensor | numpy.ndarray): Boxes in [x, y, width, height] format, calculated on demand.
xyxyn (torch.Tensor | numpy.ndarray): Normalized [x1, y1, x2, y2] boxes, relative to orig_shape
.
xywhn (torch.Tensor | numpy.ndarray): Normalized [x, y, width, height] boxes, relative to orig_shape
.
Methods:
Name | Description |
---|---|
cpu |
Moves the boxes to CPU memory. |
numpy |
Converts the boxes to a numpy array format. |
cuda |
Moves the boxes to CUDA (GPU) memory. |
to |
Moves the boxes to the specified device. |
Source code in ultralytics/engine/results.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
|
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.
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 |
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 |
---|---|---|
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
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.
__init__(probs, orig_shape=None)
Initialize the Probs class with classification probabilities and optional original shape of the image.
ultralytics.engine.results.OBB
Bases: BaseTensor
A class for storing and manipulating Oriented Bounding Boxes (OBB).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes |
Tensor | ndarray
|
A tensor or numpy array containing the detection boxes, with shape (num_boxes, 7) or (num_boxes, 8). The last two columns contain confidence and class values. If present, the third last column contains track IDs, and the fifth column from the left contains rotation. |
required |
orig_shape |
tuple
|
Original image size, in the format (height, width). |
required |
Attributes:
Name | Type | Description |
---|---|---|
xywhr |
Tensor | ndarray
|
The boxes in [x_center, y_center, width, height, rotation] 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). |
xyxyxyxyn |
Tensor | ndarray
|
The rotated boxes in xyxyxyxy format normalized by orig image size. |
xyxyxyxy |
Tensor | ndarray
|
The rotated boxes in xyxyxyxy format. |
xyxy |
Tensor | ndarray
|
The horizontal boxes in xyxyxyxy format. |
data |
Tensor
|
The raw OBB 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
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
|
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).
xywhr
property
Return the rotated boxes in xywhr format.
xyxy
cached
property
Return the horizontal boxes in xyxy format, (N, 4).
Accepts both torch and numpy boxes.
xyxyxyxy
cached
property
Return the boxes in xyxyxyxy format, (N, 4, 2).
xyxyxyxyn
cached
property
Return the boxes in xyxyxyxy format, (N, 4, 2).
__init__(boxes, orig_shape)
Initialize the Boxes class.