Reference for ultralytics/utils/instance.py
Note
Full source code for this file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/instance.py. Help us fix any issues you see by submitting a Pull Request 🛠️. Thank you 🙏!
ultralytics.utils.instance.Bboxes
Bounding Boxes class. Only numpy variables are supported.
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 |
|
__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
__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
Source code in ultralytics/utils/instance.py
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 |
|
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.