Skip to content

Commit d30d807

Browse files
authored
Merge pull request roboflow#422 from hardikdava/box_mask_annotator
Added box mask annotators
2 parents 4733ebe + 669c065 commit d30d807

3 files changed

Lines changed: 127 additions & 28 deletions

File tree

docs/annotators.md

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,87 +19,108 @@
1919

2020
</div>
2121

22-
=== "Mask"
22+
=== "BoxCorner"
2323

2424
```python
2525
>>> import supervision as sv
2626

2727
>>> image = ...
2828
>>> detections = sv.Detections(...)
2929

30-
>>> mask_annotator = sv.MaskAnnotator()
31-
>>> annotated_frame = mask_annotator.annotate(
30+
>>> corner_annotator = sv.BoxCornerAnnotator()
31+
>>> annotated_frame = corner_annotator.annotate(
3232
... scene=image.copy(),
3333
... detections=detections
3434
... )
3535
```
3636

3737
<div class="result" markdown>
3838

39-
![mask-annotator-example](https://media.roboflow.com/supervision-annotator-examples/mask-annotator-example.png){ align=center width="800" }
39+
![box-corner-annotator-example](https://media.roboflow.com/supervision-annotator-examples/box-corner-annotator-example.png){ align=center width="800" }
4040

4141
</div>
4242

43-
=== "Ellipse"
43+
=== "BoxMaskAnnotator"
4444

4545
```python
4646
>>> import supervision as sv
4747

4848
>>> image = ...
4949
>>> detections = sv.Detections(...)
5050

51-
>>> ellipse_annotator = sv.EllipseAnnotator()
52-
>>> annotated_frame = ellipse_annotator.annotate(
51+
>>> box_mask_annotator = sv.BoxMaskAnnotator()
52+
>>> annotated_frame = box_mask_annotator.annotate(
5353
... scene=image.copy(),
5454
... detections=detections
5555
... )
5656
```
5757

5858
<div class="result" markdown>
5959

60-
![ellipse-annotator-example](https://media.roboflow.com/supervision-annotator-examples/ellipse-annotator-example.png){ align=center width="800" }
60+
![box-mask-annotator-example](https://media.roboflow.com/supervision-annotator-examples/box-mask-annotator-example.png){ align=center width="800" }
6161

6262
</div>
6363

64-
=== "BoxCorner"
64+
=== "Circle"
6565

6666
```python
6767
>>> import supervision as sv
6868

6969
>>> image = ...
7070
>>> detections = sv.Detections(...)
7171

72-
>>> corner_annotator = sv.BoxCornerAnnotator()
73-
>>> annotated_frame = corner_annotator.annotate(
72+
>>> circle_annotator = sv.CircleAnnotator()
73+
>>> annotated_frame = circle_annotator.annotate(
7474
... scene=image.copy(),
7575
... detections=detections
7676
... )
7777
```
7878

7979
<div class="result" markdown>
8080

81-
![box-corner-annotator-example](https://media.roboflow.com/supervision-annotator-examples/box-corner-annotator-example.png){ align=center width="800" }
81+
![circle-annotator-example](https://media.roboflow.com/supervision-annotator-examples/circle-annotator-example.png){ align=center width="800" }
8282

8383
</div>
8484

85-
=== "Circle"
85+
=== "Ellipse"
8686

8787
```python
8888
>>> import supervision as sv
8989

9090
>>> image = ...
9191
>>> detections = sv.Detections(...)
9292

93-
>>> circle_annotator = sv.CircleAnnotator()
94-
>>> annotated_frame = circle_annotator.annotate(
93+
>>> ellipse_annotator = sv.EllipseAnnotator()
94+
>>> annotated_frame = ellipse_annotator.annotate(
9595
... scene=image.copy(),
9696
... detections=detections
9797
... )
9898
```
9999

100100
<div class="result" markdown>
101101

102-
![circle-annotator-example](https://media.roboflow.com/supervision-annotator-examples/circle-annotator-example.png){ align=center width="800" }
102+
![ellipse-annotator-example](https://media.roboflow.com/supervision-annotator-examples/ellipse-annotator-example.png){ align=center width="800" }
103+
104+
</div>
105+
106+
=== "Mask"
107+
108+
```python
109+
>>> import supervision as sv
110+
111+
>>> image = ...
112+
>>> detections = sv.Detections(...)
113+
114+
>>> mask_annotator = sv.MaskAnnotator()
115+
>>> annotated_frame = mask_annotator.annotate(
116+
... scene=image.copy(),
117+
... detections=detections
118+
... )
119+
```
120+
121+
<div class="result" markdown>
122+
123+
![mask-annotator-example](https://media.roboflow.com/supervision-annotator-examples/mask-annotator-example.png){ align=center width="800" }
103124

104125
</div>
105126

@@ -170,30 +191,34 @@
170191

171192
:::supervision.annotators.core.BoundingBoxAnnotator
172193

173-
## MaskAnnotator
174-
175-
:::supervision.annotators.core.MaskAnnotator
176-
177-
## EllipseAnnotator
178-
179-
:::supervision.annotators.core.EllipseAnnotator
180-
181194
## BoxCornerAnnotator
182195

183196
:::supervision.annotators.core.BoxCornerAnnotator
184197

198+
## BoxMaskAnnotator
199+
200+
:::supervision.annotators.core.BoxMaskAnnotator
201+
185202
## CircleAnnotator
186203

187204
:::supervision.annotators.core.CircleAnnotator
188205

189-
## LabelAnnotator
206+
## EllipseAnnotator
190207

191-
:::supervision.annotators.core.LabelAnnotator
208+
:::supervision.annotators.core.EllipseAnnotator
192209

193-
## TraceAnnotator
210+
## MaskAnnotator
194211

195-
:::supervision.annotators.core.TraceAnnotator
212+
:::supervision.annotators.core.MaskAnnotator
213+
214+
## LabelAnnotator
215+
216+
:::supervision.annotators.core.LabelAnnotator
196217

197218
## BlurAnnotator
198219

199220
:::supervision.annotators.core.BlurAnnotator
221+
222+
## TraceAnnotator
223+
224+
:::supervision.annotators.core.TraceAnnotator

supervision/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
BlurAnnotator,
1111
BoundingBoxAnnotator,
1212
BoxCornerAnnotator,
13+
BoxMaskAnnotator,
1314
CircleAnnotator,
1415
EllipseAnnotator,
1516
LabelAnnotator,

supervision/annotators/core.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,79 @@ def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
156156
return scene
157157

158158

159+
class BoxMaskAnnotator(BaseAnnotator):
160+
"""
161+
A class for drawing box masks on an image using provided detections.
162+
"""
163+
164+
def __init__(
165+
self,
166+
color: Union[Color, ColorPalette] = ColorPalette.default(),
167+
opacity: float = 0.5,
168+
color_map: str = "class",
169+
):
170+
"""
171+
Args:
172+
color (Union[Color, ColorPalette]): The color or color palette to use for
173+
annotating detections.
174+
opacity (float): Opacity of the overlay mask. Must be between `0` and `1`.
175+
color_map (str): Strategy for mapping colors to annotations.
176+
Options are `index`, `class`, or `track`.
177+
"""
178+
self.color: Union[Color, ColorPalette] = color
179+
self.color_map: ColorMap = ColorMap(color_map)
180+
self.opacity = opacity
181+
182+
def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray:
183+
"""
184+
Annotates the given scene with box masks based on the provided detections.
185+
186+
Args:
187+
scene (np.ndarray): The image where bounding boxes will be drawn.
188+
detections (Detections): Object detections to annotate.
189+
190+
Returns:
191+
np.ndarray: The annotated image.
192+
193+
Example:
194+
```python
195+
>>> import supervision as sv
196+
197+
>>> image = ...
198+
>>> detections = sv.Detections(...)
199+
200+
>>> box_mask_annotator = sv.BoxMaskAnnotator()
201+
>>> annotated_frame = box_mask_annotator.annotate(
202+
... scene=image.copy(),
203+
... detections=detections
204+
... )
205+
```
206+
207+
![box-mask-annotator-example](https://media.roboflow.com/
208+
supervision-annotator-examples/box-mask-annotator-example.png)
209+
"""
210+
mask_image = scene.copy()
211+
for detection_idx in range(len(detections)):
212+
x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int)
213+
idx = resolve_color_idx(
214+
detections=detections,
215+
detection_idx=detection_idx,
216+
color_map=self.color_map,
217+
)
218+
color = resolve_color(color=self.color, idx=idx)
219+
cv2.rectangle(
220+
img=scene,
221+
pt1=(x1, y1),
222+
pt2=(x2, y2),
223+
color=color.as_bgr(),
224+
thickness=-1,
225+
)
226+
scene = cv2.addWeighted(
227+
scene, self.opacity, mask_image, 1 - self.opacity, gamma=0
228+
)
229+
return scene
230+
231+
159232
class EllipseAnnotator(BaseAnnotator):
160233
"""
161234
A class for drawing ellipses on an image using provided detections.

0 commit comments

Comments
 (0)