Reference for ultralytics/utils/instance.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/instance.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.instance.Bboxes
A class for handling bounding boxes.
The class supports various bounding box formats like 'xyxy', 'xywh', and 'ltwh'. Bounding box data should be provided in numpy arrays.
Attributes:
Name | Type | Description |
---|---|---|
bboxes |
ndarray
|
The bounding boxes stored in a 2D numpy array. |
format |
str
|
The format of the bounding boxes ('xyxy', 'xywh', or 'ltwh'). |
Note
This class does not handle normalization or denormalization of bounding boxes.
Source code in ultralytics/utils/instance.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 |
|
__getitem__(index)
Retrieve a specific bounding box or a set of bounding boxes using indexing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int, slice, or np.ndarray
|
The index, slice, or boolean array to select the desired bounding boxes. |
required |
Returns:
Name | Type | Description |
---|---|---|
Bboxes |
Bboxes
|
A new Bboxes object containing the selected bounding boxes. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the indexed bounding boxes do not form a 2-dimensional matrix. |
Note
When using boolean indexing, make sure to provide a boolean array with the same length as the number of bounding boxes.
Source code in ultralytics/utils/instance.py
__init__(bboxes, format='xyxy')
Initializes the Bboxes class with bounding box data in a specified format.
Source code in ultralytics/utils/instance.py
__len__()
add(offset)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset |
tuple | list | int
|
the offset for four coords. |
required |
Source code in ultralytics/utils/instance.py
areas()
concatenate(boxes_list, axis=0)
classmethod
Concatenate a list of Bboxes objects into a single Bboxes object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
boxes_list |
List[Bboxes]
|
A list of Bboxes objects to concatenate. |
required |
axis |
int
|
The axis along which to concatenate the bounding boxes. Defaults to 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
Bboxes |
Bboxes
|
A new Bboxes object containing the concatenated bounding boxes. |
Note
The input should be a list or tuple of Bboxes objects.
Source code in ultralytics/utils/instance.py
convert(format)
Converts bounding box format from one type to another.
Source code in ultralytics/utils/instance.py
mul(scale)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scale |
tuple | list | int
|
the scale for four coords. |
required |
Source code in ultralytics/utils/instance.py
ultralytics.utils.instance.Instances
Container for bounding boxes, segments, and keypoints of detected objects in an image.
Attributes:
Name | Type | Description |
---|---|---|
_bboxes |
Bboxes
|
Internal object for handling bounding box operations. |
keypoints |
ndarray
|
keypoints(x, y, visible) with shape [N, 17, 3]. Default is None. |
normalized |
bool
|
Flag indicating whether the bounding box coordinates are normalized. |
segments |
ndarray
|
Segments array with shape [N, 1000, 2] after resampling. |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes |
ndarray
|
An array of bounding boxes with shape [N, 4]. |
required |
segments |
list | ndarray
|
A list or array of object segments. Default is None. |
None
|
keypoints |
ndarray
|
An array of keypoints with shape [N, 17, 3]. Default is None. |
None
|
bbox_format |
str
|
The format of bounding boxes ('xywh' or 'xyxy'). Default is 'xywh'. |
'xywh'
|
normalized |
bool
|
Whether the bounding box coordinates are normalized. Default is True. |
True
|
Examples:
# Create an Instances object
instances = Instances(
bboxes=np.array([[10, 10, 30, 30], [20, 20, 40, 40]]),
segments=[np.array([[5, 5], [10, 10]]), np.array([[15, 15], [20, 20]])],
keypoints=np.array([[[5, 5, 1], [10, 10, 1]], [[15, 15, 1], [20, 20, 1]]])
)
Note
The bounding box format is either 'xywh' or 'xyxy', and is determined by the bbox_format
argument.
This class does not perform input validation, and it assumes the inputs are well-formed.
Source code in ultralytics/utils/instance.py
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 |
|
bbox_areas
property
Calculate the area of bounding boxes.
bboxes
property
Return bounding boxes.
__getitem__(index)
Retrieve a specific instance or a set of instances using indexing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int, slice, or np.ndarray
|
The index, slice, or boolean array to select the desired instances. |
required |
Returns:
Name | Type | Description |
---|---|---|
Instances |
Instances
|
A new Instances object containing the selected bounding boxes, segments, and keypoints if present. |
Note
When using boolean indexing, make sure to provide a boolean array with the same length as the number of instances.
Source code in ultralytics/utils/instance.py
__init__(bboxes, segments=None, keypoints=None, bbox_format='xywh', normalized=True)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes |
ndarray
|
bboxes with shape [N, 4]. |
required |
segments |
list | ndarray
|
segments. |
None
|
keypoints |
ndarray
|
keypoints(x, y, visible) with shape [N, 17, 3]. |
None
|
Source code in ultralytics/utils/instance.py
__len__()
add_padding(padw, padh)
Handle rect and mosaic situation.
Source code in ultralytics/utils/instance.py
clip(w, h)
Clips bounding boxes, segments, and keypoints values to stay within image boundaries.
Source code in ultralytics/utils/instance.py
concatenate(instances_list, axis=0)
classmethod
Concatenates a list of Instances objects into a single Instances object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instances_list |
List[Instances]
|
A list of Instances objects to concatenate. |
required |
axis |
int
|
The axis along which the arrays will be concatenated. Defaults to 0. |
0
|
Returns:
Name | Type | Description |
---|---|---|
Instances |
Instances
|
A new Instances object containing the concatenated bounding boxes, segments, and keypoints if present. |
Note
The Instances
objects in the list should have the same properties, such as
the format of the bounding boxes, whether keypoints are present, and if the
coordinates are normalized.
Source code in ultralytics/utils/instance.py
convert_bbox(format)
denormalize(w, h)
Denormalizes boxes, segments, and keypoints from normalized coordinates.
Source code in ultralytics/utils/instance.py
fliplr(w)
Reverses the order of the bounding boxes and segments horizontally.
Source code in ultralytics/utils/instance.py
flipud(h)
Flips the coordinates of bounding boxes, segments, and keypoints vertically.
Source code in ultralytics/utils/instance.py
normalize(w, h)
Normalize bounding boxes, segments, and keypoints to image dimensions.
Source code in ultralytics/utils/instance.py
remove_zero_area_boxes()
Remove zero-area boxes, i.e. after clipping some boxes may have zero width or height.
This removes them.
Source code in ultralytics/utils/instance.py
scale(scale_w, scale_h, bbox_only=False)
This might be similar with denormalize func but without normalized sign.
Source code in ultralytics/utils/instance.py
update(bboxes, segments=None, keypoints=None)
Updates instance variables.