Shortcuts

draw_bounding_boxes

torchvision.utils.draw_bounding_boxes(image: Tensor, boxes: Tensor, labels: Optional[list[str]] = None, colors: Optional[Union[list[Union[str, tuple[int, int, int]]], str, tuple[int, int, int]]] = None, fill: Optional[bool] = False, width: int = 1, font: Optional[str] = None, font_size: Optional[int] = None, label_colors: Optional[Union[list[Union[str, tuple[int, int, int]]], str, tuple[int, int, int]]] = None, fill_labels: bool = False) Tensor[source]

Draws bounding boxes on given RGB image. The image values should be uint8 in [0, 255] or float in [0, 1]. If fill is True, Resulting Tensor should be saved as PNG image.

Parameters:
  • image (Tensor) – Tensor of shape (C, H, W) and dtype uint8 or float.

  • boxes (Tensor) – Tensor of size (N, 4) or (N, 8) containing bounding boxes. For (N, 4), the format is (xmin, ymin, xmax, ymax) and the boxes are absolute coordinates with respect to the image. In other words: 0 <= xmin < xmax < W and 0 <= ymin < ymax < H. For (N, 8), the format is (x1, y1, x2, y2, x3, y3, x4, y4) and the boxes are absolute coordinates with respect to the underlying object, so no need to verify the latter inequalities.

  • labels (List[str]) – List containing the labels of bounding boxes.

  • colors (color or list of colors, optional) – List containing the colors of the boxes or single color for all boxes. The color can be represented as PIL strings e.g. “red” or “#FF00FF”, or as RGB tuples e.g. (240, 10, 157). By default, random colors are generated for boxes.

  • fill (bool) – If True fills the bounding box with specified color.

  • width (int) – Width of bounding box.

  • font (str) – A filename containing a TrueType font. If the file is not found in this filename, the loader may also search in other directories, such as the fonts/ directory on Windows or /Library/Fonts/, /System/Library/Fonts/ and ~/Library/Fonts/ on macOS.

  • font_size (int) – The requested font size in points.

  • label_colors (color or list of colors, optional) – Colors for the label text. See the description of the colors argument for details. Defaults to the same colors used for the boxes, or to black if fill_labels is True.

  • fill_labels (bool) – If True fills the label background with specified box color (from the colors parameter). Default: False.

Returns:

Image Tensor of dtype uint8 with bounding boxes plotted.

Return type:

img (Tensor[C, H, W])

Examples using draw_bounding_boxes:

Repurposing masks into bounding boxes

Repurposing masks into bounding boxes

Visualization utilities

Visualization utilities

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources