🚦 AI-Powered Traffic Analysis System
Real-Time Vehicle Detection • Tracking • Speed Estimation • Red-Light Violation Detection • LLM Traffic Reports
This project is a complete end-to-end traffic intelligence system built using Computer Vision + Transformers + LLMs. It processes a traffic video and automatically produces:
✔ Vehicle detections ✔ Multi-object tracking with stable IDs ✔ Vehicle counting ✔ Speed estimation (km/h) ✔ Traffic light (red/green) detection ✔ Red-light violation logs ✔ CSV/JSON event data ✔ Human-readable traffic report generated by an LLM
Demo Video
Event Log (CSV Output)
| frame | time_sec | track_id | class_name | speed_kmh | event_type | light_state |
|---|---|---|---|---|---|---|
| 134 | 4.466 | 16 | car | 98.2953 | RED_LIGHT_VIOLATION | RED |
| 157 | 5.2333 | 18 | car | 52.4403 | RED_LIGHT_VIOLATION | RED |
| 696 | 23.2 | 44 | car | 54.8764 | RED_LIGHT_VIOLATION | RED |
| 735 | 24.5 | 45 | car | 100.5324 | RED_LIGHT_VIOLATION | RED |
| 804 | 26.8 | 46 | car | 62.6552 | RED_LIGHT_VIOLATION | RED |
| 881 | 29.3666 | 50 | car | 59.4335 | CROSSING | GREEN |
| 935 | 31.1666 | 52 | car | 67.6318 | CROSSING | GREEN |
| 1049 | 34.9666 | 55 | car | 53.6327 | CROSSING | GREEN |
| 1073 | 35.766 | 62 | motorcycle | 104.6876 | CROSSING | GREEN |
| 1074 | 35.8 | 62 | motorcycle | 58.5484 | CROSSING | GREEN |
| 1079 | 36.9666 | 59 | motorcycle | 70.4797 | CROSSING | GREEN |
AI-Generated Traffic Report
Summary of Observations:
- Total Crossings: 11
- Red-Light Violations: 5
- Average Speed: 71.2 km/h
- Maximum Speed: 104.7 km/h
Traffic Density: The traffic density appears to be moderate, with a total of 11 crossings recorded during the observation period. This level of density suggests a manageable flow of vehicles, but the presence of multiple violations indicates potential issues with driver compliance and awareness.
Speed Behavior Assessment: The average speed of 71.2 km/h is above typical urban speed limits, which often range from 50 to 60 km/h. The maximum recorded speed of 104.7 km/h is particularly concerning, as it significantly exceeds safe driving limits, especially in areas with traffic signals. This behavior poses a risk not only to the drivers but also to pedestrians and other road users.
Violation Commentary: A total of 5 red-light violations were recorded, representing a significant 45% of total crossings. These violations occurred under a red light condition, indicating a serious disregard for traffic signals. The speeds at which these violations occurred ranged from 52.4 km/h to 100.5 km/h, further exacerbating the risk of accidents. The high rate of violations suggests a need for increased enforcement and public awareness regarding traffic laws.
Safety Recommendations:
-
Increased Enforcement: Implement targeted enforcement measures at the intersection to deter red-light violations. This could include the use of automated traffic cameras and increased police presence during peak hours.
-
Public Awareness Campaigns: Launch educational campaigns to inform drivers about the dangers of speeding and running red lights. Emphasizing the legal consequences and potential accidents could help change driver behavior.
-
Traffic Signal Improvements: Consider upgrading traffic signal visibility and timing, as well as installing additional signage to alert drivers of upcoming signals. Enhanced visibility may help reduce violations and improve overall traffic safety.
📌 Key Features
1. RT-DETR Transformer-Based Detection
- High-accuracy transformer detector
- Detects: car, truck, bus, motorbike, bicycle
2. Custom Object Tracking
A lightweight, frame-to-frame centroid tracker for stable IDs.
3. Vehicle Counting
A virtual line detects crossing events.
4. Speed Estimation
- Pixel distance per frame
- Converts to km/h
- Logged into CSV
5. Red-Light Violation Detection
- Detects traffic light color (RED/GREEN)
- Logs violations automatically
6. Structured Event Logging
Exports:
results/
├── traffic_events_core.csv
└── traffic_events_core.json
Each log entry contains:
- Frame
- Timestamp
- Vehicle ID
- Vehicle class
- Speed
- Event type
- Light state
🧠 7. LLM-Powered Traffic Summary
Using gpt-4o-mini, the system generates a professional traffic analysis report.
🏗 Project Structure
traffic-analysis-ai/
├── src/
│ ├── traffic_core.py # Vision pipeline (video → logs)
│ └── llm_report.py # LLM report generator
├── data/ # Input videos (ignored in Git)
├── results/ # Output video + event logs (ignored)
├── requirements.txt
├── .env # OpenAI key (not committed)
├── .gitignore
└── README.md
🛠 Tech Stack
| Component | Technology |
|---|---|
| Object Detection | RT-DETR (Ultralytics) |
| Tracking | Custom centroid tracker |
| Speed Estimation | Pixel → meter conversion |
| Traffic Light Detection | HSV thresholding |
| Event Logging | CSV + JSON |
| LLM | gpt-4o-mini |
| Languages | Python, OpenCV |
| Deployment | Standalone scripts / Colab |
🚀 Getting Started
1️⃣ Clone Repository
git clone https://github.com/<your-username>/traffic-analysis-ai.git
cd traffic-analysis-ai
2️⃣ Install Dependencies
pip install -r requirements.txt
3️⃣ Add Your Traffic Video
Place your input video here:
data/traffic.mp4
(or update the path in src/traffic_core.py).
4️⃣ Add Your OpenAI API Key
Create a .env file in the project root:
echo "OPENAI_API_KEY=your_openai_key_here" > .env
.envis ignored by Git for security.
🎬 Run Traffic Analysis
python src/traffic_core.py
This will generate:
results/output_core.mp4results/traffic_events_core.csvresults/traffic_events_core.json
🧠 Generate LLM Traffic Report
python src/llm_report.py
Sample console output:
Traffic during the analyzed period was moderately dense...
Average speeds remained within a safe range...
One red-light violation was detected...
Recommended improvements include...
📈 Sample Log Output (CSV)
frame,time_sec,track_id,class_name,speed_kmh,event_type,light_state
109,3.63,2,car,42.1,CROSSING,GREEN
215,7.16,7,truck,38.6,RED_LIGHT_VIOLATION,RED
332,11.06,5,bus,29.8,CROSSING,GREEN
🧩 System Architecture
┌────────────────┐
│ traffic.mp4 │
└──────┬─────────┘
│
┌───────▼────────┐
│ RT-DETR │
└───────┬────────┘
│ detections
┌───────▼──────────┐
│ Simple Tracker │
└───────┬──────────┘
│
┌───────▼──────────────┐
│ Speed Estimator │
└───────┬──────────────┘
│
┌───────▼──────────────────┐
│ Event Logger (CSV/JSON) │
└───────┬──────────────────┘
│
┌───────▼───────────────────────┐
│ gpt-4o-mini LLM Report │
└───────────────────────────────┘
🤝 Contributing
Pull requests are welcome. If you find a bug or have an idea, feel free to open an issue.