Skip to main content

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.

Traffic Reducer exposes eight HTTP endpoints on http://127.0.0.1:5000. All endpoints accept and return JSON (except /video_feed, which streams MJPEG frames). Start the server with python traffic_app/app.py before making any requests.

GET /

Serves the main web dashboard as an HTML page. Open this URL in a browser to access the full Traffic Reducer interface with live camera feed, signal phase display, and control buttons. No parameters. Returns: HTML page (text/html).
curl http://127.0.0.1:5000/

GET /video_feed

Streams the annotated camera feed as a continuous MJPEG response. Each frame is JPEG-encoded and includes zone polygon overlays (Norte, Sur, Este, Oeste), bounding boxes for detected vehicles and pedestrians, and red highlights for emergency vehicles. Content-Type: multipart/x-mixed-replace; boundary=frame Embed the stream directly in an HTML page:
<img src="http://127.0.0.1:5000/video_feed" />
The stream stays open until the client disconnects. If no video source is active, the feed returns a dark placeholder frame.

GET /stats

Returns the current traffic state captured from the live camera or local video. All counts reflect the most recent completed YOLOv8 inference pass. No request body.
norte
integer
Vehicle count detected in the North zone.
sur
integer
Vehicle count detected in the South zone.
este
integer
Vehicle count detected in the East zone.
oeste
integer
Vehicle count detected in the West zone.
pedestrians
integer
Total number of pedestrian detections across all zones.
emergency
boolean
true if an emergency vehicle (bus class 5 or truck class 7 with red/yellow/blue color signature) is currently detected in the frame.
phase
string
Current active signal phase label. Possible values: "NORTE", "SUR", "ESTE", "OESTE", "INIT".
priority
string
The direction with the highest vehicle count (e.g., "ESTE"). Returns "--" when all counts are zero.
priority_idx
integer
Index of the priority direction: 0 = Norte, 1 = Sur, 2 = Este, 3 = Oeste. Returns -1 when all counts are zero.
mode
string
Current video source mode. One of "idle", "youtube", or "local".
Example response:
{
  "norte": 12,
  "sur": 8,
  "este": 24,
  "oeste": 5,
  "pedestrians": 2,
  "emergency": false,
  "phase": "ESTE",
  "priority": "ESTE",
  "priority_idx": 2,
  "mode": "youtube"
}
curl http://127.0.0.1:5000/stats

POST /predict

Determines the recommended winning signal phase using np.argmax() on the four vehicle counts and returns the result. In manual mode, the counts you supply in the request body are used directly. In live mode, the counts are read from the camera’s latest YOLOv8 inference result, and the winning phase is automatically applied to the camera display via set_phase().
The endpoint requires modelo_semaforo_ia.pkl to be successfully loaded at startup — it returns 500 with {"error": "Modelo no disponible"} if the model is absent, even though the argmax computation itself does not call model.predict(). Verify MODEL_PATH in app.py points to the correct file to pass this guard.
Request body (JSON):
live_mode
boolean
default:"false"
When true, reads vehicle counts from the live camera instead of the request body. All direction fields are ignored.
norte
number
default:"0"
Vehicle count for the North direction. Used only when live_mode is false.
sur
number
default:"0"
Vehicle count for the South direction. Used only when live_mode is false.
este
number
default:"0"
Vehicle count for the East direction. Used only when live_mode is false.
oeste
number
default:"0"
Vehicle count for the West direction. Used only when live_mode is false.
prediction
integer
Winning phase index: 0 = Norte, 1 = Sur, 2 = Este, 3 = Oeste. When all counts are zero, returns 0.
traffic_data
object
The Norte, Sur, Este, and Oeste counts (as floats) that were actually used in the prediction.
Example — manual mode:
curl -X POST http://127.0.0.1:5000/predict \
  -H 'Content-Type: application/json' \
  -d '{"norte": 10, "sur": 55, "este": 10, "oeste": 10}'
{
  "prediction": 1,
  "traffic_data": {
    "norte": 10.0,
    "sur": 55.0,
    "este": 10.0,
    "oeste": 10.0
  }
}
Example — live mode:
curl -X POST http://127.0.0.1:5000/predict \
  -H 'Content-Type: application/json' \
  -d '{"live_mode": true}'

POST /set_source

Switches the active video source. Closing the current source (YouTube ffmpeg pipe or local cv2.VideoCapture) happens immediately; the new source opens on the next loop iteration of the background thread. Request body (JSON):
mode
string
required
Target source mode. Must be "youtube" or "local". The value "idle" cannot be set through this endpoint.
Returns: {"mode": "<applied_mode>"} Errors:
  • 400 if mode is not "youtube" or "local".
  • 400 if mode is "local" and traffic_app/static/traffic_dron_view.mp4 does not exist on disk.
curl -X POST http://127.0.0.1:5000/set_source \
  -H 'Content-Type: application/json' \
  -d '{"mode": "local"}'
{"mode": "local"}

POST /set_speed

Sets the playback speed multiplier for local video. The value is clamped to a minimum of 0.05 to prevent a zero or negative interval. Has no effect on the YouTube stream. Request body (JSON):
speed
number
required
Speed multiplier. 1.0 = normal speed, 2.0 = double speed, 0.5 = half speed. Clamped to ≥ 0.05.
Returns: {"speed": <applied_value>} — the clamped value that was actually applied.
curl -X POST http://127.0.0.1:5000/set_speed \
  -H 'Content-Type: application/json' \
  -d '{"speed": 1.5}'
{"speed": 1.5}

GET /source_status

Returns the current video source mode and whether the local video file exists on disk. The dashboard calls this endpoint on load to decide whether to enable the VIDEO LOCAL button. No request body.
mode
string
Current source mode: "idle", "youtube", or "local".
has_local
boolean
true if traffic_app/static/traffic_dron_view.mp4 exists on the server’s filesystem.
curl http://127.0.0.1:5000/source_status
{"mode": "idle", "has_local": true}

GET /simulate

Returns random vehicle counts between 0 and 80 for each direction. Useful for testing the dashboard and the /predict endpoint without a live camera feed or local video file. No request body.
norte
integer
Random vehicle count for North (0–80).
sur
integer
Random vehicle count for South (0–80).
este
integer
Random vehicle count for East (0–80).
oeste
integer
Random vehicle count for West (0–80).
curl http://127.0.0.1:5000/simulate
{"norte": 34, "sur": 71, "este": 12, "oeste": 55}

Build docs developers (and LLMs) love