From ccafa3339d88bb278689a2390a79ab3e2e4386e6 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Mon, 6 Feb 2023 23:05:17 +0100 Subject: [PATCH] - PolygonZone and PolygonZoneAnnotator added --- supervision/detection/core.py | 8 ++- supervision/detection/polygon_zone.py | 80 +++++++++++++++++++++++++++ supervision/draw/utils.py | 10 ++-- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 2fef419b..7e93001b 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -150,9 +150,13 @@ class Detections: xyxy=self.xyxy[index], confidence=self.confidence[index], class_id=self.class_id[index], - tracker_id=self.tracker_id[index] if self.tracker_id is not None else None, + tracker_id=self.tracker_id[index] + if self.tracker_id is not None + else None, ) - raise TypeError(f"Detections.__getitem__ not supported for index of type {type(index)}.") + raise TypeError( + f"Detections.__getitem__ not supported for index of type {type(index)}." + ) class BoxAnnotator: diff --git a/supervision/detection/polygon_zone.py b/supervision/detection/polygon_zone.py index e69de29b..bc30a6b6 100644 --- a/supervision/detection/polygon_zone.py +++ b/supervision/detection/polygon_zone.py @@ -0,0 +1,80 @@ +from typing import Tuple + +import cv2 +import numpy as np + +from supervision import Detections +from supervision.detection.utils import generate_2d_mask +from supervision.draw.color import Color +from supervision.draw.utils import draw_polygon, draw_text +from supervision.geometry.core import Position +from supervision.geometry.utils import get_polygon_center + + +class PolygonZone: + def __init__( + self, + polygon: np.ndarray, + frame_resolution_wh: Tuple[int, int], + triggering_position: Position, + ): + self.polygon = polygon + self.frame_resolution_wh = frame_resolution_wh + self.triggering_position = triggering_position + self.mask = generate_2d_mask(polygon=polygon, resolution_wh=frame_resolution_wh) + self.current_count = 0 + + def trigger(self, detections: Detections) -> np.ndarray: + anchors = ( + np.ceil( + detections.get_anchor_coordinates(anchor=self.triggering_position) + ).astype(int) + - 1 + ) + is_in_zone = self.mask[anchors[:, 1], anchors[:, 0]] + self.current_count = np.sum(is_in_zone) + return is_in_zone.astype(bool) + + +class PolygonZoneAnnotator: + def __init__( + self, + zone: PolygonZone, + color: Color, + thickness: int = 2, + text_color: Color = Color.black(), + text_scale: float = 0.5, + text_thickness: int = 1, + text_padding: int = 10, + ): + self.zone = zone + self.color = color + self.thickness = thickness + self.text_color = text_color + self.text_scale = text_scale + self.text_thickness = text_thickness + self.text_padding = text_padding + self.font = cv2.FONT_HERSHEY_SIMPLEX + self.center = get_polygon_center(polygon=zone.polygon) + + def annotate(self, scene: np.ndarray) -> np.ndarray: + annotated_frame = draw_polygon( + scene=scene, + polygon=self.zone.polygon, + color=self.color, + thickness=self.thickness, + ) + + annotated_frame = draw_text( + scene=annotated_frame, + text=str(self.zone.current_count), + text_anchor=self.center, + background_color=self.color, + text_color=self.text_color, + text_scale=self.text_scale, + text_thickness=self.text_thickness, + text_padding=self.text_padding, + text_font=self.font, + ) + + return annotated_frame diff --git a/supervision/draw/utils.py b/supervision/draw/utils.py index ca9366de..e8bcea21 100644 --- a/supervision/draw/utils.py +++ b/supervision/draw/utils.py @@ -1,11 +1,11 @@ +from typing import Optional + import cv2 import numpy as np from supervision.draw.color import Color from supervision.geometry.core import Point, Rect -from typing import Optional - def draw_line( scene: np.ndarray, start: Point, end: Point, color: Color, thickness: int = 2 @@ -152,11 +152,13 @@ def draw_text( x=text_anchor.x - text_width // 2, y=text_anchor.y - text_height // 2, width=text_width, - height=text_height + height=text_height, ).pad(text_padding) if background_color is not None: - scene = draw_filled_rectangle(scene=scene, rect=text_rect, color=background_color) + scene = draw_filled_rectangle( + scene=scene, rect=text_rect, color=background_color + ) cv2.putText( img=scene,