Skip to content

Visualizers

MarkVisualizer

A class for visualizing different marks including bounding boxes, masks, polygons, and labels.

Parameters:

Name Type Description Default
line_thickness int

The thickness of the lines for boxes and polygons.

2
mask_opacity float

The opacity level for masks.

0.1
text_scale float

The scale of the text for labels.

0.6
Source code in maestro/visualizers.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class MarkVisualizer:
    """
    A class for visualizing different marks including bounding boxes, masks, polygons,
    and labels.

    Parameters:
        line_thickness (int): The thickness of the lines for boxes and polygons.
        mask_opacity (float): The opacity level for masks.
        text_scale (float): The scale of the text for labels.
    """
    def __init__(
        self,
        line_thickness: int = 2,
        mask_opacity: float = 0.1,
        text_scale: float = 0.6
    ) -> None:
        self.box_annotator = sv.BoundingBoxAnnotator(
            color_lookup=sv.ColorLookup.INDEX,
            thickness=line_thickness)
        self.mask_annotator = sv.MaskAnnotator(
            color_lookup=sv.ColorLookup.INDEX,
            opacity=mask_opacity)
        self.polygon_annotator = sv.PolygonAnnotator(
            color_lookup=sv.ColorLookup.INDEX,
            thickness=line_thickness)
        self.label_annotator = sv.LabelAnnotator(
            color=sv.Color.black(),
            text_color=sv.Color.white(),
            color_lookup=sv.ColorLookup.INDEX,
            text_position=sv.Position.CENTER_OF_MASS,
            text_scale=text_scale)

    def visualize(
        self,
        image: np.ndarray,
        marks: sv.Detections,
        with_box: bool = False,
        with_mask: bool = False,
        with_polygon: bool = True,
        with_label: bool = True
    ) -> np.ndarray:
        """
        Visualizes annotations on an image.

        This method takes an image and an instance of sv.Detections, and overlays
        the specified types of marks (boxes, masks, polygons, labels) on the image.

        Parameters:
            image (np.ndarray): The image on which to overlay annotations.
            marks (sv.Detections): The detection results containing the annotations.
            with_box (bool): Whether to draw bounding boxes. Defaults to False.
            with_mask (bool): Whether to overlay masks. Defaults to False.
            with_polygon (bool): Whether to draw polygons. Defaults to True.
            with_label (bool): Whether to add labels. Defaults to True.

        Returns:
            np.ndarray: The annotated image.
        """
        annotated_image = image.copy()
        if with_box:
            annotated_image = self.box_annotator.annotate(
                scene=annotated_image, detections=marks)
        if with_mask:
            annotated_image = self.mask_annotator.annotate(
                scene=annotated_image, detections=marks)
        if with_polygon:
            annotated_image = self.polygon_annotator.annotate(
                scene=annotated_image, detections=marks)
        if with_label:
            labels = list(map(str, range(len(marks))))
            annotated_image = self.label_annotator.annotate(
                scene=annotated_image, detections=marks, labels=labels)
        return annotated_image

visualize(image, marks, with_box=False, with_mask=False, with_polygon=True, with_label=True)

Visualizes annotations on an image.

This method takes an image and an instance of sv.Detections, and overlays the specified types of marks (boxes, masks, polygons, labels) on the image.

Parameters:

Name Type Description Default
image ndarray

The image on which to overlay annotations.

required
marks Detections

The detection results containing the annotations.

required
with_box bool

Whether to draw bounding boxes. Defaults to False.

False
with_mask bool

Whether to overlay masks. Defaults to False.

False
with_polygon bool

Whether to draw polygons. Defaults to True.

True
with_label bool

Whether to add labels. Defaults to True.

True

Returns:

Type Description
ndarray

np.ndarray: The annotated image.

Source code in maestro/visualizers.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def visualize(
    self,
    image: np.ndarray,
    marks: sv.Detections,
    with_box: bool = False,
    with_mask: bool = False,
    with_polygon: bool = True,
    with_label: bool = True
) -> np.ndarray:
    """
    Visualizes annotations on an image.

    This method takes an image and an instance of sv.Detections, and overlays
    the specified types of marks (boxes, masks, polygons, labels) on the image.

    Parameters:
        image (np.ndarray): The image on which to overlay annotations.
        marks (sv.Detections): The detection results containing the annotations.
        with_box (bool): Whether to draw bounding boxes. Defaults to False.
        with_mask (bool): Whether to overlay masks. Defaults to False.
        with_polygon (bool): Whether to draw polygons. Defaults to True.
        with_label (bool): Whether to add labels. Defaults to True.

    Returns:
        np.ndarray: The annotated image.
    """
    annotated_image = image.copy()
    if with_box:
        annotated_image = self.box_annotator.annotate(
            scene=annotated_image, detections=marks)
    if with_mask:
        annotated_image = self.mask_annotator.annotate(
            scene=annotated_image, detections=marks)
    if with_polygon:
        annotated_image = self.polygon_annotator.annotate(
            scene=annotated_image, detections=marks)
    if with_label:
        labels = list(map(str, range(len(marks))))
        annotated_image = self.label_annotator.annotate(
            scene=annotated_image, detections=marks, labels=labels)
    return annotated_image