tf.keras.visualization.draw_bounding_boxes

Draws bounding boxes on images.

This function draws bounding boxes on a batch of images. It supports different bounding box formats and can optionally display class labels and confidences.

images A batch of images as a 4D tensor or NumPy array. Shape should be (batch_size, height, width, channels).
bounding_boxes A dictionary containing bounding box data. Should have the following keys:

  • boxes: A tensor or array of shape (batch_size, num_boxes, 4) containing the bounding box coordinates in the specified format.
  • labels: A tensor or array of shape (batch_size, num_boxes) containing the class labels for each bounding box.
  • confidences (Optional): A tensor or array of shape (batch_size, num_boxes) containing the confidence scores for each bounding box.
bounding_box_format A string specifying the format of the bounding boxes. See keras.utils.bounding_boxes.convert_format for supported formats (e.g., "xyxy", "yxyx", "xywh", "center_xywh", "centeryxhw", and the "rel*" variants).
class_mapping A dictionary mapping class IDs (integers) to class labels (strings). Used to display class labels next to the bounding boxes. Defaults to None (no labels displayed).
color A tuple or list representing the RGB color of the bounding boxes. For example, (255, 0, 0) for red. Defaults to (128, 128, 128).
line_thickness An integer specifying the thickness of the bounding box lines. Defaults to 2.
text_thickness An integer specifying the thickness of the text labels. Defaults to 1.
font_scale A float specifying the scale of the font used for text labels. Defaults to 1.0.
data_format A string, either "channels_last" or "channels_first", specifying the order of dimensions in the input images. Defaults to the image_data_format value found in your Keras config file at ~/.keras/keras.json. If you never set it, then it will be "channels_last".

A NumPy array of the annotated images with the bounding boxes drawn. The array will have the same shape as the input images.

ValueError If images is not a 4D tensor/array, if bounding_boxes is not a dictionary, or if bounding_boxes does not contain "boxes" and "labels" keys.
TypeError If bounding_boxes is not a dictionary.
ImportError If cv2 (OpenCV) is not installed.