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
5567class _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 ]
0 commit comments