This project implements a real-time vehicle speed estimation system using computer vision techniques, combining YOLOv8 object detection with perspective transformation for accurate speed measurements. The system processes traffic footage to detect vehicles within a defined region of interest (ROI), tracks their movement across frames, and calculates their speeds using geometric principles.
The project addresses the need for automated traffic monitoring and speed enforcement in applications like intelligent transportation systems, traffic flow analysis, speed violation detection, and road safety monitoring. By leveraging state-of-the-art deep learning models and geometric transformations, the system provides accurate speed measurements without requiring specialized hardware.
The implementation demonstrates practical applications of computer vision for traffic analysis, processing video streams to track multiple vehicles simultaneously and calculate their individual speeds in real-world units (km/h).
Core Features:
The system employs YOLOv8 for vehicle detection combined with ByteTrack for multi-object tracking and perspective transformation for accurate distance-to-speed conversion. The approach uses a defined region of interest (ROI) to focus measurements on a specific road section with known dimensions.
The vehicle speed estimation pipeline consists of:
The implementation uses the Ultralytics framework for YOLOv8 inference and the Supervision library for tracking and annotation. A trapezoidal ROI is defined to match the road boundaries, with known real-world dimensions (12m width × 50m length) used for calibration. Detection smoothing and coordinate buffering reduce noise and improve measurement accuracy. The system processes video frame-by-frame, maintaining a history of vehicle positions to calculate instantaneous speeds.
The system uses perspective transformation to map image coordinates to real-world coordinates:
\[\mathbf{P}_{\text{world}} = \mathbf{M} \cdot \mathbf{P}_{\text{image}}\]
where \(\mathbf{M}\) is the \(3 \times 3\) perspective transformation matrix calculated using cv2.getPerspectiveTransform() from ROI coordinates to target dimensions.
Given source points \(\mathbf{S}\) (ROI coordinates) and destination points \(\mathbf{D}\) (target rectangle):
\[\mathbf{M} = \text{getPerspectiveTransform}(\mathbf{S}, \mathbf{D})\]
Where:
Vehicle speed is calculated using the distance traveled between consecutive frames:
\[v = \frac{d}{t} \times 3.6\]
where:
To reduce noise, speeds are calculated from a coordinate history buffer:
\[v_{\text{avg}} = \frac{1}{n-1} \sum_{i=1}^{n-1} \frac{d_i}{\Delta t} \times 3.6\]
where:
requirements.txt
opencv-python>=4.8.0
numpy>=1.24.0
ultralytics>=8.0.0
supervision>=0.16.0
# Clone the repository
git clone https://github.com/kemalkilicaslan/Vehicle-Speed-Estimation-System.git
cd Vehicle-Speed-Estimation-System
# Install required packages
pip install -r requirements.txt
Vehicle-Speed-Estimation-System
├── Vehicle-Speed-Estimation.py
├── README.md
├── requirements.txt
└── LICENSE
ROI Configuration:
# Region of Interest (trapezoidal polygon)
ROI_COORDINATES = np.array([
[750, 350], # Top-left
[1150, 350], # Top-right
[1870, 1079], # Bottom-right
[50, 1079] # Bottom-left
])
# Real-world dimensions
TARGET_WIDTH = 12 # meters (road width)
TARGET_HEIGHT = 50 # meters (measurement distance)
Detection Parameters:
# Vehicle classes to detect
TARGET_CLASSES = [2, 3, 5, 7] # Car, Motorcycle, Bus, Truck
# Detection thresholds
conf = 0.5 # Confidence threshold
iou = 0.7 # NMS (Non-Maximum Suppression) threshold
Visualization Parameters:
# Trace settings
trace_length = 10 # frames
# Label settings
text_scale = 0.8
text_thickness = 2
text_color = sv.Color.GREEN # (0, 150, 0) in BGR
python Vehicle-Speed-Estimation.py
Requirements:
Vehicle-Flow.mp4 (place in project directory)yolov8x.pt (automatically downloaded on first run)Controls:
q to quit during playbackVehicle-Speed-Estimation.mp4Modify the video path in the script:
# Change this line in Vehicle-Speed-Estimation.py
video_path = "Vehicle-Flow.mp4"
To calibrate for a new video:
ROI_COORDINATES = np.array([
[x1, y1], # Top-left corner
[x2, y2], # Top-right corner
[x3, y3], # Bottom-right corner
[x4, y4] # Bottom-left corner
])
TARGET_WIDTH = measured_width # meters
TARGET_HEIGHT = measured_length # meters
For different traffic conditions:
# Increase sensitivity (more detections)
conf = 0.3
# Decrease sensitivity (fewer false positives)
conf = 0.6
# Adjust NMS for overlapping vehicles
iou = 0.5 # More aggressive filtering
iou = 0.8 # Less filtering
Vehicle Flow:
Vehicle Speed Estimation:
Region of Interest Area:
The trapezoidal polygon defines the measurement zone on the road.
Real Target Length:
Physical dimensions: 12m width × 50m length for accurate calibration.
Region of Interest Coordinates:
Pixel coordinates of the ROI corners mapped to real-world measurements.
| Metric | Value | Notes |
|---|---|---|
| Detection Accuracy | 90-95% | Varies with video quality and lighting |
| Speed Accuracy | ±5 km/h | Depends on calibration precision |
| Processing Speed | 15-30 FPS (CPU) | GPU acceleration available |
| Multi-vehicle Tracking | Up to 50 simultaneous | ByteTrack algorithm |
| Measurement Range | 0-150 km/h | Configurable based on ROI size |
| Parameter | Value | Unit | Description |
|---|---|---|---|
| Target Width | 12 | meters | Real-world road width |
| Target Height | 50 | meters | Measurement distance |
| Detection Confidence | 0.5 | - | Vehicle detection threshold |
| NMS Threshold | 0.7 | - | Non-maximum suppression |
| Trace Length | 10 | frames | Path visualization duration |
| Coordinate Buffer | fps | frames | Speed calculation history |
| Label Text Scale | 0.8 | - | Speed label size |
| Label Text Thickness | 2 | pixels | Speed label boldness |
| Trace Thickness | 2 | pixels | Path line thickness |
| Label Position | TOP_CENTER | - | Speed display location |
| Annotation Color | GREEN | (0, 150, 0) | BGR format |
[Input Video Frame]
↓
[YOLOv8 Vehicle Detection]
├── Car (class 2)
├── Motorcycle (class 3)
├── Bus (class 5)
└── Truck (class 7)
↓
[Confidence Filtering (≥0.5)]
↓
[NMS Filtering (IoU ≤0.7)]
↓
[ROI Polygon Zone Check]
↓ (vehicles inside ROI only)
[ByteTrack Multi-Object Tracking]
├── Assign unique IDs
└── Maintain identity across frames
↓
[Detection Smoothing]
↓
[Coordinate Transformation]
├── Extract vehicle center point (pixel)
├── Apply perspective matrix M
└── Convert to real-world position (meters)
↓
[Coordinate Buffer Update]
├── Store position history (maxlen = fps)
└── Maintain temporal sequence
↓
[Speed Calculation]
├── Calculate distances between positions
├── Divide by time intervals (1/fps)
├── Convert m/s to km/h (×3.6)
└── Average over buffer window
↓
[Visualization Overlay]
├── Draw vehicle traces (green, 10 frames)
├── Add speed labels (green, top-center)
└── Annotate polygon zone boundary
↓
[Output Video Frame]
└── Save to Vehicle-Speed-Estimation.mp4
| Library | Version | Purpose |
|---|---|---|
| opencv-python | 4.8+ | Video I/O, geometric transformations, rendering |
| numpy | 1.24+ | Array operations, coordinate calculations |
| ultralytics | 8.0+ | YOLOv8 model inference and detection |
| supervision | 0.16+ | Tracking, smoothing, and annotation tools |
YOLOv8x (Extra Large):
Tracking Algorithm:
Supervision Library Tools:
| Component | Type | Purpose |
|---|---|---|
| ByteTrack | Tracker | Multi-vehicle identity management |
| DetectionsSmoother | Filter | Reduce detection jitter/noise |
| PolygonZone | ROI Filter | Spatial filtering for speed measurement |
| TraceAnnotator | Visualizer | Draw vehicle movement paths |
| LabelAnnotator | Visualizer | Display speed measurements |
OpenCV Functions:
cv2.getPerspectiveTransform(): Calculate transformation matrixcv2.perspectiveTransform(): Apply transformation to coordinatesThis project is open source and available under the Apache License 2.0.
This project utilizes YOLOv8 from Ultralytics for vehicle detection and the Supervision library for tracking and annotation. Special thanks to the computer vision community for providing excellent open-source tools for traffic analysis applications.
Note: This system is intended for research, education, and traffic analysis purposes. For legal speed enforcement applications, ensure compliance with local regulations and calibrate the system according to official standards. Speed measurements may vary based on camera angle, calibration accuracy, and environmental conditions.