Traffic Analysis using YOLO (ONNX) + ByteTrack
This example demonstrates how to perform traffic analysis from video streams using computer vision.
The system detects and tracks vehicles in a video and extracts useful traffic information such as vehicle counts, traffic flow, and movement patterns.
Traffic analysis systems are commonly used in smart city infrastructure, highway monitoring, and urban planning.
This project is ideal for:
- 🚗 Traffic monitoring systems
- 🚦 Smart traffic signal optimization
- 🏙 Smart city analytics
- 🛣 Highway traffic flow analysis
- 📊 Vehicle movement analytics
🚀 What This Project Does
The system performs the following steps:
- Detects vehicles using a YOLO ONNX model
- Tracks vehicles across frames using ByteTrack
- Assigns unique IDs to each detected vehicle
- Monitors vehicle movement across the video
- Analyzes traffic flow patterns
- Displays tracking information on the video
- Saves the annotated output video
Each tracked vehicle is labeled as:
#ID
#7
This means the detected vehicle has tracking ID 7, which remains consistent while the vehicle is visible in the scene.
📥 Model Download
Pretrained models for swatahVision are available in the Model Zoo.
🔗 https://visionai4bharat.github.io/swatahVision/model_zoo/
📁 Project Structure
traffic_analysis/
├── yolov11x-1280_onnx.py # Main script for detection and traffic analysis
├── models/
│ └── yolov11x-1280.onnx # YOLO ONNX detection model
├── data/
│ └── traffic.mp4 # Example traffic video
└── README.md # Example documentation
📁 Project Setup
1️⃣ Clone the Repository
git clone https://github.com/VisionAI4Bharat/swatahVision.git
cd swatahVision/examples/traffic_analysis
2️⃣ (Optional) Create Virtual Environment
python3 -m venv venv
source venv/bin/activate
3️⃣ Install Dependencies
pip install -r requirements.txt
▶️ Run Traffic Analysis
Run the detection and tracking script:
python yolov11x-1280_onnx.py \
--source_video_path "data/traffic.mp4" \
--source_weights_path "models/yolov11x-1280.onnx" \
--classes 2 5 7 \
--confidence_threshold 0.3 \
--iou_threshold 0.7
⚙️ Command Line Arguments
Required
--source_video_path
Path to input video file.
Optional
--source_weights_path
Path to YOLO ONNX model file.
--classes
List of class IDs to detect and track.
Example:
--classes 2 5 7
If empty, all detected classes are tracked.
--confidence_threshold
Detection confidence threshold.
Default: 0.3
--iou_threshold
IoU threshold for Non-Max Suppression.
Default: 0.7
🧠 How It Works (Technical Overview)
Processing Pipeline:
- Load YOLO ONNX object detection model
- Read frames from the input video
- Perform object detection on each frame
- Filter detections by selected vehicle classes
- Apply ByteTrack multi-object tracking
- Assign unique IDs to each vehicle
- Track vehicle movements across frames
- Analyze traffic flow and movement patterns
- Draw bounding boxes and tracking IDs on frames
- Save the annotated output video
Modern traffic analysis systems use object detection and tracking algorithms to monitor vehicle movement and traffic patterns in real-time video streams.
🚗 Traffic Metrics
Using object detection and tracking, the system can extract various traffic insights such as:
- Vehicle count in the scene
- Vehicle movement patterns
- Traffic density estimation
- Vehicle tracking trajectories
These metrics help analyze traffic congestion and flow patterns in monitored areas.
📊 Output
The generated output video contains:
- Bounding boxes around detected vehicles
- Unique tracking IDs
- Continuous vehicle tracking across frames
- Annotated video saved to disk
Example overlay:
#1
#3
#8
🖥 Model Requirements
This example requires a YOLO ONNX object detection model.
You can:
- Export from Ultralytics YOLO
- Use an existing ONNX detection model
- Use your own trained detection model
Example export:
pip install ultralytics
yolo export model=yolov8x.pt format=onnx imgsz=1280
🎯 Practical Applications
- Smart traffic management systems
- Urban traffic monitoring
- Vehicle movement analysis
- Traffic congestion monitoring
- Intelligent transportation systems
📌 Notes
- GPU recommended for real-time performance
- Works best with fixed surveillance cameras
- Supports tracking multiple vehicles simultaneously
- Detection accuracy affects tracking quality
- Suitable for highway and urban traffic monitoring