https://github.com/techieworld2/car_speed_detector
Detects car speed using YOLOv8 and OpenCV, classifies vehicles as Passed or Failed, and saves annotated results.
https://github.com/techieworld2/car_speed_detector
object-detection opencv speed-estimation yolov8
Last synced: about 2 months ago
JSON representation
Detects car speed using YOLOv8 and OpenCV, classifies vehicles as Passed or Failed, and saves annotated results.
- Host: GitHub
- URL: https://github.com/techieworld2/car_speed_detector
- Owner: techieworld2
- Created: 2025-07-16T11:11:17.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-16T11:39:18.000Z (12 months ago)
- Last Synced: 2025-07-17T15:26:40.746Z (12 months ago)
- Topics: object-detection, opencv, speed-estimation, yolov8
- Language: Python
- Homepage:
- Size: 6.45 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Car Speed Detector using YOLOv8
A Python-based vehicle speed detection system that uses **YOLOv8 object detection** to track vehicles in a video, calculate their speed, and classify them as **Passed** or **Failed** based on a speed threshold.
---
## Demo Output
- Draws a detection line on video.
- Tracks vehicles and calculates their speed using distance between frames.
- Classifies each vehicle as:
- `Passed` (within speed limit)
- `Failed` (over speed limit)
- Annotates video with:
- Bounding boxes
- Speed labels
- Summary stats (Passed/Failed count)
- Saves cropped images of detected vehicles into:
- `passed/`
- `failed/`
---
## Tech Stack
- [Ultralytics YOLOv8](https://github.com/ultralytics/ultralytics)
- OpenCV
- Python 3.8+
---
## Project Structure
```bash
car_speed_detector/
│
├── input/ # Input videos (e.g., cars.mp4)
│ └── cars.mp4
│
├── output/ # Processed output video and results
│
├── passed/ # Cropped images of vehicles within speed limit
├── failed/ # Cropped images of vehicles over speed limit
│
├── yolov8n.pt # Pre-trained YOLOv8 model
│
├── config.py # Configuration settings
├── detector.py # Core logic for detection and speed check
├── utils.py # Utility functions (e.g., speed calculations)
└── main.py # Entry point for running the pipeline
```
---
## How It Works
1. Loads the video and detects cars frame-by-frame.
2. Tracks each vehicle using YOLOv8 object tracking.
3. Computes pixel displacement between frames.
4. Converts it into speed (km/h) using:
```python
speed = (pixel_distance / PIXELS_PER_METER) * FPS * 3.6
```
5. If speed > threshold → marked as "Failed", else "Passed".
6. Saves vehicle snapshot into `passed/` or `failed/` folders.
---
## How to Run
### 1. Install Requirements
```bash
pip install ultralytics opencv-python
```
### 2. Place Input Video
Put your `.mp4` file in the `input/` directory (e.g., `input/cars.mp4`).
### 3. Run the Detector
```bash
python main.py
```
### 4. View Output
- Annotated video: `output/output_speed_check.mp4`
- Image snapshots: `passed/`, `failed/`
---
## Configuration
Edit `config.py` to change:
- Speed limit (e.g., 30 or 40 km/h)
- Line position
- Distance calibration (PIXELS_PER_METER)
---
## Limitations
- Assumes a fixed camera angle.
- Speed is estimated using pixel displacement — **not GPS-accurate**.
- Requires calibration (e.g., pixels per meter) to match real-world scale.
---
## Credits
Built with:
- [Ultralytics YOLOv8](https://github.com/ultralytics/ultralytics)
- [OpenCV](https://opencv.org/)
---