Live public traffic camera β YOLO11n vehicle detection β density-based signal control
Hridam Biswas Β· KIIT University
Traffic signals in most cities run on fixed timers β a car sits at a red light on an empty road for 2 minutes while no one crosses the other way. This system reads a live traffic camera, counts how much road is covered by vehicles, and makes a real-time signal decision. No fixed timers. No wasted time.
Public YouTube Traffic Camera (yt-dlp)
β
βΌ
ββββββββββββββββββββββββ
β Frame Capture β OpenCV VideoCapture β raw BGR frame
ββββββββββββ¬ββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β YOLO11n Detection β Detects: car Β· truck Β· bus Β· motorcycle
β (COCO classes) β Outputs: bounding boxes + class IDs
ββββββββββββ¬ββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β Density Calculation β
β β
β density = Ξ£(bbox areas) / frame_area β
β βββββββββββββββββββββββββββ β
β Γ 100 % β
ββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Rolling Average β Last N frames smoothed β removes flicker
β Smoothing (N=8) β
ββββββββββββ¬ββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββ
β Signal Decision β
β β
β density < 10% β π’ GREEN β
β density < 30% β π‘ YELLOW β
β density β₯ 30% β π΄ RED β
ββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββ
β Live Display β Bounding boxes Β· signal badge
β (OpenCV window) β density bar Β· vehicle count Β· FPS
ββββββββββββββββββββββββ
| Smoothed Density | Signal | Meaning |
|---|---|---|
< 10 % |
π’ GREEN | Road is clear β let traffic through immediately |
10 % β 30 % |
π‘ YELLOW | Moderate traffic β prepare to stop or go |
β₯ 30 % |
π΄ RED | Heavy congestion β hold all crossing traffic |
Density is rolling-averaged over the last 8 frames to prevent the signal flickering on every detection jitter.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Vehicles: 7 ββββββββββββ β
β FPS: 24.3 β GREEN β β
β ββββββββββββ β
β β
β [ live camera feed + bounding boxes ] β
β β
β Density: 8.4% ββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Element | Description |
|---|---|
| Bounding boxes | Orange rectangles around every detected vehicle |
| Signal badge | Top-right color block β GREEN / YELLOW / RED |
| Density bar | Bottom-left progress bar; color matches signal state |
| Vehicle count | Total vehicles detected in current frame |
| FPS | Live inference + display frame rate |
git clone https://github.com/Hridambiswas/traffic.git
cd traffic
pip install -r requirements.txt
# Random public camera from config
python main.py
# Specific YouTube live stream
python main.py --source "https://www.youtube.com/watch?v=ByED80IKdIU"
# Stricter detection + wider smoothing window
python main.py --conf 0.5 --window 12
# Lower resolution stream (faster)
python main.py --height 360Press Q to quit.
| Flag | Default | Description |
|---|---|---|
--source |
random from config.py |
YouTube live URL or direct stream URL |
--conf |
0.40 |
YOLO confidence threshold (higher β fewer false positives) |
--window |
8 |
Rolling average window size in frames |
--height |
480 |
Preferred stream resolution height |
All tunable parameters live in config.py:
VEHICLE_CLASSES = {2, 3, 5, 7} # car, motorcycle, bus, truck (COCO IDs)
GREEN_THRESHOLD = 10.0 # % density below which signal is GREEN
YELLOW_THRESHOLD = 30.0 # % density below which signal is YELLOW
SMOOTH_WINDOW = 8 # rolling average frame count
CONF_THRESHOLD = 0.40 # YOLO detection confidence
DISPLAY_WIDTH = 960 # output window width
DISPLAY_HEIGHT = 540 # output window height| Parameter | Lower value | Higher value |
|---|---|---|
GREEN_THRESHOLD |
More likely to stay yellow/red | Easier to get green signal |
YELLOW_THRESHOLD |
Red trips sooner | More tolerance before red |
SMOOTH_WINDOW |
Reacts faster, more flicker | Stable signal, slower response |
CONF_THRESHOLD |
Detects more vehicles | Only high-confidence detections |
Public live traffic cameras are configured in config.py:
| Camera | Location | URL |
|---|---|---|
| Cam 1 | NYC Times Square | youtube.com/watch?v=ByED80IKdIU |
| Cam 2 | Chicago Traffic | youtube.com/watch?v=AdUw5RdyZxI |
| Cam 3 | LA Highway | youtube.com/watch?v=1EiC9bvVGnk |
Add any public YouTube live stream to
CAMERA_SOURCESinconfig.py. The system randomly picks one on start and auto-reconnects if the stream drops.
| COCO Class ID | Label | Included |
|---|---|---|
| 2 | car | β |
| 3 | motorcycle | β |
| 5 | bus | β |
| 7 | truck | β |
| 0 | person | β |
| 9 | traffic light | β |
Only vehicle classes contribute to density. Pedestrians and static objects are ignored.
traffic/
β
βββ main.py β entry point Β· CLI args Β· stream reconnect loop
βββ config.py β all thresholds, paths, camera URLs
β
βββ stream.py β yt-dlp handler Β· random source with fallback
βββ detector.py β YOLO11n detection Β· bbox-area density calc
βββ signal.py β rolling-average smoothed signal decision
βββ display.py β OpenCV overlay renderer
β
βββ requirements.txt
| Package | Version | Purpose |
|---|---|---|
ultralytics |
β₯ 8.4.0 | YOLO11n model + inference |
opencv-python |
β₯ 4.11.0 | Frame capture, rendering, live window |
numpy |
β₯ 1.26.0 | Array ops for density computation |
yt-dlp |
β₯ 2026.1.0 | Public YouTube stream URL extraction |
Built by Hridam Biswas Β· KIIT University