From a63087096846433c6dd37140528ba027b2ef2e37 Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Sun, 1 Oct 2023 20:51:46 +0300 Subject: [PATCH 1/2] =?UTF-8?q?feat(annotator):=20=F0=9F=9A=80=20introduce?= =?UTF-8?q?=20circle=20annotator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- docs/detection/annotate.md | 4 ++ supervision/__init__.py | 1 + supervision/annotators/core.py | 81 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/docs/detection/annotate.md b/docs/detection/annotate.md index cc0be9fc1..c548b794e 100644 --- a/docs/detection/annotate.md +++ b/docs/detection/annotate.md @@ -14,6 +14,10 @@ :::supervision.annotators.core.BoxCornerAnnotator +## CircleAnnotator + +:::supervision.annotators.core.CircleAnnotator + ## LabelAnnotator :::supervision.annotators.core.LabelAnnotator diff --git a/supervision/__init__.py b/supervision/__init__.py index 517602e00..7ad768475 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -9,6 +9,7 @@ from supervision.annotators.core import ( BoundingBoxAnnotator, BoxCornerAnnotator, + CircleAnnotator, EllipseAnnotator, LabelAnnotator, MaskAnnotator, diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 481a17b73..12da39378 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1,3 +1,4 @@ +from math import sqrt from typing import List, Tuple, Union import cv2 @@ -315,6 +316,86 @@ def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: return scene +class CircleAnnotator(BaseAnnotator): + """ + A class for drawing circle on an image using provided detections. + """ + + def __init__( + self, + color: Union[Color, ColorPalette] = ColorPalette.default(), + thickness: int = 4, + color_map: str = "class", + ): + """ + Args: + color (Union[Color, ColorPalette]): The color or color palette to use for + annotating detections. + thickness (int): Thickness of the circle line. + color_map (str): Strategy for mapping colors to annotations. + Options are `index`, `class`, or `track`. + """ + + self.color: Union[Color, ColorPalette] = color + self.thickness: int = thickness + self.color_map: ColorMap = ColorMap(color_map) + + def annotate( + self, + scene: np.ndarray, + detections: Detections, + ) -> np.ndarray: + """ + Draws circle on the frame using the detections provided. + Args: + scene (np.ndarray): The image on which the circle will be drawn + detections (Detections): The detections for which the + circle will be drawn + Returns: + np.ndarray: The image with the circle drawn on it + Example: + ```python + >>> import supervision as sv + + >>> image = ... + >>> detections = sv.Detections(...) + + >>> circle_annotator = sv.CircleAnnotator() + >>> annotated_frame = circle_annotator.annotate( + ... scene=image.copy(), + ... detections=detections + ... ) + ``` + """ + + for detection_idx in range(len(detections)): + x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int) + center = ((x1 + x2) // 2, (y1 + y2) // 2) + distance = sqrt((x1 - center[0]) ** 2 + (y1 - center[1]) ** 2) + + idx = resolve_color_idx( + detections=detections, + detection_idx=detection_idx, + color_map=self.color_map, + ) + + color = ( + self.color.by_idx(idx) + if isinstance(self.color, ColorPalette) + else self.color + ) + + cv2.circle( + img=scene, + center=center, + radius=int(distance), + color=color.as_bgr(), + thickness=self.thickness, + ) + + return scene + + class LabelAnnotator: """ A class for annotating labels on an image using provided detections. From 9fbd0ddf6269fa5f2fafe303dcfe3410b61f8d0a Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Tue, 3 Oct 2023 00:03:20 +0300 Subject: [PATCH 2/2] =?UTF-8?q?docs(annotator):=20=F0=9F=93=9D=20circle=20?= =?UTF-8?q?annotator=20example=20picture=20added?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- supervision/annotators/core.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 12da39378..e4a4b9d22 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -366,8 +366,11 @@ def annotate( ... detections=detections ... ) ``` - """ + + ![circle-annotator-example](https://media.roboflow.com/ + supervision-annotator-examples/circle-annotator-example.png) + """ for detection_idx in range(len(detections)): x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int) center = ((x1 + x2) // 2, (y1 + y2) // 2)