1
+ import warnings
1
2
from dataclasses import replace
2
3
from typing import Iterable , Optional , Tuple
3
4
11
12
from supervision .draw .utils import draw_polygon , draw_text
12
13
from supervision .geometry .core import Position
13
14
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
15
16
16
17
17
18
class PolygonZone :
@@ -21,7 +22,6 @@ class PolygonZone:
21
22
Attributes:
22
23
polygon (np.ndarray): A polygon represented by a numpy array of shape
23
24
`(N, 2)`, containing the `x`, `y` coordinates of the points.
24
- frame_resolution_wh (Tuple[int, int]): The frame resolution (width, height)
25
25
triggering_anchors (Iterable[sv.Position]): A list of positions specifying
26
26
which anchors of the detections bounding box to consider when deciding on
27
27
whether the detection fits within the PolygonZone
@@ -41,18 +41,26 @@ class PolygonZone:
41
41
def __init__ (
42
42
self ,
43
43
polygon : npt .NDArray [np .int64 ],
44
- frame_resolution_wh : Tuple [int , int ],
44
+ frame_resolution_wh : Optional [ Tuple [int , int ]] = None ,
45
45
triggering_anchors : Iterable [Position ] = (Position .BOTTOM_CENTER ,),
46
46
):
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
+
47
55
self .polygon = polygon .astype (int )
48
- self .frame_resolution_wh = frame_resolution_wh
49
56
self .triggering_anchors = triggering_anchors
50
57
51
58
self .current_count = 0
52
59
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 )
54
62
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 )
56
64
)
57
65
58
66
def trigger (self , detections : Detections ) -> npt .NDArray [np .bool_ ]:
0 commit comments