Skip to content

Commit f9ed80f

Browse files
authored
Merge pull request #1109 from jeslinpjames/polygonzone-update
Dropped frame_resolution_wh from [PolygonZone]
2 parents da236aa + dfd9fc8 commit f9ed80f

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

supervision/detection/tools/polygon_zone.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from dataclasses import replace
23
from typing import Iterable, Optional, Tuple
34

@@ -11,7 +12,7 @@
1112
from supervision.draw.utils import draw_polygon, draw_text
1213
from supervision.geometry.core import Position
1314
from supervision.geometry.utils import get_polygon_center
14-
from supervision.utils.internal import deprecated_parameter
15+
from supervision.utils.internal import SupervisionWarnings, deprecated_parameter
1516

1617

1718
class PolygonZone:
@@ -21,7 +22,6 @@ class PolygonZone:
2122
Attributes:
2223
polygon (np.ndarray): A polygon represented by a numpy array of shape
2324
`(N, 2)`, containing the `x`, `y` coordinates of the points.
24-
frame_resolution_wh (Tuple[int, int]): The frame resolution (width, height)
2525
triggering_anchors (Iterable[sv.Position]): A list of positions specifying
2626
which anchors of the detections bounding box to consider when deciding on
2727
whether the detection fits within the PolygonZone
@@ -41,18 +41,26 @@ class PolygonZone:
4141
def __init__(
4242
self,
4343
polygon: npt.NDArray[np.int64],
44-
frame_resolution_wh: Tuple[int, int],
44+
frame_resolution_wh: Optional[Tuple[int, int]] = None,
4545
triggering_anchors: Iterable[Position] = (Position.BOTTOM_CENTER,),
4646
):
47+
if frame_resolution_wh is not None:
48+
warnings.warn(
49+
"The `frame_resolution_wh` parameter is no longer required and will be "
50+
"dropped in version supervision-0.24.0. The mask resolution is now "
51+
"calculated automatically based on the polygon coordinates.",
52+
category=SupervisionWarnings,
53+
)
54+
4755
self.polygon = polygon.astype(int)
48-
self.frame_resolution_wh = frame_resolution_wh
4956
self.triggering_anchors = triggering_anchors
5057

5158
self.current_count = 0
5259

53-
width, height = frame_resolution_wh
60+
x_max, y_max = np.max(polygon, axis=0)
61+
self.frame_resolution_wh = (x_max + 1, y_max + 1)
5462
self.mask = polygon_to_mask(
55-
polygon=polygon, resolution_wh=(width + 1, height + 1)
63+
polygon=polygon, resolution_wh=(x_max + 2, y_max + 2)
5664
)
5765

5866
def trigger(self, detections: Detections) -> npt.NDArray[np.bool_]:

0 commit comments

Comments
 (0)