An Intelligent Traffic Management System using Computer Vision & Deep Learning
Project NETRA addresses the critical issue of urban traffic congestion and delayed emergency services. Unlike traditional fixed-timer traffic lights, NETRA uses real-time camera feeds to calculate traffic density and adjust signal timings dynamically. It features ByteTrack Multi-Object Tracking for persistent vehicle identification, speed estimation, and unique vehicle counting โ plus an Automatic Ambulance Detection System that overrides signals to provide a "Green Corridor" for emergency vehicles.
๐ Web Dashboard: https://project-netra-dr457xmtu7dhxvakejabwz.streamlit.app/
Experience the interactive analytics dashboard with real-time traffic visualizations, data explorer, and comprehensive system insights!
Above: The NETRA system in action โ Real-time detection and tracking of vehicles across two lanes. Each vehicle receives a persistent Track ID and speed label. Lane 1 (Red) and Lane 2 (Blue) show live counts, unique vehicle totals, signal times, and average speeds. Movement trails visualize each vehicle's trajectory.
๐ง Hybrid AI Architecture: Utilizes a dual-model strategy:
- Generalist Model (yolov8m): Detects standard vehicles (Cars, Trucks, Buses, Rickshaws) with high accuracy.
- Specialist Model (Custom Trained): A dedicated model trained via Transfer Learning to specifically detect Ambulances.
๐ ByteTrack Vehicle Tracking: Persistent multi-object tracking powered by the supervision library:
- Unique Vehicle Counting: Each vehicle gets a stable Track ID โ eliminates per-frame double-counting.
- Speed Estimation: Real-time speed (px/s) calculated from position history over recent frames.
- Movement Trails: Fading trajectory lines visualize each vehicle's path across the frame.
- Session Statistics: Cumulative unique vehicle totals per lane across the entire session.
โฑ๏ธ Dynamic Signal Timer: Replaces static timers with an adaptive algorithm (
๐ Emergency Override Module: Instantly detects approaching ambulances (with geometric & confidence filtering) to clear the lane immediately.
๐ฃ๏ธ Multi-Lane Logic: Supports distinct ROI (Region of Interest) definitions to manage multiple lanes independently.
๐ Traffic Analytics: Automatically logs extended traffic data (vehicle counts, unique counts, speeds, timestamps, signal times) to a CSV database for urban planning analysis.
- Language: Python 3.x
- Computer Vision: OpenCV (cv2)
- Deep Learning: YOLOv8 (Ultralytics)
- Object Tracking: ByteTrack via Supervision library
- Data Processing: NumPy, Pandas (for analytics)
- Web Dashboard: Streamlit
- Training Environment: Google Colab (Tesla T4 GPU)
PROJECT-NETRA/
โโโ main.py # Main app (detection + ByteTrack tracking)
โโโ requirements.txt # Python dependencies
โโโ README.md # Project documentation
โโโ LICENSE # MIT License
โ
โโโ src/ # Source code modules
โ โโโ tracker.py # ByteTrack vehicle tracking module
โ โโโ model_runtime.py # Device + precision runtime tuning helpers
โ โโโ export_model.py # Export utility (ONNX/CoreML/TensorRT)
โ โโโ analytics.py # Interactive analytics dashboard
โ โโโ analytics_report.py # Headless analytics (no GUI)
โ โโโ web_dashboard.py # Streamlit web dashboard
โ โโโ utils/ # Utility scripts
โ โโโ check_brain.py # Model verification tool
โ โโโ mouse_finder.py # Coordinate selection helper
โ
โโโ models/ # AI model weights
โ โโโ best.pt # Custom ambulance detection model
โ โโโ yolov8m.pt # YOLOv8 traffic detection model
โ
โโโ data/ # Data storage
โ โโโ traffic_logs/ # CSV traffic logs (auto-generated)
โ
โโโ reports/ # Generated analytics
โ โโโ analytics_output/ # Visualizations & summaries
โ
โโโ docs/ # Documentation
โ โโโ ANALYTICS_GUIDE.md # Analytics usage guide
โ โโโ CONTRIBUTING.md # Contribution guidelines
โ
โโโ assets/ # Project assets
โ โโโ demo.png # Demo screenshot
โ
โโโ videos/ # Video files
โโโ traffic.mp4 # Test video input
git clone https://github.com/your-username/Project-NETRA.git
cd Project-NETRApip install -r requirements.txtOr install individually:
pip install ultralytics opencv-python pandas matplotlib seaborn streamlit pillow supervisionPlace the model files in the models/ directory:
yolov8m.pt- General traffic detection (will auto-download on first run)best.pt- Your custom trained ambulance detection model
Place your test video in the videos/ folder and rename it to traffic.mp4 (or update the path in main.py).
Run the main application:
python3 main.pypython3 main.py --traffic-model models/yolov8n.pt --device auto --precision fp32 --imgsz 640 --skip-frames 1Useful runtime flags:
--traffic-model: choose a lighter model (models/yolov8n.pt) for edge devices.--device:auto,cpu,mps,cuda.--precision:fp16(CUDA only), otherwise auto-falls back to FP32.--imgsz: reduce from 640 to 512/416 for more FPS.--skip-frames: process every(N+1)th frame to improve throughput.
Export helper:
python3 src/export_model.py --model models/yolov8n.pt --format onnx --imgsz 640MacBook M2 (Apple Silicon):
python3 src/export_model.py --model models/yolov8n.pt --format coreml --imgsz 640Jetson / NVIDIA edge (TensorRT FP16):
python3 src/export_model.py --model models/yolov8n.pt --format engine --half --device cuda:0 --imgsz 640Jetson / NVIDIA edge (TensorRT INT8):
python3 src/export_model.py --model models/yolov8n.pt --format engine --int8 --device cuda:0 --imgsz 640 --data coco8.yamlNotes:
- INT8 requires representative calibration data (
--data) for stable accuracy. - If your platform does not support requested precision, the runtime keeps working with FP32.
- q: Quit the application.
The system will automatically generate a CSV file (e.g., Traffic_Data_20260131.csv) in the project folder.
Run the comprehensive traffic analytics dashboard to visualize and analyze your collected data:
python3 src/analytics_report.pyThe analytics module provides:
โ
Traffic Pattern Graphs: Visualize hourly and daily traffic trends
โ
Peak Hour Identification: Automatically detect high-traffic periods
โ
Lane Utilization Comparison: Analyze traffic distribution across lanes
โ
Ambulance Frequency Analytics: Track emergency vehicle patterns
โ
Average Wait Time Calculations: Calculate signal timing efficiency
โ
Correlation Heatmaps: Understand relationships between traffic variables
โ
Automated Reports: Generate PDF-ready summary reports
Output Files Generated:
Traffic_Analysis_YYYYMMDD_HHMMSS.png- 4-panel visualization dashboardCorrelation_Heatmap_YYYYMMDD_HHMMSS.png- Data correlation matrixTraffic_Summary_YYYYMMDD_HHMMSS.txt- Text-based statistics report
Launch the interactive web interface for real-time monitoring and analysis:
streamlit run src/web_dashboard.pyOr use the quick launcher:
bash start_dashboard.shWeb Dashboard Features:
๐ Home Page
- Real-time KPIs and metrics
- Lane utilization charts
- Recent activity overview
๐ Analytics Page
- Interactive traffic trends
- Correlation analysis
- Detailed statistics
- Historical reports
๐ Data Explorer
- Filter and search traffic data
- Export custom datasets
- Interactive data tables
โ๏ธ System Info
- Project structure
- Model status
- Quick reference commands
Access: Dashboard opens automatically at http://localhost:8501
Perfect for:
- Live demonstrations
- Project presentations
- Real-time monitoring
- Interactive data exploration
- Input Acquisition: Video frames are captured from CCTV/Video feed.
- Preprocessing: Frames are resized for the neural network.
- Object Detection:
- Parallel Execution: Frame is passed to both the Traffic Model and Ambulance Model.
- Multi-Object Tracking (ByteTrack):
- Detection results are fed into ByteTrack via the
supervisionlibrary. - Each vehicle receives a persistent Track ID that survives across frames.
- Position history is stored per track for speed estimation and trail rendering.
- Detection results are fed into ByteTrack via the
- Heuristic Filtering:
- Confidence Threshold > 0.15 (for background vehicles).
- Ambulance Aspect Ratio Check (< 2.0) to filter out buses.
- Decision Logic:
- Case A (Ambulance): Trigger Override โ Set Signal to GREEN.
- Case B (Normal): Count Vehicles โ Calculate Time โ Update Display.
- Output: Render Bounding Boxes with Track IDs, Speed Labels, Movement Trails, Timer Overlay, and write extended CSV.
The system logs traffic patterns every 5 seconds. This data can be used to generate reports on "Peak Traffic Hours."
| Timestamp | Lane1_Count | Lane2_Count | Lane1_Unique | Lane2_Unique | Avg_Speed_L1 | Avg_Speed_L2 | Ambulance_Detected | Green_Time_L1 | Green_Time_L2 |
|---|---|---|---|---|---|---|---|---|---|
| 10:45:05 | 12 | 4 | 28 | 15 | 45.2 | 38.7 | False | 29 | 13 |
| 10:45:10 | 14 | 3 | 34 | 16 | 52.1 | 41.3 | False | 33 | 11 |
| 10:45:15 | 8 | 0 | 38 | 16 | 30.5 | 0.0 | True | 21 | 5 |
NETRA now supports YAML-based configuration for easy deployment across different environments.
# Use default configuration
python3 main.py
# Use custom configuration file
python3 main.py --config config/my-deployment.yaml
# Override specific settings via CLI
python3 main.py --config config/default.yaml --device cuda --precision fp16 --imgsz 480Edit config/default.yaml to customize:
# Video input source
video:
input_path: "videos/traffic.mp4"
display_enabled: true
# Detection thresholds
detection:
vehicle_confidence: 0.15 # Lower = more detections
ambulance_confidence: 0.7 # Higher = fewer false positives
vehicle_classes:
- car
- truck
- bus
# Lane definitions (ROI boxes)
lanes:
lane1:
roi: [50, 100, 350, 500] # [x_min, y_min, x_max, y_max]
# Dynamic signal timing
signal_timing:
base_time: 5 # Base green duration (seconds)
multiplier: 2 # Additional seconds per vehicle
max_time: 60 # Maximum green duration
# Data logging
logging:
enabled: true
output_dir: "data/traffic_logs"
log_interval_seconds: 5Solution:
- Ensure model files exist:
ls models/yolov8m.pt models/best.pt - Download if missing:
python -c "from ultralytics import YOLO; YOLO('yolov8m.pt')" - Check PyTorch installation:
python -c "import torch; print(torch.__version__)"
Solution:
- Use lighter model:
--traffic-model models/yolov8n.pt - Reduce image size:
--imgsz 416(from 640) - Skip frames:
--skip-frames 2(process every 3rd frame) - Check device:
--device cuda(if GPU available) - Monitor CPU/GPU: Use
nvidia-smi(GPU) ortop(CPU)
Solution:
- Check file path:
ls -l videos/traffic.mp4 - Verify video format:
ffprobe videos/traffic.mp4 - Convert if needed:
ffmpeg -i video.mkv -c:v libx264 -preset medium traffic.mp4 - Use absolute path:
--video /absolute/path/to/video.mp4
Solution:
# Reinstall dependencies
pip install --upgrade -r requirements.txt
# Or specific package
pip install supervision>=0.18.0Solution:
- Check logging is enabled:
logging: enabled: truein config - Verify write permissions:
ls -ld data/traffic_logs/ - Create directory if missing:
mkdir -p data/traffic_logs - Check disk space:
df -h
Solution:
- Verify ROI boxes in config: Draw boxes on sample frame to confirm
- Adjust confidence thresholds: Lower
vehicle_confidencefor more detections - Use
src/utils/mouse_finder.pyto find correct ROI coordinates
Solution:
# Use MPS device explicitly
python3 main.py --device mps
# Or fallback to CPU if unstable
python3 main.py --device cpuSolution:
- Reduce batch size (if applicable)
- Use lighter model:
yolov8n.ptinstead ofyolov8m.pt - Reduce image size:
--imgsz 416 - Skip frames:
--skip-frames 1
Solution:
- Lower confidence threshold:
ambulance_confidence: 0.5(in config) - Verify model:
python src/utils/check_brain.py --model models/best.pt - Check video has ambulances in it
- Retrain if needed with proper labeled data
-
Choose Right Model
# YOLOv8n (Nano) - Fastest, ~10M parameters python3 main.py --traffic-model models/yolov8n.pt --imgsz 416 # YOLOv8m (Medium) - Balanced, ~25M parameters python3 main.py --traffic-model models/yolov8m.pt --imgsz 640
-
Use GPU Acceleration
# NVIDIA GPU (CUDA) python3 main.py --device cuda --precision fp16 --imgsz 640 # Apple Silicon (MPS) python3 main.py --device mps --precision fp32 --imgsz 480
-
Frame Skipping (Process every Nth frame)
# Process every 3rd frame (3x speedup, slight accuracy loss) python3 main.py --skip-frames 2 -
Reduce Input Resolution
# Smaller input = faster processing python3 main.py --imgsz 480 # Default is 640 python3 main.py --imgsz 416 # For edge devices
# Use larger model + higher resolution
python3 main.py --traffic-model models/yolov8m.pt --imgsz 640 --device cuda --precision fp16# Export to optimized format
python3 src/export_model.py --model models/yolov8n.pt --format tflite --imgsz 416
# Or TensorRT for NVIDIA edge
python3 src/export_model.py --model models/yolov8n.pt --format engine --half --device cuda:0 --imgsz 480Monitor performance with logging:
python3 main.py 2>&1 | grep -E "FPS|Time|Frames"Check resource usage:
# Linux/Mac
watch -n1 'nvidia-smi' # GPU (NVIDIA)
top -p $(pgrep -f "python main") # CPU
# macOS specific
vm_stat # Memory
power_log # Power usageUnit tests verify tracker accuracy:
# Run all tests
python3 -m pytest tests/ -v
# Run specific test
python3 -m pytest tests/test_tracker.py::TestTrackerSpeedEstimation -v
# Generate coverage report
python3 -m pytest tests/ --cov=src --cov-report=htmlTests cover:
- โ Speed estimation calculations
- โ Lane assignment logic
- โ Unique vehicle counting
- โ Movement trail history
- โ Error handling
- Traffic Flow Prediction: LSTM / Prophet time-series model to predict congestion 15โ30 minutes ahead using collected CSV data.
- License Plate Recognition (ANPR): Add OCR to detect number plates for red-light violation logging.
- Night Vision: Integrating CLAHE preprocessing or thermal imaging for low-light traffic detection.
- IoT Integration: Connecting the Python logic to Arduino/Raspberry Pi to control physical traffic lights.
- Multi-Intersection Coordination: Scale from 2-lane to full 4-way intersections with graph-based signal optimization.
This project is open-source and available under the MIT License. See the LICENSE file for details.
Contributions, issues, and feature requests are welcome! Please read our CONTRIBUTING.md guide to get started.
Faiz Ahmad Khan
โญ If you find this project useful, please consider giving it a star!
