参考资料 ultralytics/utils/plotting.py
备注
该文件可在https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/utils/plotting .py 上获取。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!
ultralytics.utils.plotting.Colors
Ultralytics 默认调色板 https://ultralytics.com/.
该类提供了使用Ultralytics 调色板的方法,包括将十六进制颜色代码转换为 RGB 值。
属性
名称 | 类型 | 说明 |
---|---|---|
palette |
list of tuple
|
RGB 颜色值列表。 |
n |
int
|
调色板中颜色的数量。 |
pose_palette |
ndarray
|
一个特定的调色板数组,其 dtype 为 np.uint8。 |
源代码 ultralytics/utils/plotting.py
__call__(i, bgr=False)
__init__()
将颜色初始化为十六进制 = matplotlib.colors.TABLEAU_COLORS.values().
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.Annotator
Ultralytics 用于训练/评估镶嵌图和 JPG 文件以及预测注释的注释器。
属性
名称 | 类型 | 说明 |
---|---|---|
im |
Image.Image or numpy array
|
要注释的图像。 |
pil |
bool
|
使用 PIL 还是 cv2 绘制注释。 |
font |
truetype or load_default
|
用于文本注释的字体。 |
lw |
float
|
绘图线宽。 |
skeleton |
List[List[int]]
|
关键点的骨架结构。 |
limb_color |
List[int]
|
四肢的调色板 |
kpt_color |
List[int]
|
关键点的调色板。 |
源代码 ultralytics/utils/plotting.py
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 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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
|
__init__(im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc')
用图像和线宽以及关键点和肢体的调色板初始化注释器类。
源代码 ultralytics/utils/plotting.py
box_label(box, label='', color=(128, 128, 128), txt_color=(255, 255, 255), rotated=False)
在图片上添加一个 xyxy 框,并加上标签。
源代码 ultralytics/utils/plotting.py
display_analytics(im0, text, txt_color, bg_color, margin)
显示停车场的总体统计数据 参数: im0 (ndarray):推理图像 text (dict):标签字典 txt_color (bgr 颜色):文本前景显示颜色 bg_color (bgr 颜色):文本背景显示颜色 margin(int):文本与矩形之间的间隙,以便更好地显示文本
源代码 ultralytics/utils/plotting.py
display_objects_labels(im0, text, txt_color, bg_color, x_center, y_center, margin)
在停车场管理应用程序中显示边界框标签。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
im0 |
ndarray
|
推理图像 |
所需 |
text |
str
|
对象/类名 |
所需 |
txt_color |
bgr color
|
文本前景的显示颜色 |
所需 |
bg_color |
bgr color
|
文本背景的显示颜色 |
所需 |
x_center |
float
|
边界框的 x 位置中心点 |
所需 |
y_center |
float
|
y 边界框中心点的位置 |
所需 |
margin |
int
|
文字与矩形之间留有间隙,以便更好地显示 |
所需 |
源代码 ultralytics/utils/plotting.py
draw_centroid_and_tracks(track, color=(255, 0, 255), track_thickness=2)
绘制中心点和轨迹。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
track |
list
|
用于轨迹显示的物体跟踪点 |
所需 |
color |
tuple
|
轨道线颜色 |
(255, 0, 255)
|
track_thickness |
int
|
轨迹线粗细值 |
2
|
源代码 ultralytics/utils/plotting.py
draw_region(reg_pts=None, color=(0, 255, 0), thickness=5)
绘制区域线。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
reg_pts |
list
|
区域点数(2 号线点数,4 号区域点数) |
None
|
color |
tuple
|
区域颜色值 |
(0, 255, 0)
|
thickness |
int
|
区域面积厚度值 |
5
|
源代码 ultralytics/utils/plotting.py
draw_specific_points(keypoints, indices=[2, 5, 7], shape=(640, 640), radius=2, conf_thres=0.25)
绘制体育馆步数计数的具体关键点。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
keypoints |
list
|
要绘制的关键点数据列表 |
所需 |
indices |
list
|
要绘制的关键点 id 列表 |
[2, 5, 7]
|
shape |
tuple
|
用于模型推理的 imgsz |
(640, 640)
|
radius |
int
|
关键点半径值 |
2
|
源代码 ultralytics/utils/plotting.py
estimate_pose_angle(a, b, c)
staticmethod
计算物体的姿态角
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
a |
float)
|
姿势点 a 的值 |
所需 |
b |
float
|
姿态点 b 的值 |
所需 |
c |
float
|
c 点的位置值 |
所需 |
返回:
名称 | 类型 | 说明 |
---|---|---|
angle |
degree
|
三点间角度的度数 |
源代码 ultralytics/utils/plotting.py
fromarray(im)
get_bbox_dimension(bbox=None)
计算边界框的面积。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
bbox |
tuple
|
边界框坐标,格式为 (x_min、y_min、x_max、y_max)。 |
None
|
返回:
名称 | 类型 | 说明 |
---|---|---|
angle |
degree
|
三点间角度的度数 |
源代码 ultralytics/utils/plotting.py
kpts(kpts, shape=(640, 640), radius=5, kpt_line=True, conf_thres=0.25)
在图像上绘制关键点
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
kpts |
tensor
|
预测关键点的形状 [17, 3]。每个关键点都有(x、y、置信度)。 |
所需 |
shape |
tuple
|
图像形状的元组(h, w),其中 h 是高度,w 是宽度。 |
(640, 640)
|
radius |
int
|
绘制关键点的半径。默认值为 5。 |
5
|
kpt_line |
bool
|
如果为 True,函数将为人体姿势绘制连接关键点的线条。 连接线。默认为 True。 |
True
|
备注
kpt_line=True
目前仅支持人体姿态绘图。
源代码 ultralytics/utils/plotting.py
masks(masks, colors, im_gpu, alpha=0.5, retina_masks=False)
在图像上绘制遮罩。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
masks |
tensor
|
cuda 上的预测掩码,形状:[n, h, w] |
所需 |
colors |
List[List[Int]]
|
预测掩码的颜色,[[r, g, b] * n] |
所需 |
im_gpu |
tensor
|
图像为 cuda 格式,形状为[3, h, w], range:[0, 1] |
所需 |
alpha |
float
|
遮罩透明度:0.0 完全透明,1.0 不透明 |
0.5
|
retina_masks |
bool
|
是否使用高分辨率掩码。默认为 "假"。 |
False
|
源代码 ultralytics/utils/plotting.py
plot_angle_and_count_and_stage(angle_text, count_text, stage_text, center_kpt, line_thickness=2)
绘制姿势角度、计数值和步进阶段图。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
angle_text |
str
|
监测锻炼的角度值 |
所需 |
count_text |
str
|
用于监测锻炼的计数值 |
所需 |
stage_text |
str
|
锻炼监测的阶段性决定 |
所需 |
center_kpt |
int
|
用于运动监测的中心点姿势指数 |
所需 |
line_thickness |
int
|
文字显示厚度 |
2
|
源代码 ultralytics/utils/plotting.py
plot_distance_and_line(distance_m, distance_mm, centroids, line_color, centroid_color)
在框架上绘制距离和直线。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
distance_m |
float
|
两个 bbox 中心点之间的距离(米)。 |
所需 |
distance_mm |
float
|
两个 bbox 中心点之间的距离(毫米)。 |
所需 |
centroids |
list
|
边框中心点数据 |
所需 |
line_color |
RGB
|
距离线颜色 |
所需 |
centroid_color |
RGB
|
边界框中心点颜色。 |
所需 |
源代码 ultralytics/utils/plotting.py
queue_counts_display(label, points=None, region_color=(255, 255, 255), txt_color=(0, 0, 0), fontsize=0.7)
在以点为中心的图像上显示队列计数,字体大小和颜色可自定义。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
label |
str
|
队列计数标签 |
所需 |
points |
tuple
|
用于计算中心点的区域点,以显示文本 |
None
|
region_color |
RGB
|
队列区域颜色 |
(255, 255, 255)
|
txt_color |
RGB
|
文本显示颜色 |
(0, 0, 0)
|
fontsize |
float
|
文本字体大小 |
0.7
|
源代码 ultralytics/utils/plotting.py
rectangle(xy, fill=None, outline=None, width=1)
result()
save(filename='image.jpg')
seg_bbox(mask, mask_color=(255, 0, 255), det_label=None, track_label=None)
用于以边界框形状绘制分割对象的函数。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
mask |
list
|
用于实例分割区域绘图的掩码数据列表 |
所需 |
mask_color |
tuple
|
蒙版前景色 |
(255, 0, 255)
|
det_label |
str
|
检测标签文本 |
None
|
track_label |
str
|
跟踪标签文本 |
None
|
源代码 ultralytics/utils/plotting.py
show(title=None)
text(xy, text, txt_color=(255, 255, 255), anchor='top', box_style=False)
使用 PIL 或 cv2 为图像添加文字。
源代码 ultralytics/utils/plotting.py
visioneye(box, center_point, color=(235, 219, 11), pin_color=(255, 0, 255), thickness=2, pins_radius=10)
具有精确的人眼视力测绘和绘图功能。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
box |
list
|
边界框坐标 |
所需 |
center_point |
tuple
|
视觉中心点 |
所需 |
color |
tuple
|
对象中心点和线条颜色值 |
(235, 219, 11)
|
pin_color |
tuple
|
视觉点颜色值 |
(255, 0, 255)
|
thickness |
int
|
线条粗细的 int 值 |
2
|
pins_radius |
int
|
视觉点半径值 |
10
|
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.plot_labels(boxes, cls, names=(), save_dir=Path(''), on_plot=None)
绘制训练标签,包括类直方图和方框统计图。
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.save_one_box(xyxy, im, file=Path('im.jpg'), gain=1.02, pad=10, square=False, BGR=False, save=True)
将图像裁剪保存为{文件},裁剪尺寸为多个{gain}和{pad}像素。保存和/或返回裁剪。
该函数获取一个边界框和一幅图像,然后根据边界框保存图像的裁剪部分。 保存图像的裁剪部分。可选择裁剪成正方形,该函数还允许对边界框进行增益和填充调整。 调整边界框。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
xyxy |
Tensor or list
|
tensor 或列表,以 xyxy 格式表示边界框。 |
所需 |
im |
ndarray
|
输入图像。 |
所需 |
file |
Path
|
保存裁剪图像的路径。默认为 "im.jpg"。 |
Path('im.jpg')
|
gain |
float
|
用于增大边界框大小的乘法因子。默认为 1.02。 |
1.02
|
pad |
int
|
边框宽度和高度的像素数。默认为 10。 |
10
|
square |
bool
|
如果为 True,边框将转换为正方形。默认为 "假"。 |
False
|
BGR |
bool
|
如果为 True,图像将以 BGR 格式保存,否则将以 RGB 格式保存。默认为 "假"。 |
False
|
save |
bool
|
如果为 True,裁剪后的图像将保存到磁盘。默认为 True。 |
True
|
返回:
类型 | 说明 |
---|---|
ndarray
|
裁剪后的图片。 |
示例
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.plot_images(images, batch_idx, cls, bboxes=np.zeros(0, dtype=np.float32), confs=None, masks=np.zeros(0, dtype=np.uint8), kpts=np.zeros((0, 51), dtype=np.float32), paths=None, fname='images.jpg', names=None, on_plot=None, max_subplots=16, save=True, conf_thres=0.25)
绘制带标签的图像网格
源代码 ultralytics/utils/plotting.py
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 |
|
ultralytics.utils.plotting.plot_results(file='path/to/results.csv', dir='', segment=False, pose=False, classify=False, on_plot=None)
绘制 CSV 结果文件中的训练结果。该功能支持各种类型的数据,包括分割、姿态估计和分类、 姿势估计和分类。绘图以 "results.png "的形式保存在 CSV 所在的目录中。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
file |
str
|
包含训练结果的 CSV 文件的路径。默认为 "path/to/results.csv"。 |
'path/to/results.csv'
|
dir |
str
|
如果未提供 "file",则 CSV 文件所在的目录。默认为""。 |
''
|
segment |
bool
|
指示数据是否用于分割的标志。默认为 "假"。 |
False
|
pose |
bool
|
指示数据是否用于姿态估计的标志。默认为 "假"。 |
False
|
classify |
bool
|
指示数据是否用于分类的标志。默认为 "假"。 |
False
|
on_plot |
callable
|
绘图后执行的回调函数。以文件名为参数。 默认为 "无"。 |
None
|
示例
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.plt_color_scatter(v, f, bins=20, cmap='viridis', alpha=0.8, edgecolors='none')
根据二维直方图绘制点着色的散点图。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
v |
array - like
|
X 轴的数值。 |
所需 |
f |
array - like
|
y 轴的数值。 |
所需 |
bins |
int
|
直方图的箱数。默认为 20。 |
20
|
cmap |
str
|
散点图的颜色图。默认为 "viridis"。 |
'viridis'
|
alpha |
float
|
散点图的 Alpha 值。默认值为 0.8。 |
0.8
|
edgecolors |
str
|
散点图的边缘颜色。默认为 "无"。 |
'none'
|
例如
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.plot_tune_results(csv_file='tune_results.csv')
绘制存储在 "tune_results.csv "文件中的演化结果。该函数会为 CSV 文件中的每个键 中的每个关键字生成散点图,并根据适合度得分进行颜色编码。表现最好的配置会在图中突出显示。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
csv_file |
str
|
包含调整结果的 CSV 文件的路径。默认为 "tune_results.csv"。 |
'tune_results.csv'
|
例如
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.output_to_target(output, max_det=300)
将模型输出转换为目标格式 [batch_id、class_id、x、y、w、h、conf],以便绘图。
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.output_to_rotated_target(output, max_det=300)
将模型输出转换为目标格式 [batch_id、class_id、x、y、w、h、conf],以便绘图。
源代码 ultralytics/utils/plotting.py
ultralytics.utils.plotting.feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp'))
在推理过程中可视化给定模型模块的特征图。
参数
名称 | 类型 | 说明 | 默认值 |
---|---|---|---|
x |
Tensor
|
可视化特征。 |
所需 |
module_type |
str
|
模块类型。 |
所需 |
stage |
int
|
模型中的模块阶段。 |
所需 |
n |
int
|
绘制的最大特征图数量。默认为 32。 |
32
|
save_dir |
Path
|
保存结果的目录。默认为 Path('runs/detect/exp')。 |
Path('runs/detect/exp')
|