Skip to content

Hridambiswas/traffic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🚦 Real-Time Traffic Signal Decision System

Live public traffic camera β†’ YOLO11n vehicle detection β†’ density-based signal control

Python YOLO OpenCV yt-dlp License

Hridam Biswas Β· KIIT University


The Problem

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.


Pipeline

 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
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Signal Logic

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.


Live Window Layout

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  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

Quick Start

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 360

Press Q to quit.


CLI Reference

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

Configuration

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

Camera Sources

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_SOURCES in config.py. The system randomly picks one on start and auto-reconnects if the stream drops.


YOLO Vehicle Classes

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.


Project Structure

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

Dependencies

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

Releases

Packages

Contributors

Languages