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
__getitem__
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
Add offset to bounding box coordinates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset | int | tuple | list | Offset(s) for four coordinates. If int, the same offset is applied to all coordinates. | required |
Source code in ultralytics/utils/instance.py
areas
Return box areas.
Source code in ultralytics/utils/instance.py
concatenate 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
Converts bounding box format from one type to another.
Source code in ultralytics/utils/instance.py
mul
Multiply bounding box coordinates by scale factor(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scale | int | tuple | list | Scale factor(s) for four coordinates. If int, the same scale is applied to all coordinates. | 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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bboxes | ndarray | Bounding boxes, shape [N, 4]. | required |
segments | list | ndarray | Segmentation masks. Defaults to None. | None |
keypoints | ndarray | Keypoints, shape [N, 17, 3] and format (x, y, visible). Defaults to None. | None |
bbox_format | str | Format of bboxes. Defaults to "xywh". | 'xywh' |
normalized | bool | Whether the coordinates are normalized. Defaults to True. | True |
Source code in ultralytics/utils/instance.py
__getitem__
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
__len__
add_padding
Handle rect and mosaic situation.
Source code in ultralytics/utils/instance.py
clip
Clips bounding boxes, segments, and keypoints values to stay within image boundaries.
Source code in ultralytics/utils/instance.py
concatenate 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
denormalize
Denormalizes boxes, segments, and keypoints from normalized coordinates.
Source code in ultralytics/utils/instance.py
fliplr
Reverses the order of the bounding boxes and segments horizontally.
Source code in ultralytics/utils/instance.py
flipud
Flips the coordinates of bounding boxes, segments, and keypoints vertically.
Source code in ultralytics/utils/instance.py
normalize
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.
Source code in ultralytics/utils/instance.py
scale
Similar to denormalize func but without normalized sign.
Source code in ultralytics/utils/instance.py
update
Updates instance variables.