Traffic Reducer determines which direction receives the green light by applying a simple majority-rule (argmax) algorithm to the four live vehicle counts. The approach requires no look-ahead or timing history: whichever approach lane currently holds the largest number of detected vehicles is granted the green phase immediately. This decision executes on everyDocumentation 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.
/predict call — up to once every 2 000 ms in live mode — so the signal adapts continuously as the intersection fills and empties.
Core decision logic
The/predict endpoint reads the current per-zone counts (from live detection or from the request body in manual mode) and selects the phase index using np.argmax:
| Index | Direction |
|---|---|
| 0 | Norte (North) |
| 1 | Sur (South) |
| 2 | Este (East) |
| 3 | Oeste (West) |
live_mode is True in the request body, the endpoint also calls camera.set_phase(result), which writes the human-readable label ("NORTE", "SUR", "ESTE", "OESTE") back into traffic_state['phase'] so it appears in the video overlay and is returned by /stats.
If all four counts are zero — meaning no vehicles are detected in any zone —
sum(traffic_values) is 0 and the system defaults to phase 0 (Norte) rather than choosing randomly. This prevents undefined behaviour during idle periods or when the camera source is not yet streaming.Sklearn model
At startup,app.py loads modelo_semaforo_ia.pkl, a pre-trained sklearn classifier:
/predict call — if loading failed, the endpoint returns a 500 error rather than silently falling back. In the current live implementation the np.argmax decision rule is used directly, with the model available as the foundation for extended classifiers (e.g., incorporating historical cycle data or time-of-day features).
Training dataset
The sklearn model was trained ondataset_sintetico_entrenamiento.csv, a synthetic dataset with five columns: the four directional counts and the expected winner index (GANADOR_ESPERADO):
GANADOR_ESPERADO matches the np.argmax of the four count columns in every row, confirming that the training labels are consistent with the live decision rule. The dataset uses counts in the range 0–80, matching the random range used by the /simulate endpoint.
Simulation endpoint
During development or UI testing, the/simulate endpoint returns synthetic random counts without requiring a live camera feed:
/simulate to populate its sliders, then immediately POST those values to /predict to exercise the full decision path without any camera hardware.
Frontend polling behaviour
The web dashboard drives the signal display through two polling loops:| Mode | Trigger | Endpoint | Interval |
|---|---|---|---|
| Live | Timer | POST /predict with live_mode: true | Every 2 000 ms |
| Manual | Slider change | POST /predict with explicit counts | On each input event |
{ live_mode: true } and the server reads counts directly from traffic_state. In manual mode the user adjusts per-direction sliders and the browser sends the slider values; the server ignores the camera state entirely. Both modes receive the same response shape:
GET /stats every 1 500 ms independently of /predict, keeping the vehicle-count display, pedestrian count, and emergency indicator up to date even between signal-decision calls.