tf.keras.utils.bounding_boxes.clip_to_image_size

Clips bounding boxes to be within the image dimensions.

bounding_boxes A dictionary with 'boxes' shape (N, 4) or (batch, N, 4) and 'labels' shape (N,) or (batch, N,).
height Image height.
width Image width.
bounding_box_format The format of the input bounding boxes. Defaults to "xyxy".

Clipped bounding boxes.

Example:

boxes = {"boxes": np.array([[-10, -20, 150, 160], [50, 40, 70, 80]]),
         "labels": np.array([0, 1])}
clipped_boxes = keras.utils.bounding_boxes.clip_to_image_size(
    boxes, height=100, width=120,
)
# Output will have boxes clipped to the image boundaries, and labels
# potentially adjusted if the clipped area becomes zero