From fbcc21107920002661d906959ffca60e6defa33c Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Thu, 11 Jan 2024 00:32:01 +0200 Subject: [PATCH] added boundingboxannotator class to re-write class --- supervision/annotators/core.py | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 9ae81c95..c7f0210f 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1496,3 +1496,81 @@ class RoundBoxAnnotator(BaseAnnotator): ) return scene + + +class BoundingBoxAnnotator(BaseAnnotator): + """ + A class for drawing bounding boxes on an image using provided detections. + """ + + def __init__( + self, + color: Union[Color, ColorPalette] = ColorPalette.default(), + thickness: int = 2, + color_lookup: ColorLookup = ColorLookup.CLASS, + ): + """ + Args: + color (Union[Color, ColorPalette]): The color or color palette to use for + annotating detections. + thickness (int): Thickness of the bounding box lines. + color_lookup (str): Strategy for mapping colors to annotations. + Options are `INDEX`, `CLASS`, `TRACK`. + """ + self.color: Union[Color, ColorPalette] = color + self.thickness: int = thickness + self.color_lookup: ColorLookup = color_lookup + + def annotate( + self, + scene: np.ndarray, + detections: Detections, + custom_color_lookup: Optional[np.ndarray] = None, + ) -> np.ndarray: + """ + Annotates the given scene with bounding boxes based on the provided detections. + + Args: + scene (np.ndarray): The image where bounding boxes will be drawn. + detections (Detections): Object detections to annotate. + custom_color_lookup (Optional[np.ndarray]): Custom color lookup array. + Allows to override the default color mapping strategy. + + Returns: + The annotated image. + + Example: + ```python + >>> import supervision as sv + + >>> image = ... + >>> detections = sv.Detections(...) + + >>> bounding_box_annotator = sv.BoundingBoxAnnotator() + >>> annotated_frame = bounding_box_annotator.annotate( + ... scene=image.copy(), + ... detections=detections + ... ) + ``` + + ![bounding-box-annotator-example](https://media.roboflow.com/ + supervision-annotator-examples/bounding-box-annotator-example-purple.png) + """ + for detection_idx in range(len(detections)): + x1, y1, x2, y2 = detections.xyxy[detection_idx].astype(int) + 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, + ) + cv2.rectangle( + img=scene, + pt1=(x1, y1), + pt2=(x2, y2), + color=color.as_bgr(), + thickness=self.thickness, + ) + return scene \ No newline at end of file