From 21968e53c6009861b516e1e856cd2b166f7e0e1f Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Sun, 2 Jul 2023 23:25:43 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8Finitial=20PR=20for=20redesign?= =?UTF-8?q?ed=20annotators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/annotators/__init__.py | 0 supervision/annotators/composable.py | 18 ++++++ supervision/annotators/core.py | 97 ++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 supervision/annotators/__init__.py create mode 100644 supervision/annotators/composable.py create mode 100644 supervision/annotators/core.py diff --git a/supervision/annotators/__init__.py b/supervision/annotators/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/supervision/annotators/composable.py b/supervision/annotators/composable.py new file mode 100644 index 00000000..4c0eaeb9 --- /dev/null +++ b/supervision/annotators/composable.py @@ -0,0 +1,18 @@ +from abc import ABC +from typing import List + +import numpy as np + +from supervision.annotators.core import BaseAnnotator +from supervision.detection.core import Detections + + +class ComposableAnnotator(ABC): + def __init__(self, annotators: List[BaseAnnotator]): + self.annotators = annotators + + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + annotated_image = scene + for annotator in self.annotators: + annotated_image = annotator.annotate(scene=scene, detections=detections) + return annotated_image diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py new file mode 100644 index 00000000..137f60b2 --- /dev/null +++ b/supervision/annotators/core.py @@ -0,0 +1,97 @@ +from abc import ABC, abstractmethod +from typing import Union + +import numpy as np + +from supervision.draw.color import Color, ColorPalette +from supervision.detection.core import Detections + + +class BaseAnnotator(ABC): + @abstractmethod + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + pass + + +class BoxAnnotator(BaseAnnotator): + + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + pass + + +class MaskAnnotator(BaseAnnotator): + """ + A class for overlaying masks on an image using detections provided. + + Attributes: + color (Union[Color, ColorPalette]): The color to fill the mask, can be a single color or a color palette + opacity (float): The opacity of the masks, between 0 and 1. + """ + def __init__( + self, + color: Union[Color, ColorPalette] = ColorPalette.default(), + opacity: float = 0.5 + ): + self.color: Union[Color, ColorPalette] = color + self.opacity = opacity + + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + """ + Overlays the masks on the given image based on the provided detections, with a specified opacity. + + Args: + scene (np.ndarray): The image on which the masks will be overlaid + detections (Detections): The detections for which the masks will be overlaid + + Returns: + np.ndarray: The image with the masks overlaid + """ + if detections.mask is None: + return scene + + for i in np.flip(np.argsort(detections.area)): + class_id = ( + detections.class_id[i] if detections.class_id is not None else None + ) + idx = class_id if class_id is not None else i + color = ( + self.color.by_idx(idx) + if isinstance(self.color, ColorPalette) + else self.color + ) + + mask = detections.mask[i] + colored_mask = np.zeros_like(scene, dtype=np.uint8) + colored_mask[:] = color.as_bgr() + + scene = np.where( + np.expand_dims(mask, axis=-1), + np.uint8(self.opacity * colored_mask + (1 - self.opacity) * scene), + scene, + ) + + return scene + + +class LabelAnnotator(BaseAnnotator): + + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + pass + + +class PillowLabelAnnotator(BaseAnnotator): + + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + pass + + +class TrackAnnotator(BaseAnnotator): + + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + pass + + +class BoxMaskAnnotator(BaseAnnotator): + + def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: + pass \ No newline at end of file