Polygon Zone
PolygonZone
A class for defining a polygon-shaped zone within a frame for detecting objects.
Warning
PolygonZone uses the tracker_id
. Read
here to learn how to plug
tracking into your inference pipeline.
Attributes:
Name | Type | Description |
---|---|---|
polygon |
ndarray
|
A polygon represented by a numpy array of shape
|
triggering_anchors |
Iterable[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 |
ndarray
|
The 2D bool mask for the polygon zone |
Example
import supervision as sv
from ultralytics import YOLO
import numpy as np
import cv2
image = cv2.imread(<SOURCE_IMAGE_PATH>)
model = YOLO("yolo11s")
tracker = sv.ByteTrack()
polygon = np.array([[100, 200], [200, 100], [300, 200], [200, 300]])
polygon_zone = sv.PolygonZone(polygon=polygon)
result = model.infer(image)[0]
detections = sv.Detections.from_ultralytics(result)
detections = tracker.update_with_detections(detections)
is_detections_in_zone = polygon_zone.trigger(detections)
print(polygon_zone.current_count)
Source code in supervision/detection/tools/polygon_zone.py
Functions¶
trigger(detections)
¶
Determines if the detections are within the polygon zone.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detections |
Detections
|
The detections to be checked against the polygon zone |
required |
Returns:
Type | Description |
---|---|
NDArray[bool_]
|
np.ndarray: A boolean numpy array indicating if each detection is within the polygon zone |
Source code in supervision/detection/tools/polygon_zone.py
PolygonZoneAnnotator
A class for annotating a polygon-shaped zone within a frame with a count of detected objects.
Attributes:
Name | Type | Description |
---|---|---|
zone |
PolygonZone
|
The polygon zone to be annotated |
color |
Color
|
The color to draw the polygon lines, default is white |
thickness |
int
|
The thickness of the polygon lines, default is 2 |
text_color |
Color
|
The color of the text on the polygon, default is black |
text_scale |
float
|
The scale of the text on the polygon, default is 0.5 |
text_thickness |
int
|
The thickness of the text on the polygon, default is 1 |
text_padding |
int
|
The padding around the text on the polygon, default is 10 |
font |
int
|
The font type for the text on the polygon, default is cv2.FONT_HERSHEY_SIMPLEX |
center |
Tuple[int, int]
|
The center of the polygon for text placement |
display_in_zone_count |
bool
|
Show the label of the zone or not. Default is True |
opacity |
The opacity of zone filling when drawn on the scene. Default is 0 |
Source code in supervision/detection/tools/polygon_zone.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
Functions¶
annotate(scene, label=None)
¶
Annotates the polygon zone within a frame with a count of detected objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
The image on which the polygon zone will be annotated |
required |
label |
Optional[str]
|
A label for the count of detected objects within the polygon zone (default: None) |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The image with the polygon zone and count of detected objects |