MCPcopy
hub / github.com/ultralytics/ultralytics / plot_images

Function plot_images

ultralytics/utils/plotting.py:717–911  ·  view source on GitHub ↗

Plot image grid with labels, bounding boxes, masks, and keypoints. Args: labels (dict[str, Any]): Dictionary containing detection data with keys like 'cls', 'bboxes', 'conf', 'masks', 'keypoints', 'batch_idx', 'img'. images (torch.Tensor | np.ndarray): Batch of image

(
    labels: dict[str, Any],
    images: torch.Tensor | np.ndarray = np.zeros((0, 3, 640, 640), dtype=np.float32),
    paths: list[str] | None = None,
    fname: str = "images.jpg",
    names: dict[int, str] | None = None,
    on_plot: Callable | None = None,
    max_size: int = 1920,
    max_subplots: int = 16,
    save: bool = True,
    conf_thres: float = 0.25,
    show_labels: bool = True,
    show_conf: bool = True,
)

Source from the content-addressed store, hash-verified

715
716@threaded
717def plot_images(
718 labels: dict[str, Any],
719 images: torch.Tensor | np.ndarray = np.zeros((0, 3, 640, 640), dtype=np.float32),
720 paths: list[str] | None = None,
721 fname: str = "images.jpg",
722 names: dict[int, str] | None = None,
723 on_plot: Callable | None = None,
724 max_size: int = 1920,
725 max_subplots: int = 16,
726 save: bool = True,
727 conf_thres: float = 0.25,
728 show_labels: bool = True,
729 show_conf: bool = True,
730) -> np.ndarray | None:
731 """Plot image grid with labels, bounding boxes, masks, and keypoints.
732
733 Args:
734 labels (dict[str, Any]): Dictionary containing detection data with keys like 'cls', 'bboxes', 'conf', 'masks',
735 'keypoints', 'batch_idx', 'img'.
736 images (torch.Tensor | np.ndarray): Batch of images to plot. Shape: (batch_size, channels, height, width).
737 paths (list[str] | None): List of file paths for each image in the batch.
738 fname (str): Output filename for the plotted image grid.
739 names (dict[int, str] | None): Dictionary mapping class indices to class names.
740 on_plot (Callable | None): Callback function to be called after saving the plot.
741 max_size (int): Maximum size of the output image grid.
742 max_subplots (int): Maximum number of subplots in the image grid.
743 save (bool): Whether to save the plotted image grid to a file.
744 conf_thres (float): Confidence threshold for displaying detections.
745 show_labels (bool): Whether to display class labels.
746 show_conf (bool): Whether to display confidence values.
747
748 Returns:
749 (np.ndarray | None): Plotted image grid as a numpy array if save is False, None otherwise.
750
751 Notes:
752 This function supports both tensor and numpy array inputs. It will automatically
753 convert tensor inputs to numpy arrays for processing.
754
755 Channel Support:
756 - 1 channel: Grayscale
757 - 2 channels: Third channel added as zeros
758 - 3 channels: Used as-is (standard RGB)
759 - 4+ channels: Cropped to first 3 channels
760 """
761 for k in {"cls", "bboxes", "conf", "masks", "keypoints", "batch_idx", "images", "semantic_mask"}:
762 if k not in labels:
763 continue
764 if k == "cls" and labels[k].ndim == 2:
765 labels[k] = labels[k].squeeze(1) # squeeze if shape is (n, 1)
766 if isinstance(labels[k], torch.Tensor):
767 labels[k] = labels[k].cpu().numpy()
768
769 cls = labels.get("cls", np.zeros(0, dtype=np.int64))
770 batch_idx = labels.get("batch_idx", np.zeros(cls.shape, dtype=np.int64))
771 bboxes = labels.get("bboxes", np.zeros(0, dtype=np.float32))
772 confs = labels.get("conf", None)
773 masks = labels.get("masks", np.zeros(0, dtype=np.uint8))
774 kpts = labels.get("keypoints", np.zeros(0, dtype=np.float32))

Callers 9

plot_val_samplesMethod · 0.90
plot_predictionsMethod · 0.90
plot_training_samplesMethod · 0.90
plot_predictionsMethod · 0.90
plot_val_samplesMethod · 0.90
plot_predictionsMethod · 0.90
plot_training_samplesMethod · 0.90
plot_predictionsMethod · 0.90
plot_matchesMethod · 0.85

Calls 13

rectangleMethod · 0.95
textMethod · 0.95
box_labelMethod · 0.95
kptsMethod · 0.95
fromarrayMethod · 0.95
semantic_maskMethod · 0.95
AnnotatorClass · 0.85
getMethod · 0.80
tolistMethod · 0.80
numpyMethod · 0.45
cpuMethod · 0.45
concatenateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…