Count detections inside polygonal zones—ideal for traffic, retail, and other analytics.
Estimated read time: 4 minutes
Many applications (e.g., traffic, retail, safety) need counts within specific regions, not across the entire frame. Zone-based counting lets you define polygonal areas and track totals per zone, optionally per class. This guide uses a YOLOv8 detection model and an interactive zone tool so you can adjust zones live, without changing the code.
How it works
A detection model runs on each frame to identify objects.
A ZoneCounter analyzer checks which detections fall inside your polygons and updates the counts.
You can interactively adjust zone vertices in the display window to fine-tune boundaries—no code changes needed.
Basic Zone Counter
Cars on a freeway passing through two zone counters.
Use this static setup when you want fixed polygon zones with automatic overlays. Update the coordinates in polygon_zones to match your scene so ZoneCounter can draw per-zone totals and populate inference_result.zone_counts.
Example
Interactive Zone Counter
Cars on a freeway passing through two interactive zone counters.
Pick the interactive workflow when you need to reposition zones during a live session. Drag an entire polygon with the left mouse button, then right-click a corner handle to refine that vertex while the stream keeps running.
Interactive editing: click and drag zone vertices in the display window to reshape; press q to close the window.
Classes: use output_class_set to filter which classes are counted; omit it to include all detected labels.
Trigger point: AnchorPoint.CENTER works well for people and vehicles; try BOTTOM_CENTER for more ground-contact accuracy.
Coordinates: define polygons in image pixels for the resolution you'll display.
Performance: fewer, larger zones are more efficient than many small ones; filtering to needed classes also helps reduce overhead.
Mouse controls: use the left mouse button (LMB) to drag the entire zone; right-click (RMB) a corner handle to adjust that vertex precisely.