From b2e9a668b2c9ae74b0e62926b4948f16506fd3c8 Mon Sep 17 00:00:00 2001 From: PankajKrana Date: Tue, 17 Oct 2023 01:48:54 +0530 Subject: [PATCH 1/4] Create sv.DotMarkerAnnotator #396 --- .../annotators/dot_marker_annotator.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 supervision/annotators/dot_marker_annotator.py diff --git a/supervision/annotators/dot_marker_annotator.py b/supervision/annotators/dot_marker_annotator.py new file mode 100644 index 00000000..4f931b5f --- /dev/null +++ b/supervision/annotators/dot_marker_annotator.py @@ -0,0 +1,46 @@ +from typing import Union +import numpy as np +import cv2 +from supervision import Color, ColorPalette, Position, Detections +from supervision.annotators.base import BaseAnnotator + + + +class DotMarkerAnnotator(BaseAnnotator): + def __init__( + self, + color: Union[Color, ColorPalette] = ColorPalette.default(), + radius: int = 4, + position: Position = Position.CENTER, + color_map: str = "class", + ): + self.color = color + self.radius = radius + self.position = position + self.color_map = color_map + + def annotate( + self, + scene: np.ndarray, + detections: Detections, + ) -> np.ndarray: + annotated_scene = scene.copy() + + for detection in detections: + dot_color = self._get_color(detection) + box_position = detection.bounding_box.get_position(self.position) + dot_position = (int(box_position[0]), int(box_position[1])) + cv2.circle(annotated_scene, dot_position, self.radius, dot_color, -1) + + return annotated_scene + + def _get_color(self, detection): + if self.color_map == "class": + return self.color[detection.class_id] + elif self.color_map == "index": + return self.color[detection.index % len(self.color)] + elif self.color_map == "track": + return self.color[detection.track_id % len(self.color)] + else: + raise ValueError("Invalid color mapping strategy") + From 61809f9329c40a4d2941c532f397cb6317d0ee2b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 13:53:25 +0000 Subject: [PATCH 2/4] =?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/dot_marker_annotator.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/supervision/annotators/dot_marker_annotator.py b/supervision/annotators/dot_marker_annotator.py index 4f931b5f..ecdb9af0 100644 --- a/supervision/annotators/dot_marker_annotator.py +++ b/supervision/annotators/dot_marker_annotator.py @@ -1,9 +1,10 @@ from typing import Union -import numpy as np -import cv2 -from supervision import Color, ColorPalette, Position, Detections -from supervision.annotators.base import BaseAnnotator +import cv2 +import numpy as np + +from supervision import Color, ColorPalette, Detections, Position +from supervision.annotators.base import BaseAnnotator class DotMarkerAnnotator(BaseAnnotator): @@ -43,4 +44,3 @@ class DotMarkerAnnotator(BaseAnnotator): return self.color[detection.track_id % len(self.color)] else: raise ValueError("Invalid color mapping strategy") - From fabfe0783aad29a6fa6475016075ebe05772a354 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 18 Oct 2023 18:09:13 +0200 Subject: [PATCH 3/4] Refactor DotMarkerAnnotator and merge into core annotators The DotMarkerAnnotator class has been refactored and named DotAnnotator for simplicity. It's now part of the core annotators to conform with the project structure. The color lookup on the DotAnnotator now uses the 'color_lookup' parameter, allowing for more flexibility and customization in the colors used for the annotations. All the changes aim to standardize the way different annotators are organized and make color customization easier. --- supervision/__init__.py | 1 + supervision/annotators/core.py | 33 +++++++++++++ .../annotators/dot_marker_annotator.py | 46 ------------------- 3 files changed, 34 insertions(+), 46 deletions(-) delete mode 100644 supervision/annotators/dot_marker_annotator.py diff --git a/supervision/__init__.py b/supervision/__init__.py index 31d01314..b1d25337 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -12,6 +12,7 @@ from supervision.annotators.core import ( BoxCornerAnnotator, BoxMaskAnnotator, CircleAnnotator, + DotAnnotator, EllipseAnnotator, HaloAnnotator, LabelAnnotator, diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 8005f49b..3bde610a 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -602,6 +602,39 @@ class CircleAnnotator(BaseAnnotator): return scene +class DotAnnotator(BaseAnnotator): + def __init__( + self, + color: Union[Color, ColorPalette] = ColorPalette.default(), + radius: int = 4, + position: Position = Position.CENTER, + color_lookup: ColorLookup = ColorLookup.CLASS, + ): + self.color: Union[Color, ColorPalette] = color + self.radius: int = radius + self.position: Position = position + self.color_lookup: ColorLookup = color_lookup + + def annotate( + self, + scene: np.ndarray, + detections: Detections, + custom_color_lookup: Optional[np.ndarray] = None, + ) -> np.ndarray: + xy = detections.get_anchor_coordinates(anchor=self.position) + for detection_idx in range(len(detections)): + color = resolve_color( + color=self.color, + detections=detections, + detection_idx=detection_idx, + color_lookup=self.color_lookup + if custom_color_lookup is None + else custom_color_lookup, + ) + center = (int(xy[detection_idx, 0]), int(xy[detection_idx, 1])) + cv2.circle(scene, center, self.radius, color.as_bgr(), -1) + return scene + class LabelAnnotator: """ A class for annotating labels on an image using provided detections. diff --git a/supervision/annotators/dot_marker_annotator.py b/supervision/annotators/dot_marker_annotator.py deleted file mode 100644 index ecdb9af0..00000000 --- a/supervision/annotators/dot_marker_annotator.py +++ /dev/null @@ -1,46 +0,0 @@ -from typing import Union - -import cv2 -import numpy as np - -from supervision import Color, ColorPalette, Detections, Position -from supervision.annotators.base import BaseAnnotator - - -class DotMarkerAnnotator(BaseAnnotator): - def __init__( - self, - color: Union[Color, ColorPalette] = ColorPalette.default(), - radius: int = 4, - position: Position = Position.CENTER, - color_map: str = "class", - ): - self.color = color - self.radius = radius - self.position = position - self.color_map = color_map - - def annotate( - self, - scene: np.ndarray, - detections: Detections, - ) -> np.ndarray: - annotated_scene = scene.copy() - - for detection in detections: - dot_color = self._get_color(detection) - box_position = detection.bounding_box.get_position(self.position) - dot_position = (int(box_position[0]), int(box_position[1])) - cv2.circle(annotated_scene, dot_position, self.radius, dot_color, -1) - - return annotated_scene - - def _get_color(self, detection): - if self.color_map == "class": - return self.color[detection.class_id] - elif self.color_map == "index": - return self.color[detection.index % len(self.color)] - elif self.color_map == "track": - return self.color[detection.track_id % len(self.color)] - else: - raise ValueError("Invalid color mapping strategy") From 1b6613d4a3b3f66adf8cfef82e3679c4baeacdbb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 16:09:40 +0000 Subject: [PATCH 4/4] =?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 | 1 + 1 file changed, 1 insertion(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 3bde610a..4abb4f91 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -635,6 +635,7 @@ class DotAnnotator(BaseAnnotator): cv2.circle(scene, center, self.radius, color.as_bgr(), -1) return scene + class LabelAnnotator: """ A class for annotating labels on an image using provided detections.