Converts bounding boxes between formats.
tf.keras.utils.bounding_boxes.convert_format(
boxes, source, target, height=None, width=None, dtype='float32'
)
Supported formats (case-insensitive):
"xyxy": [left, top, right, bottom]
"yxyx": [top, left, bottom, right]
"xywh": [left, top, width, height]
"center_xywh": [center_x, center_y, width, height]
"center_yxhw": [center_y, center_x, height, width]
"rel_xyxy", "rel_yxyx", "rel_xywh", "rel_center_xywh": Relative
versions of the above formats, where coordinates are normalized
to the range [0, 1] based on the image height and width.
Args |
boxes
|
Bounding boxes tensor/array or dictionary of boxes and
labels.
|
source
|
Source format string.
|
target
|
Target format string.
|
height
|
Image height (required for relative target format).
|
width
|
Image width (required for relative target format).
|
dtype
|
Data type for conversion (optional).
|
Raises |
ValueError
|
For invalid formats, shapes, or missing dimensions.
|
Example:
boxes = np.array([[10, 20, 30, 40], [50, 60, 70, 80]])
# Convert from 'xyxy' to 'xywh' format
boxes_xywh = keras.utils.bounding_boxes.convert_format(
boxes, source='xyxy', target='xywh'
) # Output: [[10. 20. 20. 20.], [50. 60. 20. 20.]]
# Convert to relative 'rel_xyxy' format
boxes_rel_xyxy = keras.utils.bounding_boxes.convert_format(
boxes, source='xyxy', target='rel_xyxy', height=200, width=300
) # Output: [[0.03333334 0.1 0.1 0.2 ],
#[0.16666667 0.3 0.23333333 0.4 ]]