Skip to content

Commit 2587fdf

Browse files
committed
☝️ updating MaskAnnotator docs and code to be consistent with BoundingBoxAnnotator
1 parent 181c0b8 commit 2587fdf

2 files changed

Lines changed: 40 additions & 58 deletions

File tree

docs/detection/annotate.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
## BoxAnnotator
1+
## BoundingBoxAnnotator
22

3-
:::supervision.detection.annotate.BoxAnnotator
3+
:::supervision.annotators.core.BoundingBoxAnnotator
44

55
## MaskAnnotator
66

7-
:::supervision.detection.annotate.MaskAnnotator
8-
9-
## TraceAnnotator
10-
11-
:::supervision.detection.annotate.TraceAnnotator
7+
:::supervision.annotators.core.MaskAnnotator

supervision/annotators/core.py

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,36 @@ def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
6565

6666
class BoundingBoxAnnotator(BaseAnnotator):
6767
"""
68-
Basic line bounding box annotator.
68+
A class for drawing bounding boxes on an image using provided detections.
6969
"""
70-
7170
def __init__(
7271
self,
7372
color: Union[Color, ColorPalette] = ColorPalette.default(),
7473
thickness: int = 2,
7574
color_map: str = "class",
7675
):
7776
"""
78-
Parameters:
79-
color (Union[Color, ColorPalette]): The color to use for
77+
Args:
78+
color (Union[Color, ColorPalette]): The color or color palette to use for
8079
annotating detections.
81-
thickness (int): The thickness of the bounding box lines.
82-
color_map (ColorMap): The color mapping to use for annotating detections.
80+
thickness (int): Thickness of the bounding box lines.
81+
color_map (str): Strategy for mapping colors to annotations.
82+
Options are `index`, `class`, or `track`.
8383
"""
8484
self.color: Union[Color, ColorPalette] = color
8585
self.thickness: int = thickness
8686
self.color_map: ColorMap = ColorMap(color_map)
8787

8888
def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
8989
"""
90-
Draws bounding boxes on the frame using the detections provided.
90+
Annotates the given scene with bounding boxes based on the provided detections.
9191
9292
Args:
93-
scene (np.ndarray): The image on which the bounding boxes will be drawn
94-
detections (Detections): The detections for which
95-
the bounding boxes will be drawn
93+
scene (np.ndarray): The image where bounding boxes will be drawn.
94+
detections (Detections): Object detections to annotate.
95+
9696
Returns:
97-
np.ndarray: The image with the bounding boxes drawn on it.
97+
The annotated image.
9898
9999
Example:
100100
```python
@@ -103,8 +103,8 @@ def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
103103
>>> image = ...
104104
>>> detections = sv.Detections(...)
105105
106-
>>> box_line_annotator = sv.BoundingBoxAnnotator()
107-
>>> annotated_frame = box_line_annotator.annotate(
106+
>>> bounding_box_annotator = sv.BoundingBoxAnnotator()
107+
>>> annotated_frame = bounding_box_annotator.annotate(
108108
... scene=image.copy(),
109109
... detections=detections
110110
... )
@@ -130,42 +130,41 @@ def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
130130

131131
class MaskAnnotator(BaseAnnotator):
132132
"""
133-
A class for overlaying masks on an image using detections provided.
134-
135-
Attributes:
136-
color (Union[Color, ColorPalette]): The color to fill the mask,
137-
can be a single color or a color palette
138-
opacity (float): The opacity of the masks, between 0 and 1.
133+
A class for drawing masks on an image using provided detections.
139134
"""
140-
141135
def __init__(
142136
self,
143137
color: Union[Color, ColorPalette] = ColorPalette.default(),
144138
opacity: float = 0.5,
145-
color_by_track: bool = False,
139+
color_map: str = "class",
146140
):
141+
"""
142+
Args:
143+
color (Union[Color, ColorPalette]): The color or color palette to use for
144+
annotating detections.
145+
opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
146+
color_map (str): Strategy for mapping colors to annotations.
147+
Options are `index`, `class`, or `track`.
148+
"""
147149
self.color: Union[Color, ColorPalette] = color
148150
self.opacity = opacity
149-
self.color_by_track = color_by_track
151+
self.color_map: ColorMap = ColorMap(color_map)
150152

151-
def annotate(
152-
self, scene: np.ndarray, detections: Detections, **kwargs
153-
) -> np.ndarray:
153+
def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
154154
"""
155-
Overlays the masks on the given image based on the provided detections,
156-
with a specified opacity.
155+
Annotates the given scene with masks based on the provided detections.
157156
158157
Args:
159-
scene (np.ndarray): The image on which the masks will be overlaid
160-
detections (Detections): The detections for which the masks will be overlaid
158+
scene (np.ndarray): The image where masks will be drawn.
159+
detections (Detections): Object detections to annotate.
161160
162161
Returns:
163-
np.ndarray: The image with the masks overlaid
162+
The annotated image.
163+
164164
Example:
165165
```python
166166
>>> import supervision as sv
167167
168-
>>> classes = ['person', ...]
169168
>>> image = ...
170169
>>> detections = sv.Detections(...)
171170
@@ -179,27 +178,15 @@ def annotate(
179178
if detections.mask is None:
180179
return scene
181180

182-
for i in np.flip(np.argsort(detections.area)):
183-
if self.color_by_track:
184-
tracker_id = (
185-
detections.tracker_id[i]
186-
if detections.tracker_id is not None
187-
else None
188-
)
189-
idx = tracker_id if tracker_id is not None else i
190-
else:
191-
class_id = (
192-
detections.class_id[i] if detections.class_id is not None else None
193-
)
194-
idx = class_id if class_id is not None else i
195-
196-
color = (
197-
self.color.by_idx(idx)
198-
if isinstance(self.color, ColorPalette)
199-
else self.color
181+
for detection_idx in np.flip(np.argsort(detections.area)):
182+
idx = resolve_color_idx(
183+
detections=detections,
184+
detection_idx=detection_idx,
185+
color_map=self.color_map,
200186
)
187+
color = resolve_color(color=self.color, idx=idx)
201188

202-
mask = detections.mask[i]
189+
mask = detections.mask[detection_idx]
203190
colored_mask = np.zeros_like(scene, dtype=np.uint8)
204191
colored_mask[:] = color.as_bgr()
205192

@@ -208,7 +195,6 @@ def annotate(
208195
np.uint8(self.opacity * colored_mask + (1 - self.opacity) * scene),
209196
scene,
210197
)
211-
212198
return scene
213199

214200

0 commit comments

Comments
 (0)