On every unprocessed frame that arrives from the video source, Traffic Reducer runs a YOLOv8 forward pass filtered to five COCO class IDs. Each detected bounding box is then tested against four compass-direction polygon zones; the zone whose polygon contains the box’s centroid claims the detection and increments its vehicle counter. The resulting per-zone counts are what the signal-decision algorithm reads to choose the green light.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Xander44-4/traffic_reducer/llms.txt
Use this file to discover all available pages before exploring further.
YOLOv8 inference configuration
Inference is executed inside_run_yolo(), submitted asynchronously to the ThreadPoolExecutor:
agnostic_nms=True applies non-maximum suppression across all classes rather than per-class, reducing double-counted detections at intersections where vehicle types often overlap.
COCO class mapping
| Class ID | Label | Treatment |
|---|---|---|
| 0 | person | Counted as pedestrian; not assigned to a vehicle zone |
| 2 | car | Centroid tested against zone polygons |
| 3 | motorcycle | Centroid tested against zone polygons |
| 5 | bus | Centroid tested against zone polygons; also checked for emergency lights |
| 7 | truck | Centroid tested against zone polygons; also checked for emergency lights |
pedestrian_count and stored in traffic_state['pedestrians']. They do not contribute to any directional vehicle total.
Zone polygon definitions
The intersection is divided into four named zones using normalized coordinates in the range 0–1 (relative to frame width and height). At inference time the polygons are scaled to pixel coordinates by multiplying by[w, h]:
Zone assignment algorithm
For each non-pedestrian detection,_run_yolo() computes the bounding-box centroid and iterates over the four zones in definition order:
cv2.pointPolygonTest returns a value ≥ 0 when the point lies on or inside the polygon. The loop breaks on the first match, so each vehicle is counted exactly once. Detections whose centroid falls outside every zone are labelled 'other' and excluded from all counts.
The
'other' category typically includes vehicles in the centre of the intersection — where turning or crossing traffic briefly occupies no single approach — and detections near the frame boundary where no zone polygon reaches.Overlay drawing
After_run_yolo() returns, _draw_overlay() paints zone outlines and color-coded bounding boxes directly onto the display frame before JPEG encoding. Zone polygon borders are drawn first; bounding boxes are layered on top.
Zone colors
| Zone | Direction | BGR Color | Appearance |
|---|---|---|---|
norte | North | (255, 180, 60) | Amber |
sur | South | (60, 200, 255) | Cyan |
este | East | (90, 230, 130) | Green |
oeste | West | (200, 120, 255) | Purple |
- Pedestrians (
'ped'): bright blue-white(255, 200, 0), 2 px border - Emergency vehicles (
'emergency'): vivid red(0, 50, 255), 3 px border - Unmatched vehicles (
'other'): mid-grey(130, 130, 130), 1 px border
Scale correction
Because YOLO always runs on a copy of the frame at the native capture resolution while the display frame may have been resized,_draw_overlay() applies per-axis scale factors before drawing:
JPEG encoding quality
The final annotated frame is encoded withcv2.imencode at a quality setting that balances dashboard latency against visual fidelity:
| Source mode | JPEG quality |
|---|---|
local | 82 |
youtube | 78 |