Reference for ultralytics/utils/plotting.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/plotting.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.plotting.Colors
Ultralytics default color palette https://ultralytics.com/.
This class provides methods to work with the Ultralytics color palette, including converting hex color codes to RGB values.
Attributes:
Name | Type | Description |
---|---|---|
palette |
list of tuple
|
List of RGB color values. |
n |
int
|
The number of colors in the palette. |
pose_palette |
array
|
A specific color palette array with dtype np.uint8. |
Source code in ultralytics/utils/plotting.py
__call__(i, bgr=False)
__init__()
Initialize colors as hex = matplotlib.colors.TABLEAU_COLORS.values().
Source code in ultralytics/utils/plotting.py
hex2rgb(h)
staticmethod
ultralytics.utils.plotting.Annotator
Ultralytics Annotator for train/val mosaics and JPGs and predictions annotations.
Attributes:
Name | Type | Description |
---|---|---|
im |
Image.Image or numpy array
|
The image to annotate. |
pil |
bool
|
Whether to use PIL or cv2 for drawing annotations. |
font |
truetype or load_default
|
Font used for text annotations. |
lw |
float
|
Line width for drawing. |
skeleton |
List[List[int]]
|
Skeleton structure for keypoints. |
limb_color |
List[int]
|
Color palette for limbs. |
kpt_color |
List[int]
|
Color palette for keypoints. |
Source code in ultralytics/utils/plotting.py
60 61 62 63 64 65 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 |
|
__init__(im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc')
Initialize the Annotator class with image and line width along with color palette for keypoints and limbs.
Source code in ultralytics/utils/plotting.py
box_label(box, label='', color=(128, 128, 128), txt_color=(255, 255, 255))
Add one xyxy box to image with label.
Source code in ultralytics/utils/plotting.py
fromarray(im)
kpts(kpts, shape=(640, 640), radius=5, kpt_line=True)
Plot keypoints on the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kpts |
tensor
|
Predicted keypoints with shape [17, 3]. Each keypoint has (x, y, confidence). |
required |
shape |
tuple
|
Image shape as a tuple (h, w), where h is the height and w is the width. |
(640, 640)
|
radius |
int
|
Radius of the drawn keypoints. Default is 5. |
5
|
kpt_line |
bool
|
If True, the function will draw lines connecting keypoints for human pose. Default is True. |
True
|
Note: kpt_line=True
currently only supports human pose plotting.
Source code in ultralytics/utils/plotting.py
masks(masks, colors, im_gpu, alpha=0.5, retina_masks=False)
Plot masks on image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
masks |
tensor
|
Predicted masks on cuda, shape: [n, h, w] |
required |
colors |
List[List[Int]]
|
Colors for predicted masks, [[r, g, b] * n] |
required |
im_gpu |
tensor
|
Image is in cuda, shape: [3, h, w], range: [0, 1] |
required |
alpha |
float
|
Mask transparency: 0.0 fully transparent, 1.0 opaque |
0.5
|
retina_masks |
bool
|
Whether to use high resolution masks or not. Defaults to False. |
False
|
Source code in ultralytics/utils/plotting.py
rectangle(xy, fill=None, outline=None, width=1)
result()
text(xy, text, txt_color=(255, 255, 255), anchor='top', box_style=False)
Adds text to an image using PIL or cv2.
Source code in ultralytics/utils/plotting.py
ultralytics.utils.plotting.plot_labels(boxes, cls, names=(), save_dir=Path(''), on_plot=None)
Plot training labels including class histograms and box statistics.
Source code in 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)
Save image crop as {file} with crop size multiple {gain} and {pad} pixels. Save and/or return crop.
This function takes a bounding box and an image, and then saves a cropped portion of the image according to the bounding box. Optionally, the crop can be squared, and the function allows for gain and padding adjustments to the bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xyxy |
Tensor or list
|
A tensor or list representing the bounding box in xyxy format. |
required |
im |
ndarray
|
The input image. |
required |
file |
Path
|
The path where the cropped image will be saved. Defaults to 'im.jpg'. |
Path('im.jpg')
|
gain |
float
|
A multiplicative factor to increase the size of the bounding box. Defaults to 1.02. |
1.02
|
pad |
int
|
The number of pixels to add to the width and height of the bounding box. Defaults to 10. |
10
|
square |
bool
|
If True, the bounding box will be transformed into a square. Defaults to False. |
False
|
BGR |
bool
|
If True, the image will be saved in BGR format, otherwise in RGB. Defaults to False. |
False
|
save |
bool
|
If True, the cropped image will be saved to disk. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
ndarray
|
The cropped image. |
Example
Source code in ultralytics/utils/plotting.py
ultralytics.utils.plotting.plot_images(images, batch_idx, cls, bboxes=np.zeros(0, dtype=np.float32), 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)
Plot image grid with labels.
Source code in ultralytics/utils/plotting.py
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 |
|
ultralytics.utils.plotting.plot_results(file='path/to/results.csv', dir='', segment=False, pose=False, classify=False, on_plot=None)
Plot training results from a results CSV file. The function supports various types of data including segmentation, pose estimation, and classification. Plots are saved as 'results.png' in the directory where the CSV is located.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
str
|
Path to the CSV file containing the training results. Defaults to 'path/to/results.csv'. |
'path/to/results.csv'
|
dir |
str
|
Directory where the CSV file is located if 'file' is not provided. Defaults to ''. |
''
|
segment |
bool
|
Flag to indicate if the data is for segmentation. Defaults to False. |
False
|
pose |
bool
|
Flag to indicate if the data is for pose estimation. Defaults to False. |
False
|
classify |
bool
|
Flag to indicate if the data is for classification. Defaults to False. |
False
|
on_plot |
callable
|
Callback function to be executed after plotting. Takes filename as an argument. Defaults to None. |
None
|
Example
Source code in ultralytics/utils/plotting.py
ultralytics.utils.plotting.plt_color_scatter(v, f, bins=20, cmap='viridis', alpha=0.8, edgecolors='none')
Plots a scatter plot with points colored based on a 2D histogram.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v |
array - like
|
Values for the x-axis. |
required |
f |
array - like
|
Values for the y-axis. |
required |
bins |
int
|
Number of bins for the histogram. Defaults to 20. |
20
|
cmap |
str
|
Colormap for the scatter plot. Defaults to 'viridis'. |
'viridis'
|
alpha |
float
|
Alpha for the scatter plot. Defaults to 0.8. |
0.8
|
edgecolors |
str
|
Edge colors for the scatter plot. Defaults to 'none'. |
'none'
|
Examples:
Source code in ultralytics/utils/plotting.py
ultralytics.utils.plotting.plot_tune_results(csv_file='tune_results.csv')
Plot the evolution results stored in an 'tune_results.csv' file. The function generates a scatter plot for each key in the CSV, color-coded based on fitness scores. The best-performing configurations are highlighted on the plots.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
csv_file |
str
|
Path to the CSV file containing the tuning results. Defaults to 'tune_results.csv'. |
'tune_results.csv'
|
Examples:
Source code in ultralytics/utils/plotting.py
ultralytics.utils.plotting.output_to_target(output, max_det=300)
Convert model output to target format [batch_id, class_id, x, y, w, h, conf] for plotting.
Source code in ultralytics/utils/plotting.py
ultralytics.utils.plotting.feature_visualization(x, module_type, stage, n=32, save_dir=Path('runs/detect/exp'))
Visualize feature maps of a given model module during inference.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
Features to be visualized. |
required |
module_type |
str
|
Module type. |
required |
stage |
int
|
Module stage within the model. |
required |
n |
int
|
Maximum number of feature maps to plot. Defaults to 32. |
32
|
save_dir |
Path
|
Directory to save results. Defaults to Path('runs/detect/exp'). |
Path('runs/detect/exp')
|