From 1fbdec6046113493d5cd44083881b30a77ae4a9d Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Sun, 24 Mar 2024 11:35:18 +0200 Subject: [PATCH 1/6] added `corner_radius` attribute to `LabelAnnotator` --- supervision/annotators/core.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 2be2562ee..7dc8ab4fc 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -907,6 +907,7 @@ class LabelAnnotator: def __init__( self, + corner_radius: int = 15, color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, text_color: Color = Color.WHITE, text_scale: float = 0.5, @@ -917,6 +918,8 @@ def __init__( ): """ Args: + corner_radius (int): The radius to apply round edges. If the selected + value is higher than the lower dimension, width or height, is clipped. color (Union[Color, ColorPalette]): The color or color palette to use for annotating the text background. text_color (Color): The color to use for the text. @@ -928,6 +931,7 @@ def __init__( color_lookup (str): Strategy for mapping colors to annotations. Options are `INDEX`, `CLASS`, `TRACK`. """ + self.corner_radius: int = corner_radius self.color: Union[Color, ColorPalette] = color self.text_color: Color = text_color self.text_scale: float = text_scale From 3e4ddd5bd9e2f37efee41a570858941bd2f1dd0c Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Sun, 24 Mar 2024 11:36:10 +0200 Subject: [PATCH 2/6] added new custom method that draws rounded rectangles --- supervision/annotators/core.py | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 7dc8ab4fc..c137a8d4b 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1103,6 +1103,49 @@ def annotate( ) return scene + @staticmethod + def draw_rounded_rectangle( + img: ImageType, pt1: Tuple[int, int], pt2: Tuple[int, int], + color: Tuple[int, int, int], thickness: int, corner_radius: int + ) -> ImageType: + x1, y1 = pt1 + x2, y2 = pt2 + + width = x2 - x1 + height = y2 - y1 + + max_corner_radius = min(width, height) // 2 + + corner_radius = min(corner_radius, max_corner_radius) + + rectangle_coords = [ + ((x1 + corner_radius, y1), (x2 - corner_radius, y2)), + ((x1, y1 + corner_radius),(x2, y2 - corner_radius)) + ] + circle_centers = [ + (x1 + corner_radius, y1 + corner_radius), + (x2 - corner_radius, y1 + corner_radius), + (x1 + corner_radius, y2 - corner_radius), + (x2 - corner_radius, y2 - corner_radius), + ] + + for coords in rectangle_coords: + cv2.rectangle( + img=img, + pt1=coords[0], + pt2=coords[1], + color=color, + thickness=thickness + ) + for center in circle_centers: + cv2.circle( + img=img, + center=center, + radius=corner_radius, + color=color, + thickness=thickness + ) + class BlurAnnotator(BaseAnnotator): """ From ed3c73eb02afc6d2685b87d83b4ab8b73eff9dc8 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Sun, 24 Mar 2024 11:36:51 +0200 Subject: [PATCH 3/6] modified to use new custom method for rectangle drawing --- supervision/annotators/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index c137a8d4b..22506d2b9 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1084,12 +1084,13 @@ def annotate( text_x = text_background_xyxy[0] + self.text_padding text_y = text_background_xyxy[1] + self.text_padding + text_h - cv2.rectangle( + self.draw_rounded_rectangle( img=scene, pt1=(text_background_xyxy[0], text_background_xyxy[1]), pt2=(text_background_xyxy[2], text_background_xyxy[3]), color=color.as_bgr(), thickness=cv2.FILLED, + corner_radius=self.corner_radius ) cv2.putText( img=scene, From 957ced06147bcc8c44a314bd8a2926352188dd55 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Sun, 24 Mar 2024 11:42:19 +0200 Subject: [PATCH 4/6] (pre-commit): align spacing --- supervision/annotators/core.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 22506d2b9..15da147d5 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1090,7 +1090,7 @@ def annotate( pt2=(text_background_xyxy[2], text_background_xyxy[3]), color=color.as_bgr(), thickness=cv2.FILLED, - corner_radius=self.corner_radius + corner_radius=self.corner_radius, ) cv2.putText( img=scene, @@ -1106,22 +1106,26 @@ def annotate( @staticmethod def draw_rounded_rectangle( - img: ImageType, pt1: Tuple[int, int], pt2: Tuple[int, int], - color: Tuple[int, int, int], thickness: int, corner_radius: int + img: ImageType, + pt1: Tuple[int, int], + pt2: Tuple[int, int], + color: Tuple[int, int, int], + thickness: int, + corner_radius: int, ) -> ImageType: x1, y1 = pt1 x2, y2 = pt2 - + width = x2 - x1 height = y2 - y1 - + max_corner_radius = min(width, height) // 2 - + corner_radius = min(corner_radius, max_corner_radius) - + rectangle_coords = [ ((x1 + corner_radius, y1), (x2 - corner_radius, y2)), - ((x1, y1 + corner_radius),(x2, y2 - corner_radius)) + ((x1, y1 + corner_radius), (x2, y2 - corner_radius)), ] circle_centers = [ (x1 + corner_radius, y1 + corner_radius), @@ -1132,11 +1136,7 @@ def draw_rounded_rectangle( for coords in rectangle_coords: cv2.rectangle( - img=img, - pt1=coords[0], - pt2=coords[1], - color=color, - thickness=thickness + img=img, pt1=coords[0], pt2=coords[1], color=color, thickness=thickness ) for center in circle_centers: cv2.circle( @@ -1144,7 +1144,7 @@ def draw_rounded_rectangle( center=center, radius=corner_radius, color=color, - thickness=thickness + thickness=thickness, ) From b585bb0724f8392856a281ab9e9a2788b77c2c13 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Mon, 25 Mar 2024 17:28:59 +0100 Subject: [PATCH 5/6] test slightly simplified API --- supervision/annotators/core.py | 63 ++++++++++++++++------------------ 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 15da147d5..2e28d658e 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -907,7 +907,6 @@ class LabelAnnotator: def __init__( self, - corner_radius: int = 15, color: Union[Color, ColorPalette] = ColorPalette.DEFAULT, text_color: Color = Color.WHITE, text_scale: float = 0.5, @@ -915,11 +914,10 @@ def __init__( text_padding: int = 10, text_position: Position = Position.TOP_LEFT, color_lookup: ColorLookup = ColorLookup.CLASS, + border_radius: int = 0, ): """ Args: - corner_radius (int): The radius to apply round edges. If the selected - value is higher than the lower dimension, width or height, is clipped. color (Union[Color, ColorPalette]): The color or color palette to use for annotating the text background. text_color (Color): The color to use for the text. @@ -930,8 +928,10 @@ def __init__( Possible values are defined in the `Position` enum. color_lookup (str): Strategy for mapping colors to annotations. Options are `INDEX`, `CLASS`, `TRACK`. + border_radius (int): The radius to apply round edges. If the selected + value is higher than the lower dimension, width or height, is clipped. """ - self.corner_radius: int = corner_radius + self.border_radius: int = border_radius self.color: Union[Color, ColorPalette] = color self.text_color: Color = text_color self.text_scale: float = text_scale @@ -1085,12 +1085,10 @@ def annotate( text_y = text_background_xyxy[1] + self.text_padding + text_h self.draw_rounded_rectangle( - img=scene, - pt1=(text_background_xyxy[0], text_background_xyxy[1]), - pt2=(text_background_xyxy[2], text_background_xyxy[3]), + scene=scene, + xyxy=text_background_xyxy, color=color.as_bgr(), - thickness=cv2.FILLED, - corner_radius=self.corner_radius, + border_radius=self.border_radius, ) cv2.putText( img=scene, @@ -1106,46 +1104,45 @@ def annotate( @staticmethod def draw_rounded_rectangle( - img: ImageType, - pt1: Tuple[int, int], - pt2: Tuple[int, int], + scene: np.ndarray, + xyxy: Tuple[int, int, int, int], color: Tuple[int, int, int], - thickness: int, - corner_radius: int, - ) -> ImageType: - x1, y1 = pt1 - x2, y2 = pt2 - + border_radius: int, + ) -> np.ndarray: + x1, y1, x2, y2 = xyxy width = x2 - x1 height = y2 - y1 - max_corner_radius = min(width, height) // 2 - - corner_radius = min(corner_radius, max_corner_radius) + border_radius = min(border_radius, min(width, height) // 2) - rectangle_coords = [ - ((x1 + corner_radius, y1), (x2 - corner_radius, y2)), - ((x1, y1 + corner_radius), (x2, y2 - corner_radius)), + rectangle_coordinates = [ + ((x1 + border_radius, y1), (x2 - border_radius, y2)), + ((x1, y1 + border_radius), (x2, y2 - border_radius)), ] circle_centers = [ - (x1 + corner_radius, y1 + corner_radius), - (x2 - corner_radius, y1 + corner_radius), - (x1 + corner_radius, y2 - corner_radius), - (x2 - corner_radius, y2 - corner_radius), + (x1 + border_radius, y1 + border_radius), + (x2 - border_radius, y1 + border_radius), + (x1 + border_radius, y2 - border_radius), + (x2 - border_radius, y2 - border_radius), ] - for coords in rectangle_coords: + for coordinates in rectangle_coordinates: cv2.rectangle( - img=img, pt1=coords[0], pt2=coords[1], color=color, thickness=thickness + img=scene, + pt1=coordinates[0], + pt2=coordinates[1], + color=color, + thickness=-1 ) for center in circle_centers: cv2.circle( - img=img, + img=scene, center=center, - radius=corner_radius, + radius=border_radius, color=color, - thickness=thickness, + thickness=-1, ) + return scene class BlurAnnotator(BaseAnnotator): From 90f6893d3de1d54ee04367e20dcd8bae065a9d99 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:29:14 +0000 Subject: [PATCH 6/6] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/annotators/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 2e28d658e..6ae5a3eaa 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1132,7 +1132,7 @@ def draw_rounded_rectangle( pt1=coordinates[0], pt2=coordinates[1], color=color, - thickness=-1 + thickness=-1, ) for center in circle_centers: cv2.circle(