Skip to content

Commit b7aba15

Browse files
CopilotBorda
andcommitted
Fix cv2 constant usage at module level and update CI workflows
Co-authored-by: Borda <[email protected]>
1 parent b4b3422 commit b7aba15

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/supervision/annotators/core.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,19 @@
4949
scale_image,
5050
)
5151

52-
CV2_FONT = cv2.FONT_HERSHEY_SIMPLEX
52+
# Lazy initialization for cv2 constants to avoid import errors
53+
CV2_FONT = None
54+
55+
56+
def _get_cv2_font() -> int:
57+
"""Get cv2.FONT_HERSHEY_SIMPLEX constant, ensuring cv2 is installed."""
58+
global CV2_FONT
59+
if CV2_FONT is None:
60+
from supervision.utils.internal import ensure_cv2_installed
61+
62+
ensure_cv2_installed()
63+
CV2_FONT = cv2.FONT_HERSHEY_SIMPLEX
64+
return CV2_FONT
5365

5466

5567
class _BaseLabelAnnotator(BaseAnnotator):
@@ -1277,7 +1289,7 @@ def _get_label_properties(
12771289
for line in wrapped_lines:
12781290
(text_w, text_h) = cv2.getTextSize(
12791291
text=line,
1280-
fontFace=CV2_FONT,
1292+
fontFace=_get_cv2_font(),
12811293
fontScale=self.text_scale,
12821294
thickness=self.text_thickness,
12831295
)[0]
@@ -1360,7 +1372,7 @@ def _draw_labels(
13601372
# Use a character with ascenders and descenders as height reference
13611373
(_, text_h) = cv2.getTextSize(
13621374
text="Tg",
1363-
fontFace=CV2_FONT,
1375+
fontFace=_get_cv2_font(),
13641376
fontScale=self.text_scale,
13651377
thickness=self.text_thickness,
13661378
)[0]
@@ -1369,7 +1381,7 @@ def _draw_labels(
13691381

13701382
(_, text_h) = cv2.getTextSize(
13711383
text=line,
1372-
fontFace=CV2_FONT,
1384+
fontFace=_get_cv2_font(),
13731385
fontScale=self.text_scale,
13741386
thickness=self.text_thickness,
13751387
)[0]
@@ -1381,7 +1393,7 @@ def _draw_labels(
13811393
img=scene,
13821394
text=line,
13831395
org=(text_x, text_y),
1384-
fontFace=CV2_FONT,
1396+
fontFace=_get_cv2_font(),
13851397
fontScale=self.text_scale,
13861398
color=text_color.as_bgr(),
13871399
thickness=self.text_thickness,
@@ -3106,7 +3118,7 @@ def _draw_labels(self, scene: npt.NDArray[np.uint8]) -> None:
31063118

31073119
(text_w, _) = cv2.getTextSize(
31083120
text=text,
3109-
fontFace=CV2_FONT,
3121+
fontFace=_get_cv2_font(),
31103122
fontScale=self.label_scale,
31113123
thickness=self.text_thickness,
31123124
)[0]

src/supervision/draw/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def draw_text(
234234
text_scale: float = 0.5,
235235
text_thickness: int = 1,
236236
text_padding: int = 10,
237-
text_font: int = cv2.FONT_HERSHEY_SIMPLEX,
237+
text_font: int | None = None,
238238
background_color: Color | None = None,
239239
) -> npt.NDArray[np.uint8]:
240240
"""
@@ -274,6 +274,8 @@ def draw_text(
274274
```
275275
"""
276276
ensure_cv2_installed()
277+
if text_font is None:
278+
text_font = cv2.FONT_HERSHEY_SIMPLEX
277279
text_width, text_height = cv2.getTextSize(
278280
text=text,
279281
fontFace=text_font,

0 commit comments

Comments
 (0)