From b7607c6f152d73ee5556c67bdb6ff94b54d29847 Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Thu, 11 Apr 2024 18:57:30 +0530 Subject: [PATCH 1/8] Updated PolyZone class to compute frame_resolution_wh based on polygon coordinates --- supervision/detection/tools/polygon_zone.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index 00b746aa..f4825125 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -2,6 +2,7 @@ from dataclasses import replace from typing import Iterable, Optional, Tuple import cv2 +import warnings import numpy as np import numpy.typing as npt @@ -11,7 +12,7 @@ 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 -from supervision.utils.internal import deprecated_parameter +from supervision.utils.internal import deprecated_parameter, SupervisionWarnings class PolygonZone: @@ -41,18 +42,24 @@ class PolygonZone: def __init__( self, polygon: npt.NDArray[np.int64], - frame_resolution_wh: Tuple[int, int], triggering_anchors: Iterable[Position] = (Position.BOTTOM_CENTER,), + frame_resolution_wh: Optional[Tuple[int, int]] = None, ): + if frame_resolution_wh is not None: + warnings.warn( + "The `frame_resolution_wh` parameter is no longer required and will be dropped in version supervision-0.24.0. The mask resolution is now calculated automatically based on the polygon coordinates.", + category=SupervisionWarnings, + ) + self.polygon = polygon.astype(int) - self.frame_resolution_wh = frame_resolution_wh self.triggering_anchors = triggering_anchors self.current_count = 0 - width, height = frame_resolution_wh + x_max, y_max = np.max(polygon, axis=0) + self.frame_resolution_wh = (x_max , y_max) self.mask = polygon_to_mask( - polygon=polygon, resolution_wh=(width + 1, height + 1) + polygon=polygon, resolution_wh=(x_max + 1, y_max + 1) ) def trigger(self, detections: Detections) -> npt.NDArray[np.bool_]: From 28baf18cb6dcd6005fa030e35390028adfe350e8 Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Thu, 11 Apr 2024 18:59:48 +0530 Subject: [PATCH 2/8] updated PolygonZone Attributes docs --- supervision/detection/tools/polygon_zone.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index f4825125..72306e7e 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -22,13 +22,16 @@ class PolygonZone: Attributes: polygon (np.ndarray): A polygon represented by a numpy array of shape `(N, 2)`, containing the `x`, `y` coordinates of the points. - frame_resolution_wh (Tuple[int, int]): The frame resolution (width, height) triggering_anchors (Iterable[sv.Position]): A list of positions specifying which anchors of the detections bounding box to consider when deciding on whether the detection fits within the PolygonZone (default: (sv.Position.BOTTOM_CENTER,)). current_count (int): The current count of detected objects within the zone mask (np.ndarray): The 2D bool mask for the polygon zone + frame_resolution_wh (Optional[Tuple[int, int]], optional): DEPRECATED. The frame resolution + (width, height). This parameter is no longer required and will be dropped in version + supervision-0.24.0. The mask resolution is now calculated automatically based on the + polygon coordinates. Defaults to None. """ @deprecated_parameter( From b64c61c3885fdd449e0dfae9080c1c6073c75aed Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Thu, 11 Apr 2024 19:04:06 +0530 Subject: [PATCH 3/8] formated code --- supervision/detection/tools/polygon_zone.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index 72306e7e..ccc6ec95 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -50,9 +50,10 @@ class PolygonZone: ): if frame_resolution_wh is not None: warnings.warn( - "The `frame_resolution_wh` parameter is no longer required and will be dropped in version supervision-0.24.0. The mask resolution is now calculated automatically based on the polygon coordinates.", - category=SupervisionWarnings, - ) + "The `frame_resolution_wh` parameter is no longer required and will be dropped in version " + "supervision-0.24.0. The mask resolution is now calculated automatically based on the polygon coordinates.", + category=SupervisionWarnings, + ) self.polygon = polygon.astype(int) self.triggering_anchors = triggering_anchors @@ -60,7 +61,7 @@ class PolygonZone: self.current_count = 0 x_max, y_max = np.max(polygon, axis=0) - self.frame_resolution_wh = (x_max , y_max) + self.frame_resolution_wh = (x_max, y_max) self.mask = polygon_to_mask( polygon=polygon, resolution_wh=(x_max + 1, y_max + 1) ) @@ -100,6 +101,7 @@ class PolygonZone: return is_in_zone.astype(bool) + class PolygonZoneAnnotator: """ A class for annotating a polygon-shaped zone within a From 743e42080bcc4ba60ff7ffda03f8c50e52a6edfc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 11 Apr 2024 13:57:48 +0000 Subject: [PATCH 4/8] =?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/detection/tools/polygon_zone.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index ccc6ec95..f78bd609 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -1,8 +1,8 @@ +import warnings from dataclasses import replace from typing import Iterable, Optional, Tuple import cv2 -import warnings import numpy as np import numpy.typing as npt @@ -12,7 +12,7 @@ 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 -from supervision.utils.internal import deprecated_parameter, SupervisionWarnings +from supervision.utils.internal import SupervisionWarnings, deprecated_parameter class PolygonZone: @@ -101,7 +101,6 @@ class PolygonZone: return is_in_zone.astype(bool) - class PolygonZoneAnnotator: """ A class for annotating a polygon-shaped zone within a From 510df5457255aa70cdb8a8daa24aedaa624d9dd9 Mon Sep 17 00:00:00 2001 From: Jeslin P James Date: Fri, 12 Apr 2024 16:16:48 +0530 Subject: [PATCH 5/8] Updated order of parameters in PolygonZone __init__() --- supervision/detection/tools/polygon_zone.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index ccc6ec95..fa71b124 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -1,8 +1,8 @@ +import warnings from dataclasses import replace from typing import Iterable, Optional, Tuple import cv2 -import warnings import numpy as np import numpy.typing as npt @@ -12,7 +12,7 @@ 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 -from supervision.utils.internal import deprecated_parameter, SupervisionWarnings +from supervision.utils.internal import SupervisionWarnings, deprecated_parameter class PolygonZone: @@ -45,8 +45,8 @@ class PolygonZone: def __init__( self, polygon: npt.NDArray[np.int64], - triggering_anchors: Iterable[Position] = (Position.BOTTOM_CENTER,), frame_resolution_wh: Optional[Tuple[int, int]] = None, + triggering_anchors: Iterable[Position] = (Position.BOTTOM_CENTER,), ): if frame_resolution_wh is not None: warnings.warn( @@ -101,7 +101,6 @@ class PolygonZone: return is_in_zone.astype(bool) - class PolygonZoneAnnotator: """ A class for annotating a polygon-shaped zone within a From 0ad3796571e04b3435d1a31d4adec483a18412e0 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Fri, 12 Apr 2024 16:13:09 +0200 Subject: [PATCH 6/8] bug fix --- supervision/detection/tools/polygon_zone.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index fa71b124..244b793b 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -61,9 +61,9 @@ class PolygonZone: self.current_count = 0 x_max, y_max = np.max(polygon, axis=0) - self.frame_resolution_wh = (x_max, y_max) + self.frame_resolution_wh = (x_max + 1, y_max + 1) self.mask = polygon_to_mask( - polygon=polygon, resolution_wh=(x_max + 1, y_max + 1) + polygon=polygon, resolution_wh=(x_max + 2, y_max + 2) ) def trigger(self, detections: Detections) -> npt.NDArray[np.bool_]: From 2c62f0b26dfdac73ce941e425c2325ad87f2499b Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Fri, 12 Apr 2024 16:15:10 +0200 Subject: [PATCH 7/8] drop `frame_resolution_wh` from docs --- supervision/detection/tools/polygon_zone.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index 244b793b..561cb9ce 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -28,10 +28,6 @@ class PolygonZone: (default: (sv.Position.BOTTOM_CENTER,)). current_count (int): The current count of detected objects within the zone mask (np.ndarray): The 2D bool mask for the polygon zone - frame_resolution_wh (Optional[Tuple[int, int]], optional): DEPRECATED. The frame resolution - (width, height). This parameter is no longer required and will be dropped in version - supervision-0.24.0. The mask resolution is now calculated automatically based on the - polygon coordinates. Defaults to None. """ @deprecated_parameter( From dfd9fc8e6d696eec671445136b8a50a5c2c901bd Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Fri, 12 Apr 2024 16:17:29 +0200 Subject: [PATCH 8/8] fix too long string --- supervision/detection/tools/polygon_zone.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/supervision/detection/tools/polygon_zone.py b/supervision/detection/tools/polygon_zone.py index 561cb9ce..a1997212 100644 --- a/supervision/detection/tools/polygon_zone.py +++ b/supervision/detection/tools/polygon_zone.py @@ -46,8 +46,9 @@ class PolygonZone: ): if frame_resolution_wh is not None: warnings.warn( - "The `frame_resolution_wh` parameter is no longer required and will be dropped in version " - "supervision-0.24.0. The mask resolution is now calculated automatically based on the polygon coordinates.", + "The `frame_resolution_wh` parameter is no longer required and will be " + "dropped in version supervision-0.24.0. The mask resolution is now " + "calculated automatically based on the polygon coordinates.", category=SupervisionWarnings, )