Skip to content

Annotators

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> bounding_box_annotator = sv.BoundingBoxAnnotator()
>>> annotated_frame = bounding_box_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

bounding-box-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> corner_annotator = sv.BoxCornerAnnotator()
>>> annotated_frame = corner_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

box-corner-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> color_annotator = sv.ColorAnnotator()
>>> annotated_frame = color_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

box-mask-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> circle_annotator = sv.CircleAnnotator()
>>> annotated_frame = circle_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

circle-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> dot_annotator = sv.DotAnnotator()
>>> annotated_frame = dot_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

circle-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> triangle_annotator = sv.TriangleAnnotator()
>>> annotated_frame = triangle_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

triangle-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> ellipse_annotator = sv.EllipseAnnotator()
>>> annotated_frame = ellipse_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

ellipse-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> halo_annotator = sv.HaloAnnotator()
>>> annotated_frame = halo_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

ellipse-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> mask_annotator = sv.MaskAnnotator()
>>> annotated_frame = mask_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

mask-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> polygon_annotator = sv.PolygonAnnotator()
>>> annotated_frame = polygon_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

polygon-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
>>> annotated_frame = label_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

label-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> blur_annotator = sv.BlurAnnotator()
>>> annotated_frame = blur_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

blur-annotator-example

>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> pixelate_annotator = sv.PixelateAnnotator()
>>> annotated_frame = pixelate_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

pixelate-annotator-example

>>> import supervision as sv
>>> from ultralytics import YOLO

>>> model = YOLO('yolov8x.pt')

>>> trace_annotator = sv.TraceAnnotator()

>>> video_info = sv.VideoInfo.from_video_path(video_path='...')
>>> frames_generator = get_video_frames_generator(source_path='...')
>>> tracker = sv.ByteTrack()

>>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
...    for frame in frames_generator:
...        result = model(frame)[0]
...        detections = sv.Detections.from_ultralytics(result)
...        detections = tracker.update_with_detections(detections)
...        annotated_frame = trace_annotator.annotate(
...            scene=frame.copy(),
...            detections=detections)
...        sink.write_frame(frame=annotated_frame)

trace-annotator-example

>>> import supervision as sv
>>> from ultralytics import YOLO

>>> model = YOLO('yolov8x.pt')

>>> heat_map_annotator = sv.HeatMapAnnotator()

>>> video_info = sv.VideoInfo.from_video_path(video_path='...')
>>> frames_generator = get_video_frames_generator(source_path='...')

>>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
...    for frame in frames_generator:
...        result = model(frame)[0]
...        detections = sv.Detections.from_ultralytics(result)
...        annotated_frame = heat_map_annotator.annotate(
...            scene=frame.copy(),
...            detections=detections)
...        sink.write_frame(frame=annotated_frame)

trace-annotator-example

BoundingBoxAnnotator

Bases: BaseAnnotator

A class for drawing bounding boxes on an image using provided detections.

Source code in supervision/annotators/core.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class BoundingBoxAnnotator(BaseAnnotator):
    """
    A class for drawing bounding boxes on an image using provided detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        thickness: int = 2,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the bounding box lines.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.thickness: int = thickness
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with bounding boxes based on the provided detections.

        Args:
            scene (np.ndarray): The image where bounding boxes will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> bounding_box_annotator = sv.BoundingBoxAnnotator()
            >>> annotated_frame = bounding_box_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![bounding-box-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/bounding-box-annotator-example-purple.png)
        """
        for detection_idx in range(len(detections)):
            x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            cv2.rectangle(
                img=scene,
                pt1=(x1, y1),
                pt2=(x2, y2),
                color=color.as_bgr(),
                thickness=self.thickness,
            )
        return scene

__init__(color=ColorPalette.default(), thickness=2, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
thickness int

Thickness of the bounding box lines.

2
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    thickness: int = 2,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        thickness (int): Thickness of the bounding box lines.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.thickness: int = thickness
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with bounding boxes based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where bounding boxes will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> bounding_box_annotator = sv.BoundingBoxAnnotator()
>>> annotated_frame = bounding_box_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

bounding-box-annotator-example

Source code in supervision/annotators/core.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with bounding boxes based on the provided detections.

    Args:
        scene (np.ndarray): The image where bounding boxes will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> bounding_box_annotator = sv.BoundingBoxAnnotator()
        >>> annotated_frame = bounding_box_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![bounding-box-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/bounding-box-annotator-example-purple.png)
    """
    for detection_idx in range(len(detections)):
        x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        cv2.rectangle(
            img=scene,
            pt1=(x1, y1),
            pt2=(x2, y2),
            color=color.as_bgr(),
            thickness=self.thickness,
        )
    return scene

BoxCornerAnnotator

Bases: BaseAnnotator

A class for drawing box corners on an image using provided detections.

Source code in supervision/annotators/core.py
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
class BoxCornerAnnotator(BaseAnnotator):
    """
    A class for drawing box corners on an image using provided detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        thickness: int = 4,
        corner_length: int = 15,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the corner lines.
            corner_length (int): Length of each corner line.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.thickness: int = thickness
        self.corner_length: int = corner_length
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with box corners based on the provided detections.

        Args:
            scene (np.ndarray): The image where box corners will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> corner_annotator = sv.BoxCornerAnnotator()
            >>> annotated_frame = corner_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![box-corner-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/box-corner-annotator-example-purple.png)
        """
        for detection_idx in range(len(detections)):
            x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            corners = [(x1, y1), (x2, y1), (x1, y2), (x2, y2)]

            for x, y in corners:
                x_end = x + self.corner_length if x == x1 else x - self.corner_length
                cv2.line(
                    scene, (x, y), (x_end, y), color.as_bgr(), thickness=self.thickness
                )

                y_end = y + self.corner_length if y == y1 else y - self.corner_length
                cv2.line(
                    scene, (x, y), (x, y_end), color.as_bgr(), thickness=self.thickness
                )
        return scene

__init__(color=ColorPalette.default(), thickness=4, corner_length=15, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
thickness int

Thickness of the corner lines.

4
corner_length int

Length of each corner line.

15
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    thickness: int = 4,
    corner_length: int = 15,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        thickness (int): Thickness of the corner lines.
        corner_length (int): Length of each corner line.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.thickness: int = thickness
    self.corner_length: int = corner_length
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with box corners based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where box corners will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> corner_annotator = sv.BoxCornerAnnotator()
>>> annotated_frame = corner_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

box-corner-annotator-example

Source code in supervision/annotators/core.py
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with box corners based on the provided detections.

    Args:
        scene (np.ndarray): The image where box corners will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> corner_annotator = sv.BoxCornerAnnotator()
        >>> annotated_frame = corner_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![box-corner-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/box-corner-annotator-example-purple.png)
    """
    for detection_idx in range(len(detections)):
        x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        corners = [(x1, y1), (x2, y1), (x1, y2), (x2, y2)]

        for x, y in corners:
            x_end = x + self.corner_length if x == x1 else x - self.corner_length
            cv2.line(
                scene, (x, y), (x_end, y), color.as_bgr(), thickness=self.thickness
            )

            y_end = y + self.corner_length if y == y1 else y - self.corner_length
            cv2.line(
                scene, (x, y), (x, y_end), color.as_bgr(), thickness=self.thickness
            )
    return scene

ColorAnnotator

Bases: BaseAnnotator

A class for drawing box masks on an image using provided detections.

Source code in supervision/annotators/core.py
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
class ColorAnnotator(BaseAnnotator):
    """
    A class for drawing box masks on an image using provided detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        opacity: float = 0.5,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.color_lookup: ColorLookup = color_lookup
        self.opacity = opacity

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with box masks based on the provided detections.

        Args:
            scene (np.ndarray): The image where bounding boxes will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> color_annotator = sv.ColorAnnotator()
            >>> annotated_frame = color_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![box-mask-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/box-mask-annotator-example-purple.png)
        """
        mask_image = scene.copy()
        for detection_idx in range(len(detections)):
            x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            cv2.rectangle(
                img=scene,
                pt1=(x1, y1),
                pt2=(x2, y2),
                color=color.as_bgr(),
                thickness=-1,
            )
        scene = cv2.addWeighted(
            scene, self.opacity, mask_image, 1 - self.opacity, gamma=0
        )
        return scene

__init__(color=ColorPalette.default(), opacity=0.5, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
opacity float

Opacity of the overlay mask. Must be between 0 and 1.

0.5
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    opacity: float = 0.5,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.color_lookup: ColorLookup = color_lookup
    self.opacity = opacity

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with box masks based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where bounding boxes will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> color_annotator = sv.ColorAnnotator()
>>> annotated_frame = color_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

box-mask-annotator-example

Source code in supervision/annotators/core.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with box masks based on the provided detections.

    Args:
        scene (np.ndarray): The image where bounding boxes will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> color_annotator = sv.ColorAnnotator()
        >>> annotated_frame = color_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![box-mask-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/box-mask-annotator-example-purple.png)
    """
    mask_image = scene.copy()
    for detection_idx in range(len(detections)):
        x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        cv2.rectangle(
            img=scene,
            pt1=(x1, y1),
            pt2=(x2, y2),
            color=color.as_bgr(),
            thickness=-1,
        )
    scene = cv2.addWeighted(
        scene, self.opacity, mask_image, 1 - self.opacity, gamma=0
    )
    return scene

CircleAnnotator

Bases: BaseAnnotator

A class for drawing circle on an image using provided detections.

Source code in supervision/annotators/core.py
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
class CircleAnnotator(BaseAnnotator):
    """
    A class for drawing circle on an image using provided detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        thickness: int = 2,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the circle line.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """

        self.color: Union[Color, ColorPalette] = color
        self.thickness: int = thickness
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with circles based on the provided detections.

        Args:
            scene (np.ndarray): The image where box corners will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> circle_annotator = sv.CircleAnnotator()
            >>> annotated_frame = circle_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```


        ![circle-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/circle-annotator-example-purple.png)
        """
        for detection_idx in range(len(detections)):
            x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
            center = ((x1 + x2) // 2, (y1 + y2) // 2)
            distance = sqrt((x1 - center[0]) ** 2 + (y1 - center[1]) ** 2)
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            cv2.circle(
                img=scene,
                center=center,
                radius=int(distance),
                color=color.as_bgr(),
                thickness=self.thickness,
            )

        return scene

__init__(color=ColorPalette.default(), thickness=2, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
thickness int

Thickness of the circle line.

2
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    thickness: int = 2,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        thickness (int): Thickness of the circle line.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """

    self.color: Union[Color, ColorPalette] = color
    self.thickness: int = thickness
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with circles based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where box corners will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> circle_annotator = sv.CircleAnnotator()
>>> annotated_frame = circle_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

circle-annotator-example

Source code in supervision/annotators/core.py
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with circles based on the provided detections.

    Args:
        scene (np.ndarray): The image where box corners will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> circle_annotator = sv.CircleAnnotator()
        >>> annotated_frame = circle_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```


    ![circle-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/circle-annotator-example-purple.png)
    """
    for detection_idx in range(len(detections)):
        x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
        center = ((x1 + x2) // 2, (y1 + y2) // 2)
        distance = sqrt((x1 - center[0]) ** 2 + (y1 - center[1]) ** 2)
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        cv2.circle(
            img=scene,
            center=center,
            radius=int(distance),
            color=color.as_bgr(),
            thickness=self.thickness,
        )

    return scene

DotAnnotator

Bases: BaseAnnotator

A class for drawing dots on an image at specific coordinates based on provided detections.

Source code in supervision/annotators/core.py
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
class DotAnnotator(BaseAnnotator):
    """
    A class for drawing dots on an image at specific coordinates based on provided
    detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        radius: int = 4,
        position: Position = Position.CENTER,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            radius (int): Radius of the drawn dots.
            position (Position): The anchor position for placing the dot.
            color_lookup (ColorLookup): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.radius: int = radius
        self.position: Position = position
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with dots based on the provided detections.

        Args:
            scene (np.ndarray): The image where dots will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> dot_annotator = sv.DotAnnotator()
            >>> annotated_frame = dot_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![dot-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/dot-annotator-example-purple.png)
        """
        xy = detections.get_anchors_coordinates(anchor=self.position)
        for detection_idx in range(len(detections)):
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            center = (int(xy[detection_idx, 0]), int(xy[detection_idx, 1]))
            cv2.circle(scene, center, self.radius, color.as_bgr(), -1)
        return scene

__init__(color=ColorPalette.default(), radius=4, position=Position.CENTER, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
radius int

Radius of the drawn dots.

4
position Position

The anchor position for placing the dot.

CENTER
color_lookup ColorLookup

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    radius: int = 4,
    position: Position = Position.CENTER,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        radius (int): Radius of the drawn dots.
        position (Position): The anchor position for placing the dot.
        color_lookup (ColorLookup): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.radius: int = radius
    self.position: Position = position
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with dots based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where dots will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> dot_annotator = sv.DotAnnotator()
>>> annotated_frame = dot_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

dot-annotator-example

Source code in supervision/annotators/core.py
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with dots based on the provided detections.

    Args:
        scene (np.ndarray): The image where dots will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> dot_annotator = sv.DotAnnotator()
        >>> annotated_frame = dot_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![dot-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/dot-annotator-example-purple.png)
    """
    xy = detections.get_anchors_coordinates(anchor=self.position)
    for detection_idx in range(len(detections)):
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        center = (int(xy[detection_idx, 0]), int(xy[detection_idx, 1]))
        cv2.circle(scene, center, self.radius, color.as_bgr(), -1)
    return scene

TriangleAnnotator

Bases: BaseAnnotator

A class for drawing triangle markers on an image at specific coordinates based on provided detections.

Source code in supervision/annotators/core.py
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
class TriangleAnnotator(BaseAnnotator):
    """
    A class for drawing triangle markers on an image at specific coordinates based on
    provided detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        base: int = 10,
        height: int = 10,
        position: Position = Position.TOP_CENTER,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            base (int): The base width of the triangle.
            height (int): The height of the triangle.
            position (Position): The anchor position for placing the triangle.
            color_lookup (ColorLookup): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.base: int = base
        self.height: int = height
        self.position: Position = position
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with triangles based on the provided detections.

        Args:
            scene (np.ndarray): The image where triangles will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            np.ndarray: The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> triangle_annotator = sv.TriangleAnnotator()
            >>> annotated_frame = triangle_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![triangle-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/triangle-annotator-example.png)
        """
        xy = detections.get_anchors_coordinates(anchor=self.position)
        for detection_idx in range(len(detections)):
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            tip_x, tip_y = int(xy[detection_idx, 0]), int(xy[detection_idx, 1])
            vertices = np.array(
                [
                    [tip_x - self.base // 2, tip_y - self.height],
                    [tip_x + self.base // 2, tip_y - self.height],
                    [tip_x, tip_y],
                ],
                np.int32,
            )

            cv2.fillPoly(scene, [vertices], color.as_bgr())

        return scene

__init__(color=ColorPalette.default(), base=10, height=10, position=Position.TOP_CENTER, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
base int

The base width of the triangle.

10
height int

The height of the triangle.

10
position Position

The anchor position for placing the triangle.

TOP_CENTER
color_lookup ColorLookup

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    base: int = 10,
    height: int = 10,
    position: Position = Position.TOP_CENTER,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        base (int): The base width of the triangle.
        height (int): The height of the triangle.
        position (Position): The anchor position for placing the triangle.
        color_lookup (ColorLookup): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.base: int = base
    self.height: int = height
    self.position: Position = position
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with triangles based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where triangles will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

np.ndarray: The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> triangle_annotator = sv.TriangleAnnotator()
>>> annotated_frame = triangle_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

triangle-annotator-example

Source code in supervision/annotators/core.py
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with triangles based on the provided detections.

    Args:
        scene (np.ndarray): The image where triangles will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        np.ndarray: The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> triangle_annotator = sv.TriangleAnnotator()
        >>> annotated_frame = triangle_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![triangle-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/triangle-annotator-example.png)
    """
    xy = detections.get_anchors_coordinates(anchor=self.position)
    for detection_idx in range(len(detections)):
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        tip_x, tip_y = int(xy[detection_idx, 0]), int(xy[detection_idx, 1])
        vertices = np.array(
            [
                [tip_x - self.base // 2, tip_y - self.height],
                [tip_x + self.base // 2, tip_y - self.height],
                [tip_x, tip_y],
            ],
            np.int32,
        )

        cv2.fillPoly(scene, [vertices], color.as_bgr())

    return scene

EllipseAnnotator

Bases: BaseAnnotator

A class for drawing ellipses on an image using provided detections.

Source code in supervision/annotators/core.py
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
class EllipseAnnotator(BaseAnnotator):
    """
    A class for drawing ellipses on an image using provided detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        thickness: int = 2,
        start_angle: int = -45,
        end_angle: int = 235,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the ellipse lines.
            start_angle (int): Starting angle of the ellipse.
            end_angle (int): Ending angle of the ellipse.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.thickness: int = thickness
        self.start_angle: int = start_angle
        self.end_angle: int = end_angle
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with ellipses based on the provided detections.

        Args:
            scene (np.ndarray): The image where ellipses will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> ellipse_annotator = sv.EllipseAnnotator()
            >>> annotated_frame = ellipse_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![ellipse-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/ellipse-annotator-example-purple.png)
        """
        for detection_idx in range(len(detections)):
            x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            center = (int((x1 + x2) / 2), y2)
            width = x2 - x1
            cv2.ellipse(
                scene,
                center=center,
                axes=(int(width), int(0.35 * width)),
                angle=0.0,
                startAngle=self.start_angle,
                endAngle=self.end_angle,
                color=color.as_bgr(),
                thickness=self.thickness,
                lineType=cv2.LINE_4,
            )
        return scene

__init__(color=ColorPalette.default(), thickness=2, start_angle=-45, end_angle=235, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
thickness int

Thickness of the ellipse lines.

2
start_angle int

Starting angle of the ellipse.

-45
end_angle int

Ending angle of the ellipse.

235
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    thickness: int = 2,
    start_angle: int = -45,
    end_angle: int = 235,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        thickness (int): Thickness of the ellipse lines.
        start_angle (int): Starting angle of the ellipse.
        end_angle (int): Ending angle of the ellipse.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.thickness: int = thickness
    self.start_angle: int = start_angle
    self.end_angle: int = end_angle
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with ellipses based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where ellipses will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> ellipse_annotator = sv.EllipseAnnotator()
>>> annotated_frame = ellipse_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

ellipse-annotator-example

Source code in supervision/annotators/core.py
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with ellipses based on the provided detections.

    Args:
        scene (np.ndarray): The image where ellipses will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> ellipse_annotator = sv.EllipseAnnotator()
        >>> annotated_frame = ellipse_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![ellipse-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/ellipse-annotator-example-purple.png)
    """
    for detection_idx in range(len(detections)):
        x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        center = (int((x1 + x2) / 2), y2)
        width = x2 - x1
        cv2.ellipse(
            scene,
            center=center,
            axes=(int(width), int(0.35 * width)),
            angle=0.0,
            startAngle=self.start_angle,
            endAngle=self.end_angle,
            color=color.as_bgr(),
            thickness=self.thickness,
            lineType=cv2.LINE_4,
        )
    return scene

HaloAnnotator

Bases: BaseAnnotator

A class for drawing Halos on an image using provided detections.

Warning

This annotator utilizes the sv.Detections.mask.

Source code in supervision/annotators/core.py
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
class HaloAnnotator(BaseAnnotator):
    """
    A class for drawing Halos on an image using provided detections.

    !!! warning

        This annotator utilizes the `sv.Detections.mask`.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        opacity: float = 0.8,
        kernel_size: int = 40,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
            kernel_size (int): The size of the average pooling kernel used for creating
                the halo.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.opacity = opacity
        self.color_lookup: ColorLookup = color_lookup
        self.kernel_size: int = kernel_size

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with halos based on the provided detections.

        Args:
            scene (np.ndarray): The image where masks will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> halo_annotator = sv.HaloAnnotator()
            >>> annotated_frame = halo_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![halo-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/halo-annotator-example-purple.png)
        """
        if detections.mask is None:
            return scene
        colored_mask = np.zeros_like(scene, dtype=np.uint8)
        fmask = np.array([False] * scene.shape[0] * scene.shape[1]).reshape(
            scene.shape[0], scene.shape[1]
        )

        for detection_idx in np.flip(np.argsort(detections.area)):
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            mask = detections.mask[detection_idx]
            fmask = np.logical_or(fmask, mask)
            color_bgr = color.as_bgr()
            colored_mask[mask] = color_bgr

        colored_mask = cv2.blur(colored_mask, (self.kernel_size, self.kernel_size))
        colored_mask[fmask] = [0, 0, 0]
        gray = cv2.cvtColor(colored_mask, cv2.COLOR_BGR2GRAY)
        alpha = self.opacity * gray / gray.max()
        alpha_mask = alpha[:, :, np.newaxis]
        scene = np.uint8(scene * (1 - alpha_mask) + colored_mask * self.opacity)
        return scene

__init__(color=ColorPalette.default(), opacity=0.8, kernel_size=40, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
opacity float

Opacity of the overlay mask. Must be between 0 and 1.

0.8
kernel_size int

The size of the average pooling kernel used for creating the halo.

40
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    opacity: float = 0.8,
    kernel_size: int = 40,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
        kernel_size (int): The size of the average pooling kernel used for creating
            the halo.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.opacity = opacity
    self.color_lookup: ColorLookup = color_lookup
    self.kernel_size: int = kernel_size

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with halos based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where masks will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> halo_annotator = sv.HaloAnnotator()
>>> annotated_frame = halo_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

halo-annotator-example

Source code in supervision/annotators/core.py
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with halos based on the provided detections.

    Args:
        scene (np.ndarray): The image where masks will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> halo_annotator = sv.HaloAnnotator()
        >>> annotated_frame = halo_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![halo-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/halo-annotator-example-purple.png)
    """
    if detections.mask is None:
        return scene
    colored_mask = np.zeros_like(scene, dtype=np.uint8)
    fmask = np.array([False] * scene.shape[0] * scene.shape[1]).reshape(
        scene.shape[0], scene.shape[1]
    )

    for detection_idx in np.flip(np.argsort(detections.area)):
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        mask = detections.mask[detection_idx]
        fmask = np.logical_or(fmask, mask)
        color_bgr = color.as_bgr()
        colored_mask[mask] = color_bgr

    colored_mask = cv2.blur(colored_mask, (self.kernel_size, self.kernel_size))
    colored_mask[fmask] = [0, 0, 0]
    gray = cv2.cvtColor(colored_mask, cv2.COLOR_BGR2GRAY)
    alpha = self.opacity * gray / gray.max()
    alpha_mask = alpha[:, :, np.newaxis]
    scene = np.uint8(scene * (1 - alpha_mask) + colored_mask * self.opacity)
    return scene

HeatMapAnnotator

A class for drawing heatmaps on an image based on provided detections. Heat accumulates over time and is drawn as a semi-transparent overlay of blurred circles.

Source code in supervision/annotators/core.py
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
class HeatMapAnnotator:
    """
    A class for drawing heatmaps on an image based on provided detections.
    Heat accumulates over time and is drawn as a semi-transparent overlay
    of blurred circles.
    """

    def __init__(
        self,
        position: Position = Position.BOTTOM_CENTER,
        opacity: float = 0.2,
        radius: int = 40,
        kernel_size: int = 25,
        top_hue: int = 0,
        low_hue: int = 125,
    ):
        """
        Args:
            position (Position): The position of the heatmap. Defaults to
                `BOTTOM_CENTER`.
            opacity (float): Opacity of the overlay mask, between 0 and 1.
            radius (int): Radius of the heat circle.
            kernel_size (int): Kernel size for blurring the heatmap.
            top_hue (int): Hue at the top of the heatmap. Defaults to 0 (red).
            low_hue (int): Hue at the bottom of the heatmap. Defaults to 125 (blue).
        """
        self.position = position
        self.opacity = opacity
        self.radius = radius
        self.kernel_size = kernel_size
        self.heat_mask = None
        self.top_hue = top_hue
        self.low_hue = low_hue

    def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
        """
        Annotates the scene with a heatmap based on the provided detections.

        Args:
            scene (np.ndarray): The image where the heatmap will be drawn.
            detections (Detections): Object detections to annotate.

        Returns:
            Annotated image.

        Example:
            ```python
            >>> import supervision as sv
            >>> from ultralytics import YOLO

            >>> model = YOLO('yolov8x.pt')

            >>> heat_map_annotator = sv.HeatMapAnnotator()

            >>> video_info = sv.VideoInfo.from_video_path(video_path='...')
            >>> frames_generator = get_video_frames_generator(source_path='...')

            >>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
            ...    for frame in frames_generator:
            ...        result = model(frame)[0]
            ...        detections = sv.Detections.from_ultralytics(result)
            ...        annotated_frame = heat_map_annotator.annotate(
            ...            scene=frame.copy(),
            ...            detections=detections)
            ...        sink.write_frame(frame=annotated_frame)
            ```

        ![heatmap-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/heat-map-annotator-example-purple.png)
        """

        if self.heat_mask is None:
            self.heat_mask = np.zeros(scene.shape[:2])
        mask = np.zeros(scene.shape[:2])
        for xy in detections.get_anchors_coordinates(self.position):
            cv2.circle(mask, (int(xy[0]), int(xy[1])), self.radius, 1, -1)
        self.heat_mask = mask + self.heat_mask
        temp = self.heat_mask.copy()
        temp = self.low_hue - temp / temp.max() * (self.low_hue - self.top_hue)
        temp = temp.astype(np.uint8)
        if self.kernel_size is not None:
            temp = cv2.blur(temp, (self.kernel_size, self.kernel_size))
        hsv = np.zeros(scene.shape)
        hsv[..., 0] = temp
        hsv[..., 1] = 255
        hsv[..., 2] = 255
        temp = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
        mask = cv2.cvtColor(self.heat_mask.astype(np.uint8), cv2.COLOR_GRAY2BGR) > 0
        scene[mask] = cv2.addWeighted(temp, self.opacity, scene, 1 - self.opacity, 0)[
            mask
        ]
        return scene

__init__(position=Position.BOTTOM_CENTER, opacity=0.2, radius=40, kernel_size=25, top_hue=0, low_hue=125)

Parameters:

Name Type Description Default
position Position

The position of the heatmap. Defaults to BOTTOM_CENTER.

BOTTOM_CENTER
opacity float

Opacity of the overlay mask, between 0 and 1.

0.2
radius int

Radius of the heat circle.

40
kernel_size int

Kernel size for blurring the heatmap.

25
top_hue int

Hue at the top of the heatmap. Defaults to 0 (red).

0
low_hue int

Hue at the bottom of the heatmap. Defaults to 125 (blue).

125
Source code in supervision/annotators/core.py
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
def __init__(
    self,
    position: Position = Position.BOTTOM_CENTER,
    opacity: float = 0.2,
    radius: int = 40,
    kernel_size: int = 25,
    top_hue: int = 0,
    low_hue: int = 125,
):
    """
    Args:
        position (Position): The position of the heatmap. Defaults to
            `BOTTOM_CENTER`.
        opacity (float): Opacity of the overlay mask, between 0 and 1.
        radius (int): Radius of the heat circle.
        kernel_size (int): Kernel size for blurring the heatmap.
        top_hue (int): Hue at the top of the heatmap. Defaults to 0 (red).
        low_hue (int): Hue at the bottom of the heatmap. Defaults to 125 (blue).
    """
    self.position = position
    self.opacity = opacity
    self.radius = radius
    self.kernel_size = kernel_size
    self.heat_mask = None
    self.top_hue = top_hue
    self.low_hue = low_hue

annotate(scene, detections)

Annotates the scene with a heatmap based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where the heatmap will be drawn.

required
detections Detections

Object detections to annotate.

required

Returns:

Type Description
ndarray

Annotated image.

Example
>>> import supervision as sv
>>> from ultralytics import YOLO

>>> model = YOLO('yolov8x.pt')

>>> heat_map_annotator = sv.HeatMapAnnotator()

>>> video_info = sv.VideoInfo.from_video_path(video_path='...')
>>> frames_generator = get_video_frames_generator(source_path='...')

>>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
...    for frame in frames_generator:
...        result = model(frame)[0]
...        detections = sv.Detections.from_ultralytics(result)
...        annotated_frame = heat_map_annotator.annotate(
...            scene=frame.copy(),
...            detections=detections)
...        sink.write_frame(frame=annotated_frame)

heatmap-annotator-example

Source code in supervision/annotators/core.py
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
    """
    Annotates the scene with a heatmap based on the provided detections.

    Args:
        scene (np.ndarray): The image where the heatmap will be drawn.
        detections (Detections): Object detections to annotate.

    Returns:
        Annotated image.

    Example:
        ```python
        >>> import supervision as sv
        >>> from ultralytics import YOLO

        >>> model = YOLO('yolov8x.pt')

        >>> heat_map_annotator = sv.HeatMapAnnotator()

        >>> video_info = sv.VideoInfo.from_video_path(video_path='...')
        >>> frames_generator = get_video_frames_generator(source_path='...')

        >>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
        ...    for frame in frames_generator:
        ...        result = model(frame)[0]
        ...        detections = sv.Detections.from_ultralytics(result)
        ...        annotated_frame = heat_map_annotator.annotate(
        ...            scene=frame.copy(),
        ...            detections=detections)
        ...        sink.write_frame(frame=annotated_frame)
        ```

    ![heatmap-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/heat-map-annotator-example-purple.png)
    """

    if self.heat_mask is None:
        self.heat_mask = np.zeros(scene.shape[:2])
    mask = np.zeros(scene.shape[:2])
    for xy in detections.get_anchors_coordinates(self.position):
        cv2.circle(mask, (int(xy[0]), int(xy[1])), self.radius, 1, -1)
    self.heat_mask = mask + self.heat_mask
    temp = self.heat_mask.copy()
    temp = self.low_hue - temp / temp.max() * (self.low_hue - self.top_hue)
    temp = temp.astype(np.uint8)
    if self.kernel_size is not None:
        temp = cv2.blur(temp, (self.kernel_size, self.kernel_size))
    hsv = np.zeros(scene.shape)
    hsv[..., 0] = temp
    hsv[..., 1] = 255
    hsv[..., 2] = 255
    temp = cv2.cvtColor(hsv.astype(np.uint8), cv2.COLOR_HSV2BGR)
    mask = cv2.cvtColor(self.heat_mask.astype(np.uint8), cv2.COLOR_GRAY2BGR) > 0
    scene[mask] = cv2.addWeighted(temp, self.opacity, scene, 1 - self.opacity, 0)[
        mask
    ]
    return scene

MaskAnnotator

Bases: BaseAnnotator

A class for drawing masks on an image using provided detections.

Warning

This annotator utilizes the sv.Detections.mask.

Source code in supervision/annotators/core.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class MaskAnnotator(BaseAnnotator):
    """
    A class for drawing masks on an image using provided detections.

    !!! warning

        This annotator utilizes the `sv.Detections.mask`.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        opacity: float = 0.5,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.opacity = opacity
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with masks based on the provided detections.

        Args:
            scene (np.ndarray): The image where masks will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> mask_annotator = sv.MaskAnnotator()
            >>> annotated_frame = mask_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![mask-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/mask-annotator-example-purple.png)
        """
        if detections.mask is None:
            return scene

        colored_mask = np.array(scene, copy=True, dtype=np.uint8)

        for detection_idx in np.flip(np.argsort(detections.area)):
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            mask = detections.mask[detection_idx]
            colored_mask[mask] = color.as_bgr()

        scene = cv2.addWeighted(colored_mask, self.opacity, scene, 1 - self.opacity, 0)
        return scene.astype(np.uint8)

__init__(color=ColorPalette.default(), opacity=0.5, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
opacity float

Opacity of the overlay mask. Must be between 0 and 1.

0.5
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    opacity: float = 0.5,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.opacity = opacity
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with masks based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where masks will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> mask_annotator = sv.MaskAnnotator()
>>> annotated_frame = mask_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

mask-annotator-example

Source code in supervision/annotators/core.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with masks based on the provided detections.

    Args:
        scene (np.ndarray): The image where masks will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> mask_annotator = sv.MaskAnnotator()
        >>> annotated_frame = mask_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![mask-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/mask-annotator-example-purple.png)
    """
    if detections.mask is None:
        return scene

    colored_mask = np.array(scene, copy=True, dtype=np.uint8)

    for detection_idx in np.flip(np.argsort(detections.area)):
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        mask = detections.mask[detection_idx]
        colored_mask[mask] = color.as_bgr()

    scene = cv2.addWeighted(colored_mask, self.opacity, scene, 1 - self.opacity, 0)
    return scene.astype(np.uint8)

PolygonAnnotator

Bases: BaseAnnotator

A class for drawing polygons on an image using provided detections.

Warning

This annotator utilizes the sv.Detections.mask.

Source code in supervision/annotators/core.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
class PolygonAnnotator(BaseAnnotator):
    """
    A class for drawing polygons on an image using provided detections.

    !!! warning

        This annotator utilizes the `sv.Detections.mask`.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        thickness: int = 2,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating detections.
            thickness (int): Thickness of the polygon lines.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.thickness: int = thickness
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with polygons based on the provided detections.

        Args:
            scene (np.ndarray): The image where polygons will be drawn.
            detections (Detections): Object detections to annotate.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> polygon_annotator = sv.PolygonAnnotator()
            >>> annotated_frame = polygon_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![polygon-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/polygon-annotator-example-purple.png)
        """
        if detections.mask is None:
            return scene

        for detection_idx in range(len(detections)):
            mask = detections.mask[detection_idx]
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            for polygon in mask_to_polygons(mask=mask):
                scene = draw_polygon(
                    scene=scene,
                    polygon=polygon,
                    color=color,
                    thickness=self.thickness,
                )

        return scene

__init__(color=ColorPalette.default(), thickness=2, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating detections.

default()
thickness int

Thickness of the polygon lines.

2
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    thickness: int = 2,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating detections.
        thickness (int): Thickness of the polygon lines.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.thickness: int = thickness
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Annotates the given scene with polygons based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where polygons will be drawn.

required
detections Detections

Object detections to annotate.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> polygon_annotator = sv.PolygonAnnotator()
>>> annotated_frame = polygon_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

polygon-annotator-example

Source code in supervision/annotators/core.py
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with polygons based on the provided detections.

    Args:
        scene (np.ndarray): The image where polygons will be drawn.
        detections (Detections): Object detections to annotate.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> polygon_annotator = sv.PolygonAnnotator()
        >>> annotated_frame = polygon_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![polygon-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/polygon-annotator-example-purple.png)
    """
    if detections.mask is None:
        return scene

    for detection_idx in range(len(detections)):
        mask = detections.mask[detection_idx]
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        for polygon in mask_to_polygons(mask=mask):
            scene = draw_polygon(
                scene=scene,
                polygon=polygon,
                color=color,
                thickness=self.thickness,
            )

    return scene

LabelAnnotator

A class for annotating labels on an image using provided detections.

Source code in supervision/annotators/core.py
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
class LabelAnnotator:
    """
    A class for annotating labels on an image using provided detections.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        text_color: Color = Color.black(),
        text_scale: float = 0.5,
        text_thickness: int = 1,
        text_padding: int = 10,
        text_position: Position = Position.TOP_LEFT,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color or color palette to use for
                annotating the text background.
            text_color (Color): The color to use for the text.
            text_scale (float): Font scale for the text.
            text_thickness (int): Thickness of the text characters.
            text_padding (int): Padding around the text within its background box.
            text_position (Position): Position of the text relative to the detection.
                Possible values are defined in the `Position` enum.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.text_color: Color = text_color
        self.text_scale: float = text_scale
        self.text_thickness: int = text_thickness
        self.text_padding: int = text_padding
        self.text_anchor: Position = text_position
        self.color_lookup: ColorLookup = color_lookup

    @staticmethod
    def resolve_text_background_xyxy(
        center_coordinates: Tuple[int, int],
        text_wh: Tuple[int, int],
        position: Position,
    ) -> Tuple[int, int, int, int]:
        center_x, center_y = center_coordinates
        text_w, text_h = text_wh

        if position == Position.TOP_LEFT:
            return center_x, center_y - text_h, center_x + text_w, center_y
        elif position == Position.TOP_RIGHT:
            return center_x - text_w, center_y - text_h, center_x, center_y
        elif position == Position.TOP_CENTER:
            return (
                center_x - text_w // 2,
                center_y - text_h,
                center_x + text_w // 2,
                center_y,
            )
        elif position == Position.CENTER or position == Position.CENTER_OF_MASS:
            return (
                center_x - text_w // 2,
                center_y - text_h // 2,
                center_x + text_w // 2,
                center_y + text_h // 2,
            )
        elif position == Position.BOTTOM_LEFT:
            return center_x, center_y, center_x + text_w, center_y + text_h
        elif position == Position.BOTTOM_RIGHT:
            return center_x - text_w, center_y, center_x, center_y + text_h
        elif position == Position.BOTTOM_CENTER:
            return (
                center_x - text_w // 2,
                center_y,
                center_x + text_w // 2,
                center_y + text_h,
            )

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        labels: List[str] = None,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Annotates the given scene with labels based on the provided detections.

        Args:
            scene (np.ndarray): The image where labels will be drawn.
            detections (Detections): Object detections to annotate.
            labels (List[str]): Optional. Custom labels for each detection.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
            >>> annotated_frame = label_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![label-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/label-annotator-example-purple.png)
        """
        font = cv2.FONT_HERSHEY_SIMPLEX
        anchors_coordinates = detections.get_anchors_coordinates(
            anchor=self.text_anchor
        ).astype(int)
        for detection_idx, center_coordinates in enumerate(anchors_coordinates):
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            text = (
                f"{detections.class_id[detection_idx]}"
                if (labels is None or len(detections) != len(labels))
                else labels[detection_idx]
            )
            text_w, text_h = cv2.getTextSize(
                text=text,
                fontFace=font,
                fontScale=self.text_scale,
                thickness=self.text_thickness,
            )[0]
            text_w_padded = text_w + 2 * self.text_padding
            text_h_padded = text_h + 2 * self.text_padding
            text_background_xyxy = self.resolve_text_background_xyxy(
                center_coordinates=tuple(center_coordinates),
                text_wh=(text_w_padded, text_h_padded),
                position=self.text_anchor,
            )

            text_x = text_background_xyxy[0] + self.text_padding
            text_y = text_background_xyxy[1] + self.text_padding + text_h

            cv2.rectangle(
                img=scene,
                pt1=(text_background_xyxy[0], text_background_xyxy[1]),
                pt2=(text_background_xyxy[2], text_background_xyxy[3]),
                color=color.as_bgr(),
                thickness=cv2.FILLED,
            )
            cv2.putText(
                img=scene,
                text=text,
                org=(text_x, text_y),
                fontFace=font,
                fontScale=self.text_scale,
                color=self.text_color.as_rgb(),
                thickness=self.text_thickness,
                lineType=cv2.LINE_AA,
            )
        return scene

__init__(color=ColorPalette.default(), text_color=Color.black(), text_scale=0.5, text_thickness=1, text_padding=10, text_position=Position.TOP_LEFT, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color or color palette to use for annotating the text background.

default()
text_color Color

The color to use for the text.

black()
text_scale float

Font scale for the text.

0.5
text_thickness int

Thickness of the text characters.

1
text_padding int

Padding around the text within its background box.

10
text_position Position

Position of the text relative to the detection. Possible values are defined in the Position enum.

TOP_LEFT
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    text_color: Color = Color.black(),
    text_scale: float = 0.5,
    text_thickness: int = 1,
    text_padding: int = 10,
    text_position: Position = Position.TOP_LEFT,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color or color palette to use for
            annotating the text background.
        text_color (Color): The color to use for the text.
        text_scale (float): Font scale for the text.
        text_thickness (int): Thickness of the text characters.
        text_padding (int): Padding around the text within its background box.
        text_position (Position): Position of the text relative to the detection.
            Possible values are defined in the `Position` enum.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.text_color: Color = text_color
    self.text_scale: float = text_scale
    self.text_thickness: int = text_thickness
    self.text_padding: int = text_padding
    self.text_anchor: Position = text_position
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, labels=None, custom_color_lookup=None)

Annotates the given scene with labels based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where labels will be drawn.

required
detections Detections

Object detections to annotate.

required
labels List[str]

Optional. Custom labels for each detection.

None
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
>>> annotated_frame = label_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

label-annotator-example

Source code in supervision/annotators/core.py
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    labels: List[str] = None,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Annotates the given scene with labels based on the provided detections.

    Args:
        scene (np.ndarray): The image where labels will be drawn.
        detections (Detections): Object detections to annotate.
        labels (List[str]): Optional. Custom labels for each detection.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
        >>> annotated_frame = label_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![label-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/label-annotator-example-purple.png)
    """
    font = cv2.FONT_HERSHEY_SIMPLEX
    anchors_coordinates = detections.get_anchors_coordinates(
        anchor=self.text_anchor
    ).astype(int)
    for detection_idx, center_coordinates in enumerate(anchors_coordinates):
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        text = (
            f"{detections.class_id[detection_idx]}"
            if (labels is None or len(detections) != len(labels))
            else labels[detection_idx]
        )
        text_w, text_h = cv2.getTextSize(
            text=text,
            fontFace=font,
            fontScale=self.text_scale,
            thickness=self.text_thickness,
        )[0]
        text_w_padded = text_w + 2 * self.text_padding
        text_h_padded = text_h + 2 * self.text_padding
        text_background_xyxy = self.resolve_text_background_xyxy(
            center_coordinates=tuple(center_coordinates),
            text_wh=(text_w_padded, text_h_padded),
            position=self.text_anchor,
        )

        text_x = text_background_xyxy[0] + self.text_padding
        text_y = text_background_xyxy[1] + self.text_padding + text_h

        cv2.rectangle(
            img=scene,
            pt1=(text_background_xyxy[0], text_background_xyxy[1]),
            pt2=(text_background_xyxy[2], text_background_xyxy[3]),
            color=color.as_bgr(),
            thickness=cv2.FILLED,
        )
        cv2.putText(
            img=scene,
            text=text,
            org=(text_x, text_y),
            fontFace=font,
            fontScale=self.text_scale,
            color=self.text_color.as_rgb(),
            thickness=self.text_thickness,
            lineType=cv2.LINE_AA,
        )
    return scene

BlurAnnotator

Bases: BaseAnnotator

A class for blurring regions in an image using provided detections.

Source code in supervision/annotators/core.py
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
class BlurAnnotator(BaseAnnotator):
    """
    A class for blurring regions in an image using provided detections.
    """

    def __init__(self, kernel_size: int = 15):
        """
        Args:
            kernel_size (int): The size of the average pooling kernel used for blurring.
        """
        self.kernel_size: int = kernel_size

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
    ) -> np.ndarray:
        """
        Annotates the given scene by blurring regions based on the provided detections.

        Args:
            scene (np.ndarray): The image where blurring will be applied.
            detections (Detections): Object detections to annotate.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> blur_annotator = sv.BlurAnnotator()
            >>> annotated_frame = circle_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![blur-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/blur-annotator-example-purple.png)
        """
        image_height, image_width = scene.shape[:2]
        clipped_xyxy = clip_boxes(
            xyxy=detections.xyxy, resolution_wh=(image_width, image_height)
        ).astype(int)

        for x1, y1, x2, y2 in clipped_xyxy:
            roi = scene[y1:y2, x1:x2]
            roi = cv2.blur(roi, (self.kernel_size, self.kernel_size))
            scene[y1:y2, x1:x2] = roi

        return scene

__init__(kernel_size=15)

Parameters:

Name Type Description Default
kernel_size int

The size of the average pooling kernel used for blurring.

15
Source code in supervision/annotators/core.py
949
950
951
952
953
954
def __init__(self, kernel_size: int = 15):
    """
    Args:
        kernel_size (int): The size of the average pooling kernel used for blurring.
    """
    self.kernel_size: int = kernel_size

annotate(scene, detections)

Annotates the given scene by blurring regions based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where blurring will be applied.

required
detections Detections

Object detections to annotate.

required

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> blur_annotator = sv.BlurAnnotator()
>>> annotated_frame = circle_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

blur-annotator-example

Source code in supervision/annotators/core.py
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
) -> np.ndarray:
    """
    Annotates the given scene by blurring regions based on the provided detections.

    Args:
        scene (np.ndarray): The image where blurring will be applied.
        detections (Detections): Object detections to annotate.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> blur_annotator = sv.BlurAnnotator()
        >>> annotated_frame = circle_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![blur-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/blur-annotator-example-purple.png)
    """
    image_height, image_width = scene.shape[:2]
    clipped_xyxy = clip_boxes(
        xyxy=detections.xyxy, resolution_wh=(image_width, image_height)
    ).astype(int)

    for x1, y1, x2, y2 in clipped_xyxy:
        roi = scene[y1:y2, x1:x2]
        roi = cv2.blur(roi, (self.kernel_size, self.kernel_size))
        scene[y1:y2, x1:x2] = roi

    return scene

PixelateAnnotator

Bases: BaseAnnotator

A class for pixelating regions in an image using provided detections.

Source code in supervision/annotators/core.py
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
class PixelateAnnotator(BaseAnnotator):
    """
    A class for pixelating regions in an image using provided detections.
    """

    def __init__(self, pixel_size: int = 20):
        """
        Args:
            pixel_size (int): The size of the pixelation.
        """
        self.pixel_size: int = pixel_size

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
    ) -> np.ndarray:
        """
        Annotates the given scene by pixelating regions based on the provided
            detections.

        Args:
            scene (np.ndarray): The image where pixelating will be applied.
            detections (Detections): Object detections to annotate.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv

            >>> image = ...
            >>> detections = sv.Detections(...)

            >>> pixelate_annotator = sv.PixelateAnnotator()
            >>> annotated_frame = pixelate_annotator.annotate(
            ...     scene=image.copy(),
            ...     detections=detections
            ... )
            ```

        ![pixelate-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/pixelate-annotator-example-10.png)
        """
        image_height, image_width = scene.shape[:2]
        clipped_xyxy = clip_boxes(
            xyxy=detections.xyxy, resolution_wh=(image_width, image_height)
        ).astype(int)

        for x1, y1, x2, y2 in clipped_xyxy:
            roi = scene[y1:y2, x1:x2]
            scaled_up_roi = cv2.resize(
                src=roi, dsize=None, fx=1 / self.pixel_size, fy=1 / self.pixel_size
            )
            scaled_down_roi = cv2.resize(
                src=scaled_up_roi,
                dsize=(roi.shape[1], roi.shape[0]),
                interpolation=cv2.INTER_NEAREST,
            )

            scene[y1:y2, x1:x2] = scaled_down_roi

        return scene

__init__(pixel_size=20)

Parameters:

Name Type Description Default
pixel_size int

The size of the pixelation.

20
Source code in supervision/annotators/core.py
1206
1207
1208
1209
1210
1211
def __init__(self, pixel_size: int = 20):
    """
    Args:
        pixel_size (int): The size of the pixelation.
    """
    self.pixel_size: int = pixel_size

annotate(scene, detections)

Annotates the given scene by pixelating regions based on the provided detections.

Parameters:

Name Type Description Default
scene ndarray

The image where pixelating will be applied.

required
detections Detections

Object detections to annotate.

required

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv

>>> image = ...
>>> detections = sv.Detections(...)

>>> pixelate_annotator = sv.PixelateAnnotator()
>>> annotated_frame = pixelate_annotator.annotate(
...     scene=image.copy(),
...     detections=detections
... )

pixelate-annotator-example

Source code in supervision/annotators/core.py
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
) -> np.ndarray:
    """
    Annotates the given scene by pixelating regions based on the provided
        detections.

    Args:
        scene (np.ndarray): The image where pixelating will be applied.
        detections (Detections): Object detections to annotate.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv

        >>> image = ...
        >>> detections = sv.Detections(...)

        >>> pixelate_annotator = sv.PixelateAnnotator()
        >>> annotated_frame = pixelate_annotator.annotate(
        ...     scene=image.copy(),
        ...     detections=detections
        ... )
        ```

    ![pixelate-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/pixelate-annotator-example-10.png)
    """
    image_height, image_width = scene.shape[:2]
    clipped_xyxy = clip_boxes(
        xyxy=detections.xyxy, resolution_wh=(image_width, image_height)
    ).astype(int)

    for x1, y1, x2, y2 in clipped_xyxy:
        roi = scene[y1:y2, x1:x2]
        scaled_up_roi = cv2.resize(
            src=roi, dsize=None, fx=1 / self.pixel_size, fy=1 / self.pixel_size
        )
        scaled_down_roi = cv2.resize(
            src=scaled_up_roi,
            dsize=(roi.shape[1], roi.shape[0]),
            interpolation=cv2.INTER_NEAREST,
        )

        scene[y1:y2, x1:x2] = scaled_down_roi

    return scene

TraceAnnotator

A class for drawing trace paths on an image based on detection coordinates.

Warning

This annotator utilizes the sv.Detections.tracker_id. Read here to learn how to plug tracking into your inference pipeline.

Source code in supervision/annotators/core.py
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
class TraceAnnotator:
    """
    A class for drawing trace paths on an image based on detection coordinates.

    !!! warning

        This annotator utilizes the `sv.Detections.tracker_id`. Read
        [here](https://supervision.roboflow.com/trackers/) to learn how to plug
        tracking into your inference pipeline.
    """

    def __init__(
        self,
        color: Union[Color, ColorPalette] = ColorPalette.default(),
        position: Position = Position.CENTER,
        trace_length: int = 30,
        thickness: int = 2,
        color_lookup: ColorLookup = ColorLookup.CLASS,
    ):
        """
        Args:
            color (Union[Color, ColorPalette]): The color to draw the trace, can be
                a single color or a color palette.
            position (Position): The position of the trace.
                Defaults to `CENTER`.
            trace_length (int): The maximum length of the trace in terms of historical
                points. Defaults to `30`.
            thickness (int): The thickness of the trace lines. Defaults to `2`.
            color_lookup (str): Strategy for mapping colors to annotations.
                Options are `INDEX`, `CLASS`, `TRACK`.
        """
        self.color: Union[Color, ColorPalette] = color
        self.trace = Trace(max_size=trace_length, anchor=position)
        self.thickness = thickness
        self.color_lookup: ColorLookup = color_lookup

    def annotate(
        self,
        scene: np.ndarray,
        detections: Detections,
        custom_color_lookup: Optional[np.ndarray] = None,
    ) -> np.ndarray:
        """
        Draws trace paths on the frame based on the detection coordinates provided.

        Args:
            scene (np.ndarray): The image on which the traces will be drawn.
            detections (Detections): The detections which include coordinates for
                which the traces will be drawn.
            custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
                Allows to override the default color mapping strategy.

        Returns:
            The annotated image.

        Example:
            ```python
            >>> import supervision as sv
            >>> from ultralytics import YOLO

            >>> model = YOLO('yolov8x.pt')

            >>> trace_annotator = sv.TraceAnnotator()

            >>> video_info = sv.VideoInfo.from_video_path(video_path='...')
            >>> frames_generator = sv.get_video_frames_generator(source_path='...')
            >>> tracker = sv.ByteTrack()

            >>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
            ...    for frame in frames_generator:
            ...        result = model(frame)[0]
            ...        detections = sv.Detections.from_ultralytics(result)
            ...        detections = tracker.update_with_detections(detections)
            ...        annotated_frame = trace_annotator.annotate(
            ...            scene=frame.copy(),
            ...            detections=detections)
            ...        sink.write_frame(frame=annotated_frame)
            ```

        ![trace-annotator-example](https://media.roboflow.com/
        supervision-annotator-examples/trace-annotator-example-purple.png)
        """
        self.trace.put(detections)

        for detection_idx in range(len(detections)):
            tracker_id = int(detections.tracker_id[detection_idx])
            color = resolve_color(
                color=self.color,
                detections=detections,
                detection_idx=detection_idx,
                color_lookup=self.color_lookup
                if custom_color_lookup is None
                else custom_color_lookup,
            )
            xy = self.trace.get(tracker_id=tracker_id)
            if len(xy) > 1:
                scene = cv2.polylines(
                    scene,
                    [xy.astype(np.int32)],
                    False,
                    color=color.as_bgr(),
                    thickness=self.thickness,
                )
        return scene

__init__(color=ColorPalette.default(), position=Position.CENTER, trace_length=30, thickness=2, color_lookup=ColorLookup.CLASS)

Parameters:

Name Type Description Default
color Union[Color, ColorPalette]

The color to draw the trace, can be a single color or a color palette.

default()
position Position

The position of the trace. Defaults to CENTER.

CENTER
trace_length int

The maximum length of the trace in terms of historical points. Defaults to 30.

30
thickness int

The thickness of the trace lines. Defaults to 2.

2
color_lookup str

Strategy for mapping colors to annotations. Options are INDEX, CLASS, TRACK.

CLASS
Source code in supervision/annotators/core.py
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
def __init__(
    self,
    color: Union[Color, ColorPalette] = ColorPalette.default(),
    position: Position = Position.CENTER,
    trace_length: int = 30,
    thickness: int = 2,
    color_lookup: ColorLookup = ColorLookup.CLASS,
):
    """
    Args:
        color (Union[Color, ColorPalette]): The color to draw the trace, can be
            a single color or a color palette.
        position (Position): The position of the trace.
            Defaults to `CENTER`.
        trace_length (int): The maximum length of the trace in terms of historical
            points. Defaults to `30`.
        thickness (int): The thickness of the trace lines. Defaults to `2`.
        color_lookup (str): Strategy for mapping colors to annotations.
            Options are `INDEX`, `CLASS`, `TRACK`.
    """
    self.color: Union[Color, ColorPalette] = color
    self.trace = Trace(max_size=trace_length, anchor=position)
    self.thickness = thickness
    self.color_lookup: ColorLookup = color_lookup

annotate(scene, detections, custom_color_lookup=None)

Draws trace paths on the frame based on the detection coordinates provided.

Parameters:

Name Type Description Default
scene ndarray

The image on which the traces will be drawn.

required
detections Detections

The detections which include coordinates for which the traces will be drawn.

required
custom_color_lookup Optional[ndarray]

Custom color lookup array. Allows to override the default color mapping strategy.

None

Returns:

Type Description
ndarray

The annotated image.

Example
>>> import supervision as sv
>>> from ultralytics import YOLO

>>> model = YOLO('yolov8x.pt')

>>> trace_annotator = sv.TraceAnnotator()

>>> video_info = sv.VideoInfo.from_video_path(video_path='...')
>>> frames_generator = sv.get_video_frames_generator(source_path='...')
>>> tracker = sv.ByteTrack()

>>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
...    for frame in frames_generator:
...        result = model(frame)[0]
...        detections = sv.Detections.from_ultralytics(result)
...        detections = tracker.update_with_detections(detections)
...        annotated_frame = trace_annotator.annotate(
...            scene=frame.copy(),
...            detections=detections)
...        sink.write_frame(frame=annotated_frame)

trace-annotator-example

Source code in supervision/annotators/core.py
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
def annotate(
    self,
    scene: np.ndarray,
    detections: Detections,
    custom_color_lookup: Optional[np.ndarray] = None,
) -> np.ndarray:
    """
    Draws trace paths on the frame based on the detection coordinates provided.

    Args:
        scene (np.ndarray): The image on which the traces will be drawn.
        detections (Detections): The detections which include coordinates for
            which the traces will be drawn.
        custom_color_lookup (Optional[np.ndarray]): Custom color lookup array.
            Allows to override the default color mapping strategy.

    Returns:
        The annotated image.

    Example:
        ```python
        >>> import supervision as sv
        >>> from ultralytics import YOLO

        >>> model = YOLO('yolov8x.pt')

        >>> trace_annotator = sv.TraceAnnotator()

        >>> video_info = sv.VideoInfo.from_video_path(video_path='...')
        >>> frames_generator = sv.get_video_frames_generator(source_path='...')
        >>> tracker = sv.ByteTrack()

        >>> with sv.VideoSink(target_path='...', video_info=video_info) as sink:
        ...    for frame in frames_generator:
        ...        result = model(frame)[0]
        ...        detections = sv.Detections.from_ultralytics(result)
        ...        detections = tracker.update_with_detections(detections)
        ...        annotated_frame = trace_annotator.annotate(
        ...            scene=frame.copy(),
        ...            detections=detections)
        ...        sink.write_frame(frame=annotated_frame)
        ```

    ![trace-annotator-example](https://media.roboflow.com/
    supervision-annotator-examples/trace-annotator-example-purple.png)
    """
    self.trace.put(detections)

    for detection_idx in range(len(detections)):
        tracker_id = int(detections.tracker_id[detection_idx])
        color = resolve_color(
            color=self.color,
            detections=detections,
            detection_idx=detection_idx,
            color_lookup=self.color_lookup
            if custom_color_lookup is None
            else custom_color_lookup,
        )
        xy = self.trace.get(tracker_id=tracker_id)
        if len(xy) > 1:
            scene = cv2.polylines(
                scene,
                [xy.astype(np.int32)],
                False,
                color=color.as_bgr(),
                thickness=self.thickness,
            )
    return scene

ColorLookup

Bases: Enum

Enumeration class to define strategies for mapping colors to annotations.

This enum supports three different lookup strategies
  • INDEX: Colors are determined by the index of the detection within the scene.
  • CLASS: Colors are determined by the class label of the detected object.
  • TRACK: Colors are determined by the tracking identifier of the object.
Source code in supervision/annotators/utils.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ColorLookup(Enum):
    """
    Enumeration class to define strategies for mapping colors to annotations.

    This enum supports three different lookup strategies:
        - `INDEX`: Colors are determined by the index of the detection within the scene.
        - `CLASS`: Colors are determined by the class label of the detected object.
        - `TRACK`: Colors are determined by the tracking identifier of the object.
    """

    INDEX = "index"
    CLASS = "class"
    TRACK = "track"

    @classmethod
    def list(cls):
        return list(map(lambda c: c.value, cls))