In cities where traffic laws struggle to keep pace with rapid urbanization, accidents are a daily reality. Chaotic intersections, aggressive driving, and inadequate enforcement create dangerous conditions for everyone on the road.
This system provides the data and insights cities need to:
- 📍 Identify accident-prone zones with precision
- 📊 Quantify traffic risk patterns over time
- 🎯 Prioritize safety interventions based on real evidence
- 🚨 Support traffic management decisions with visual proof
Vehicle count trends over time - Understanding traffic density patterns helps cities plan better infrastructure
Real-time risk detection - Spikes indicate dangerous moments where multiple vehicles come dangerously close
DBSCAN clustering reveals accident-prone zones - These red clusters show exactly where cities should focus their safety efforts
- Powered by YOLO11s for accurate real-time detection
- Filters vehicle classes: cars, trucks, buses, motorcycles
- Optimized frame processing for faster analysis
- Smart confidence thresholding to reduce false positives
- Calculates midpoint distances between all vehicles
- Identifies dangerous proximity (< threshold distance)
- Visual alerts: 🔴 Red = Risky | 🟢 Green = Safe
- White lines connect vehicles in dangerous proximity
- All risky interactions logged for analysis
- Nearest-neighbor matching across frames
- Exponential smoothing eliminates detection jitter
- Stable bounding boxes for better visualization
Three powerful outputs generated automatically:
| Visualization | Insight Provided |
|---|---|
| Traffic vs Time | Understand peak hours and traffic flow patterns |
| Risk vs Time | Identify when accidents are most likely to occur |
| Hotspot Map | Pinpoint exact locations requiring safety intervention |
traffic_log.csv
- Frame-by-frame vehicle counts
- Risk counts per frame
- Timestamps and FPS metrics
- Perfect for time-series analysis
hotspot_points.csv
- Coordinates of every risky interaction
- Used for DBSCAN clustering
- Enables geographic hotspot mapping
pip install ultralytics opencv-python numpy pandas matplotlib scikit-learn1. Place your traffic video in your project directory
2. Update the video path in the script:
VIDEO_PATH = "/path/to/your/traffic_video.mp4"3. Run the analysis:
python traffic_analysis.py4. View results:
- 📁 CSV logs saved in project root
- 📊 PNG visualizations in
outputs/folder
Customize the system behavior by adjusting these parameters:
DIST_THRESHOLD # Risk distance (pixels) - Lower = stricter
MIN_CONF # YOLO confidence (0-1) - Higher = fewer false positives
RESIZE_WIDTH # Frame width - Lower = faster processing
PROCESS_EVERY_N # Frame skip - Higher = faster analysis
DBSCAN_EPS # Hotspot cluster radius
DBSCAN_MIN_SAMPLES # Minimum points per cluster| City Planners | Traffic Police | Researchers |
|---|---|---|
| Identify intersections requiring traffic lights or roundabouts | Deploy officers at high-risk times and locations | Study traffic behavior patterns |
| Justify infrastructure investments with data | Document dangerous driving patterns | Validate traffic flow models |
| Monitor effectiveness of safety interventions | Support enforcement strategy with evidence | Analyze the impact of road design changes |
- Advanced Models: YOLO11m or recent versions, RF-DETR for better accuracy
- Speed Analysis: Factor in velocity vectors for better risk assessment
- Heatmap Overlays: Visual intensity maps on actual road layouts
- Real-time Dashboard: FastAPI + WebSockets for live monitoring
- GPS Integration: Map hotspots to real geographic coordinates
This project is open-source and welcomes contributions! Whether you're improving detection accuracy, adding new features, or optimizing performance - your input helps make roads safer.
Open-source for learning, research, and experimental use.
In cities where traffic enforcement is challenging, data becomes the first step toward safer roads. This system transforms hours of chaotic traffic footage into actionable insights that save lives.
Every hotspot identified is a potential accident prevented.
Every risk pattern revealed is an opportunity for intervention.
Made with ❤️ for safer cities