imgutils.detect.visual

Overview:

This module provides functionality for visualizing object detection results on images. It includes tools for drawing bounding boxes, labels, and confidence scores on detected objects.

The main function detection_visualize() can be used to visualize detection results from various object detection models, with customizable appearance settings like font size, padding, and label visibility.

See imgutils.detect.head.detect_heads() and imgutils.detect.person.detect_person() for examples.

detection_visualize

imgutils.detect.visual.detection_visualize(image: str | PathLike | bytes | bytearray | BinaryIO | Image, detection: List[Tuple[Tuple[float, float, float, float], str, float] | Tuple[Tuple[float, float, float, float], str, float, ndarray]], labels: List[str] | None = None, text_padding: int = 6, fontsize: int = 12, max_short_edge_size: int | None = None, mask_alpha: float = 0.5, fp=None, no_label: bool = False)[source]

Visualize object detection results by drawing bounding boxes, masks, and labels on an image.

This function takes detection results (bounding boxes and/or masks) and renders them on the input image, with customizable appearance settings. It supports both bounding box and instance segmentation results.

Parameters:
  • image (ImageTyping) – Input image to visualize detections on. Can be a PIL Image, numpy array, or path to image file.

  • detection (List[Union[BBoxWithScoreAndLabel, MaskWithScoreAndLabel]]) – List of detection results, each containing bounding box coordinates, label, confidence score, and optionally a segmentation mask. The coordinates should be in pixels, not normalized.

  • labels (Optional[List[str]]) – List of predefined labels. If None, labels will be extracted from detection results.

  • text_padding (int) – Padding around label text in pixels.

  • fontsize (int) – Font size for label text.

  • max_short_edge_size (Optional[int]) – Maximum size of shortest image edge. If specified, image will be resized while maintaining aspect ratio.

  • mask_alpha (float) – Transparency level for mask visualization (0.0 to 1.0).

  • fp (matplotlib.font_manager.FontProperties or None) – Font properties for matplotlib font. Only used if matplotlib is available.

  • no_label (bool) – If True, suppresses drawing of labels.

Returns:

PIL Image with visualized detection results.

Return type:

PIL.Image.Image

Examples::
>>> from imgutils.detect import detect_heads, detection_visualize
>>> from imgutils.data import load_image
>>>
>>> # Basic usage
>>> image = load_image("path/to/image.jpg")
>>> detections = detect_heads(image)
>>> visualized = detection_visualize(image, detections)
>>> visualized.save("output.png")

See imgutils.detect.head.detect_heads() and imgutils.detect.person.detect_person() for examples.