Skip to content

Commit 476a31f

Browse files
fix(pre_commit): 🎨 auto format pre-commit hooks
1 parent e1e455f commit 476a31f

File tree

3 files changed

+86
-82
lines changed

3 files changed

+86
-82
lines changed

‎supervision/annotators/core.py‎

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
import numpy as np
66

77
from supervision.annotators.base import BaseAnnotator
8-
from supervision.annotators.utils import (
9-
ColorLookup,
10-
Trace,
11-
resolve_color,
12-
)
8+
from supervision.annotators.utils import ColorLookup, Trace, resolve_color
139
from supervision.detection.core import Detections
1410
from supervision.draw.color import Color, ColorPalette
1511
from supervision.geometry.core import Position
@@ -42,7 +38,7 @@ def annotate(
4238
self,
4339
scene: np.ndarray,
4440
detections: Detections,
45-
custom_color_lookup: Optional[np.ndarray] = None
41+
custom_color_lookup: Optional[np.ndarray] = None,
4642
) -> np.ndarray:
4743
"""
4844
Annotates the given scene with bounding boxes based on the provided detections.
@@ -79,8 +75,9 @@ def annotate(
7975
color=self.color,
8076
detections=detections,
8177
detection_idx=detection_idx,
82-
color_lookup=self.color_lookup if
83-
custom_color_lookup is None else custom_color_lookup
78+
color_lookup=self.color_lookup
79+
if custom_color_lookup is None
80+
else custom_color_lookup,
8481
)
8582
cv2.rectangle(
8683
img=scene,
@@ -119,7 +116,7 @@ def annotate(
119116
self,
120117
scene: np.ndarray,
121118
detections: Detections,
122-
custom_color_lookup: Optional[np.ndarray] = None
119+
custom_color_lookup: Optional[np.ndarray] = None,
123120
) -> np.ndarray:
124121
"""
125122
Annotates the given scene with masks based on the provided detections.
@@ -158,8 +155,9 @@ def annotate(
158155
color=self.color,
159156
detections=detections,
160157
detection_idx=detection_idx,
161-
color_lookup=self.color_lookup if
162-
custom_color_lookup is None else custom_color_lookup
158+
color_lookup=self.color_lookup
159+
if custom_color_lookup is None
160+
else custom_color_lookup,
163161
)
164162
mask = detections.mask[detection_idx]
165163
colored_mask = np.zeros_like(scene, dtype=np.uint8)
@@ -198,7 +196,7 @@ def annotate(
198196
self,
199197
scene: np.ndarray,
200198
detections: Detections,
201-
custom_color_lookup: Optional[np.ndarray] = None
199+
custom_color_lookup: Optional[np.ndarray] = None,
202200
) -> np.ndarray:
203201
"""
204202
Annotates the given scene with box masks based on the provided detections.
@@ -236,8 +234,9 @@ def annotate(
236234
color=self.color,
237235
detections=detections,
238236
detection_idx=detection_idx,
239-
color_lookup=self.color_lookup if
240-
custom_color_lookup is None else custom_color_lookup
237+
color_lookup=self.color_lookup
238+
if custom_color_lookup is None
239+
else custom_color_lookup,
241240
)
242241
cv2.rectangle(
243242
img=scene,
@@ -283,7 +282,7 @@ def annotate(
283282
self,
284283
scene: np.ndarray,
285284
detections: Detections,
286-
custom_color_lookup: Optional[np.ndarray] = None
285+
custom_color_lookup: Optional[np.ndarray] = None,
287286
) -> np.ndarray:
288287
"""
289288
Annotates the given scene with halos based on the provided detections.
@@ -326,8 +325,9 @@ def annotate(
326325
color=self.color,
327326
detections=detections,
328327
detection_idx=detection_idx,
329-
color_lookup=self.color_lookup if
330-
custom_color_lookup is None else custom_color_lookup
328+
color_lookup=self.color_lookup
329+
if custom_color_lookup is None
330+
else custom_color_lookup,
331331
)
332332
mask = detections.mask[detection_idx]
333333
fmask = np.logical_or(fmask, mask)
@@ -376,7 +376,7 @@ def annotate(
376376
self,
377377
scene: np.ndarray,
378378
detections: Detections,
379-
custom_color_lookup: Optional[np.ndarray] = None
379+
custom_color_lookup: Optional[np.ndarray] = None,
380380
) -> np.ndarray:
381381
"""
382382
Annotates the given scene with ellipses based on the provided detections.
@@ -413,8 +413,9 @@ def annotate(
413413
color=self.color,
414414
detections=detections,
415415
detection_idx=detection_idx,
416-
color_lookup=self.color_lookup if
417-
custom_color_lookup is None else custom_color_lookup
416+
color_lookup=self.color_lookup
417+
if custom_color_lookup is None
418+
else custom_color_lookup,
418419
)
419420
center = (int((x1 + x2) / 2), y2)
420421
width = x2 - x1
@@ -462,7 +463,7 @@ def annotate(
462463
self,
463464
scene: np.ndarray,
464465
detections: Detections,
465-
custom_color_lookup: Optional[np.ndarray] = None
466+
custom_color_lookup: Optional[np.ndarray] = None,
466467
) -> np.ndarray:
467468
"""
468469
Annotates the given scene with box corners based on the provided detections.
@@ -499,8 +500,9 @@ def annotate(
499500
color=self.color,
500501
detections=detections,
501502
detection_idx=detection_idx,
502-
color_lookup=self.color_lookup if
503-
custom_color_lookup is None else custom_color_lookup
503+
color_lookup=self.color_lookup
504+
if custom_color_lookup is None
505+
else custom_color_lookup,
504506
)
505507
corners = [(x1, y1), (x2, y1), (x1, y2), (x2, y2)]
506508

@@ -545,7 +547,7 @@ def annotate(
545547
self,
546548
scene: np.ndarray,
547549
detections: Detections,
548-
custom_color_lookup: Optional[np.ndarray] = None
550+
custom_color_lookup: Optional[np.ndarray] = None,
549551
) -> np.ndarray:
550552
"""
551553
Annotates the given scene with circles based on the provided detections.
@@ -585,8 +587,9 @@ def annotate(
585587
color=self.color,
586588
detections=detections,
587589
detection_idx=detection_idx,
588-
color_lookup=self.color_lookup if
589-
custom_color_lookup is None else custom_color_lookup
590+
color_lookup=self.color_lookup
591+
if custom_color_lookup is None
592+
else custom_color_lookup,
590593
)
591594
cv2.circle(
592595
img=scene,
@@ -682,7 +685,7 @@ def annotate(
682685
scene: np.ndarray,
683686
detections: Detections,
684687
labels: List[str] = None,
685-
custom_color_lookup: Optional[np.ndarray] = None
688+
custom_color_lookup: Optional[np.ndarray] = None,
686689
) -> np.ndarray:
687690
"""
688691
Annotates the given scene with labels based on the provided detections.
@@ -721,8 +724,9 @@ def annotate(
721724
color=self.color,
722725
detections=detections,
723726
detection_idx=detection_idx,
724-
color_lookup=self.color_lookup if
725-
custom_color_lookup is None else custom_color_lookup
727+
color_lookup=self.color_lookup
728+
if custom_color_lookup is None
729+
else custom_color_lookup,
726730
)
727731
text = (
728732
f"{detections.class_id[detection_idx]}"
@@ -861,7 +865,7 @@ def annotate(
861865
self,
862866
scene: np.ndarray,
863867
detections: Detections,
864-
custom_color_lookup: Optional[np.ndarray] = None
868+
custom_color_lookup: Optional[np.ndarray] = None,
865869
) -> np.ndarray:
866870
"""
867871
Draws trace paths on the frame based on the detection coordinates provided.
@@ -901,8 +905,9 @@ def annotate(
901905
color=self.color,
902906
detections=detections,
903907
detection_idx=detection_idx,
904-
color_lookup=self.color_lookup if
905-
custom_color_lookup is None else custom_color_lookup
908+
color_lookup=self.color_lookup
909+
if custom_color_lookup is None
910+
else custom_color_lookup,
906911
)
907912
xy = self.trace.get(tracker_id=tracker_id)
908913
if len(xy) > 1:

‎supervision/annotators/utils.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ColorLookup(Enum):
1212
"""
1313
Enum for annotator color lookup.
1414
"""
15+
1516
INDEX = "index"
1617
CLASS = "class"
1718
TRACK = "track"
@@ -20,7 +21,7 @@ class ColorLookup(Enum):
2021
def resolve_color_idx(
2122
detections: Detections,
2223
detection_idx: int,
23-
color_lookup: Union[ColorLookup, np.ndarray] = ColorLookup.CLASS
24+
color_lookup: Union[ColorLookup, np.ndarray] = ColorLookup.CLASS,
2425
) -> int:
2526
if detection_idx >= len(detections):
2627
raise ValueError(
@@ -63,7 +64,7 @@ def resolve_color(
6364
color: Union[Color, ColorPalette],
6465
detections: Detections,
6566
detection_idx: int,
66-
color_lookup: Union[ColorLookup, np.ndarray] = ColorLookup.CLASS
67+
color_lookup: Union[ColorLookup, np.ndarray] = ColorLookup.CLASS,
6768
) -> Color:
6869
idx = resolve_color_idx(
6970
detections=detections,

‎test/annotators/test_utils.py‎

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from contextlib import ExitStack as DoesNotRaise
2-
3-
import numpy as np
4-
52
from test.utils import mock_detections
63
from typing import Optional
74

5+
import numpy as np
86
import pytest
97

108
from supervision.annotators.utils import ColorLookup, resolve_color_idx
@@ -15,83 +13,83 @@
1513
"detections, detection_idx, color_lookup, expected_result, exception",
1614
[
1715
(
18-
mock_detections(
16+
mock_detections(
1917
xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]],
2018
class_id=[5, 3],
2119
tracker_id=[2, 6],
2220
),
23-
0,
24-
ColorLookup.INDEX,
25-
0,
26-
DoesNotRaise(),
21+
0,
22+
ColorLookup.INDEX,
23+
0,
24+
DoesNotRaise(),
2725
), # multiple detections; index lookup
2826
(
29-
mock_detections(
27+
mock_detections(
3028
xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]],
3129
class_id=[5, 3],
3230
tracker_id=[2, 6],
3331
),
34-
0,
35-
ColorLookup.CLASS,
36-
5,
37-
DoesNotRaise(),
32+
0,
33+
ColorLookup.CLASS,
34+
5,
35+
DoesNotRaise(),
3836
), # multiple detections; class lookup
3937
(
40-
mock_detections(
38+
mock_detections(
4139
xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]],
4240
class_id=[5, 3],
4341
tracker_id=[2, 6],
4442
),
45-
0,
46-
ColorLookup.TRACK,
47-
2,
48-
DoesNotRaise(),
43+
0,
44+
ColorLookup.TRACK,
45+
2,
46+
DoesNotRaise(),
4947
), # multiple detections; track lookup
5048
(
51-
Detections.empty(),
52-
0,
53-
ColorLookup.INDEX,
54-
None,
55-
pytest.raises(ValueError),
49+
Detections.empty(),
50+
0,
51+
ColorLookup.INDEX,
52+
None,
53+
pytest.raises(ValueError),
5654
), # no detections; index lookup; out of bounds
5755
(
58-
mock_detections(
56+
mock_detections(
5957
xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]],
6058
class_id=[5, 3],
6159
tracker_id=[2, 6],
6260
),
63-
2,
64-
ColorLookup.INDEX,
65-
None,
66-
pytest.raises(ValueError),
61+
2,
62+
ColorLookup.INDEX,
63+
None,
64+
pytest.raises(ValueError),
6765
), # multiple detections; index lookup; out of bounds
6866
(
69-
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
70-
0,
71-
ColorLookup.CLASS,
72-
None,
73-
pytest.raises(ValueError),
67+
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
68+
0,
69+
ColorLookup.CLASS,
70+
None,
71+
pytest.raises(ValueError),
7472
), # multiple detections; class lookup; no class_id
7573
(
76-
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
77-
0,
78-
ColorLookup.TRACK,
79-
None,
80-
pytest.raises(ValueError),
74+
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
75+
0,
76+
ColorLookup.TRACK,
77+
None,
78+
pytest.raises(ValueError),
8179
), # multiple detections; class lookup; no track_id
8280
(
83-
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
84-
0,
85-
np.array([1, 0]),
86-
1,
87-
DoesNotRaise(),
81+
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
82+
0,
83+
np.array([1, 0]),
84+
1,
85+
DoesNotRaise(),
8886
), # multiple detections; custom lookup; correct length
8987
(
90-
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
91-
0,
92-
np.array([1]),
93-
None,
94-
pytest.raises(ValueError),
88+
mock_detections(xyxy=[[10, 10, 20, 20], [20, 20, 30, 30]]),
89+
0,
90+
np.array([1]),
91+
None,
92+
pytest.raises(ValueError),
9593
), # multiple detections; custom lookup; wrong length
9694
],
9795
)

0 commit comments

Comments
 (0)