参考资料 ultralytics/engine/results.py
备注
该文件可在https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/engine/results .py。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!
ultralytics.engine.results.BaseTensor
垒球 SimpleClass
tensor 基类,带有便于操作和设备处理的附加方法。
源代码 ultralytics/engine/results.py
shape
property
返回数据的形状tensor 。
__getitem__(idx)
__init__(data, orig_shape)
使用数据和原始形状初始化 BaseTensor。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
data |
Tensor | ndarray
|
预测,如 bboxes、掩码和关键点。 |
所需 |
orig_shape |
tuple
|
图像的原始形状。 |
所需 |
源代码 ultralytics/engine/results.py
__len__()
cpu()
cuda()
numpy()
ultralytics.engine.results.Results
垒球 SimpleClass
用于存储和处理推理结果的类。
属性
名称 | 类型 | 说明 |
---|---|---|
orig_img |
ndarray
|
原始图像的 numpy 数组。 |
orig_shape |
tuple
|
原始图像的形状,格式为(高、宽)。 |
boxes |
Boxes
|
包含检测边界框的对象。 |
masks |
Masks
|
包含检测掩码的对象。 |
probs |
Probs
|
包含分类任务中类别概率的对象。 |
keypoints |
Keypoints
|
包含每个对象的检测关键点的对象。 |
speed |
dict
|
预处理、推理和后处理速度词典(毫秒/图像)。 |
names |
dict
|
类名词典 |
path |
str
|
图像文件的路径。 |
方法
名称 | 说明 |
---|---|
update |
根据新的检测结果更新对象属性。 |
cpu |
返回包含 CPU 内存中所有张量的 Results 对象副本。 |
numpy |
返回结果对象的副本,其中所有张量均为 numpy 数组。 |
cuda |
返回包含 GPU 内存中所有张量的 Results 对象副本。 |
to |
返回带有指定设备和 dtype 上张量的 Results 对象副本。 |
new |
返回一个具有相同图像、路径和名称的新结果对象。 |
plot |
将检测结果绘制在输入图像上,并返回注释图像。 |
show |
在屏幕上显示带注释的结果。 |
save |
将注释结果保存到文件中。 |
verbose |
返回每个任务的日志字符串,详细说明检测和分类情况。 |
save_txt |
将检测结果保存到文本文件中。 |
save_crop |
保存裁剪后的检测图像。 |
tojson |
将检测结果转换为 JSON 格式。 |
源代码 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 431 432 433 434 435 436 |
|
__getitem__(idx)
__init__(orig_img, path, names, boxes=None, masks=None, probs=None, keypoints=None, obb=None)
初始化结果类。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
orig_img |
ndarray
|
原始图像的 numpy 数组。 |
所需 |
path |
str
|
图像文件的路径。 |
所需 |
names |
dict
|
类名字典。 |
所需 |
boxes |
tensor
|
每个检测的边界框坐标的二维tensor 。 |
None
|
masks |
tensor
|
检测掩码的 3Dtensor ,其中每个掩码都是二值图像。 |
None
|
probs |
tensor
|
分类任务中每个类别概率的一维tensor 。 |
None
|
keypoints |
tensor
|
每个检测点关键点坐标的二维tensor 。 |
None
|
obb |
tensor
|
每个检测的定向边界框坐标的二维tensor 。 |
None
|
源代码 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)
在输入的 RGB 图像上绘制检测结果。接受 numpy 数组(cv2)或 PIL 图像。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
conf |
bool
|
是否绘制检测置信度得分。 |
True
|
line_width |
float
|
边界框的线宽。如果无,则按图像大小缩放。 |
None
|
font_size |
float
|
文本的字体大小。如果无,则按图像大小缩放。 |
None
|
font |
str
|
文本使用的字体。 |
'Arial.ttf'
|
pil |
bool
|
是否将图像作为 PIL 图像返回。 |
False
|
img |
ndarray
|
如果没有,则绘制到原始图像。 |
None
|
im_gpu |
Tensor
|
在 gpu 中以形状(1, 3, 640, 640)对图像进行归一化处理,以便更快地绘制掩膜图。 |
None
|
kpt_radius |
int
|
绘制关键点的半径。默认值为 5。 |
5
|
kpt_line |
bool
|
是否绘制连接关键点的线条。 |
True
|
labels |
bool
|
是否绘制边界框的标签。 |
True
|
boxes |
bool
|
是否绘制边界框。 |
True
|
masks |
bool
|
是否绘制面具。 |
True
|
probs |
bool
|
是否绘制分类概率 |
True
|
show |
bool
|
是否直接显示注释图像。 |
False
|
save |
bool
|
是否将注释的图像保存到 |
False
|
filename |
str
|
如果保存为 True,则保存图像的文件名。 |
None
|
返回:
类型 | 说明 |
---|---|
ndarray
|
注释图像的 numpy 数组。 |
示例
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
源代码 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_crop(save_dir, file_name=Path('im.jpg'))
将裁剪后的预测保存到 save_dir/cls/file_name.jpg
.
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
save_dir |
str | Path
|
保存路径。 |
所需 |
file_name |
str | Path
|
文件名。 |
Path('im.jpg')
|
源代码 ultralytics/engine/results.py
save_txt(txt_file, save_conf=False)
将预测结果保存到 txt 文件中。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
txt_file |
str
|
txt 文件路径。 |
所需 |
save_conf |
bool
|
是否保存信心分数。 |
False
|
源代码 ultralytics/engine/results.py
show(*args, **kwargs)
summary(normalize=False, decimals=5)
将结果转换为摘要格式。
源代码 ultralytics/engine/results.py
to(*args, **kwargs)
tojson(normalize=False, decimals=5)
update(boxes=None, masks=None, probs=None, obb=None)
更新结果对象的方框、掩码和 probs 属性。
源代码 ultralytics/engine/results.py
verbose()
返回每个任务的日志字符串。
源代码 ultralytics/engine/results.py
ultralytics.engine.results.Boxes
垒球 BaseTensor
管理检测框,方便访问和操作检测框坐标、置信度分数、类别 标识符和可选的跟踪 ID。支持多种方框坐标格式,包括绝对和 规范化形式。
属性
名称 | 类型 | 说明 |
---|---|---|
data |
Tensor
|
原始tensor 包含检测框及其相关数据。 |
orig_shape |
tuple
|
原始图像尺寸的元组(高、宽),用于标准化。 |
is_track |
bool
|
表示跟踪 ID 是否包含在包装盒数据中。 |
属性
xyxy (torch.Tensor | numpy.ndarray):格式为 [x1, y1, x2, y2] 的方框。
conf (torch.Tensor | numpy.ndarray):每个方框的置信度得分。
cls (torch.Tensor | numpy.ndarray):每个方框的类标签:每个方框的类标签。
id (torch.Tensor | numpy.ndarray,可选):每个方框的跟踪 ID(如果有)。
xywh (torch.Tensor | numpy.ndarray):按要求计算的 [x, y, width, height] 格式的方框。
xyxyn (torch.Tensor | numpy.ndarray):归一化 [x1, y1, x2, y2] 方框,相对于 orig_shape
.
xywhn (torch.Tensor | numpy.ndarray):归一化的 [x、y、宽、高] 方框,相对于 orig_shape
.
方法
名称 | 说明 |
---|---|
cpu |
将方框移动到 CPU 内存中。 |
numpy |
将方框转换为 numpy 数组格式。 |
cuda |
将方框移动到 CUDA(GPU)内存。 |
to |
将方框移动到指定设备。 |
源代码 ultralytics/engine/results.py
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 521 522 523 524 525 526 |
|
cls
property
返回方框的类值。
conf
property
返回方框的置信度值。
id
property
返回盒子的轨道 ID(如果有)。
xywh
cached
property
以 xywh 格式返回方框。
xywhn
cached
property
以 xywh 格式返回按原始图像大小归一化的方框。
xyxy
property
以 xyxy 格式返回方框。
xyxyn
cached
property
以 xyxy 格式返回按原始图像大小归一化的方框。
__init__(boxes, orig_shape)
初始化方框类。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
boxes |
Tensor | ndarray
|
tensor 或 numpy 数组,其中包含检测框,形状为 形状 (num_boxes, 6) 或 (num_boxes, 7)。最后两列包含置信度和类别值。 如果存在,倒数第三列包含轨迹 ID。 |
所需 |
orig_shape |
tuple
|
原始图像大小,格式为(高、宽)。 |
所需 |
源代码 ultralytics/engine/results.py
ultralytics.engine.results.Masks
垒球 BaseTensor
用于存储和操作检测掩码的类。
属性
名称 | 类型 | 说明 |
---|---|---|
xy |
list
|
以像素坐标表示的线段列表。 |
xyn |
list
|
标准化片段列表。 |
方法
名称 | 说明 |
---|---|
cpu |
返回 CPU 内存中的掩码tensor 。 |
numpy |
以 numpy 数组形式返回掩码tensor 。 |
cuda |
返回 GPU 内存中的掩码tensor 。 |
to |
返回具有指定设备和 dtype 的掩码tensor 。 |
源代码 ultralytics/engine/results.py
xy
cached
property
以像素坐标返回线段。
xyn
cached
property
返回归一化分段。
__init__(masks, orig_shape)
ultralytics.engine.results.Keypoints
垒球 BaseTensor
用于存储和操作检测关键点的类。
属性
名称 | 类型 | 说明 |
---|---|---|
xy |
Tensor
|
关键点集合,包含每次检测的 x、y 坐标。 |
xyn |
Tensor
|
xy 的标准化版本,坐标范围为 [0,1]。 |
conf |
Tensor
|
与关键点相关的置信度值(如果有),否则为 "无"。 |
方法
名称 | 说明 |
---|---|
cpu |
返回 CPU 内存中关键点tensor 的副本。 |
numpy |
以 numpy 数组形式返回关键点tensor 的副本。 |
cuda |
返回 GPU 内存中关键点tensor 的副本。 |
to |
用指定的设备和 dtype 返回关键点tensor 的副本。 |
源代码 ultralytics/engine/results.py
conf
cached
property
返回关键点的置信度值(如果有),否则为空。
xy
cached
property
返回关键点的 x、y 坐标。
xyn
cached
property
返回关键点的归一化 x、y 坐标。
__init__(keypoints, orig_shape)
使用检测关键点和原始图像尺寸初始化关键点对象。
源代码 ultralytics/engine/results.py
ultralytics.engine.results.Probs
垒球 BaseTensor
用于存储和处理分类预测的类。
属性
名称 | 类型 | 说明 |
---|---|---|
top1 |
int
|
前 1 级的指数。 |
top5 |
list[int]
|
前 5 个等级的指数。 |
top1conf |
Tensor
|
对前 1 级充满信心。 |
top5conf |
Tensor
|
前五名班级的机密 |
方法
名称 | 说明 |
---|---|
cpu |
返回 CPU 内存中 probstensor 的副本。 |
numpy |
以 numpy 数组形式返回 probstensor 的副本。 |
cuda |
返回 GPU 内存中 probstensor 的副本。 |
to |
返回带有指定设备和 dtype 的 probstensor 的副本。 |
源代码 ultralytics/engine/results.py
top1
cached
property
返回顶部 1 的索引。
top1conf
cached
property
返回前 1 名的置信度。
top5
cached
property
返回前 5 名的指数。
top5conf
cached
property
返回前 5 名的保密信息。
__init__(probs, orig_shape=None)
ultralytics.engine.results.OBB
垒球 BaseTensor
用于存储和操作定向边框(OBB)的类。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
boxes |
Tensor | ndarray
|
tensor 或包含检测框的 numpy 数组、 形状为 (num_boxes, 7) 或 (num_boxes, 8)。最后两列包含置信度和类别值。 如果存在,倒数第三列包含轨迹 ID,左起第五列包含旋转。 |
所需 |
orig_shape |
tuple
|
原始图像大小,格式为(高、宽)。 |
所需 |
属性
名称 | 类型 | 说明 |
---|---|---|
xywhr |
Tensor | ndarray
|
x_center, y_center, width, height, rotation] 格式的方框。 |
conf |
Tensor | ndarray
|
方框的置信度值。 |
cls |
Tensor | ndarray
|
方框的类值。 |
id |
Tensor | ndarray
|
盒子的轨道 ID(如果有)。 |
xyxyxyxyn |
Tensor | ndarray
|
按原始图像大小归一化的 xyxyxyxy 格式旋转框。 |
xyxyxyxy |
Tensor | ndarray
|
以 xyxyxyxy 格式旋转的方框。 |
xyxy |
Tensor | ndarray
|
xyxyxyxy 格式的水平方框。 |
data |
Tensor
|
原始 OBBtensor (OBB 的别名 |
方法
名称 | 说明 |
---|---|
cpu |
将对象移至 CPU 内存。 |
numpy |
将对象转换为 numpy 数组。 |
cuda |
将对象移至 CUDA 内存。 |
to |
将对象移动到指定设备。 |
源代码 ultralytics/engine/results.py
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 744 745 746 747 748 749 |
|
cls
property
返回方框的类值。
conf
property
返回方框的置信度值。
id
property
返回盒子的轨道 ID(如果有)。
xywhr
property
以 xywhr 格式返回旋转后的方框。
xyxy
cached
property
以 xyxy 格式返回水平方框(N,4)。
同时接受torch 和 numpy 框。
xyxyxyxy
cached
property
以 xyxyxyxy 格式返回方框(N,4,2)。
xyxyxyxyn
cached
property
以 xyxyxyxy 格式返回方框(N,4,2)。
__init__(boxes, orig_shape)
初始化方框类。