This Python project detects and classifies vehicles (bike, car, truck) from a video feed using OpenCV. It applies a bird’s eye view transformation to better track and analyze vehicle motion within a defined Region of Interest (ROI).
🚗 Vehicle Detection with Bird’s Eye View Perspective
- Manual ROI selection via 4-point mouse click
- Perspective transformation to top-down view
- Background subtraction using KNN (with shadow detection disabled)
- Vehicle detection using contour analysis
- Vehicle classification into:
- Bike
- Car
- Truck
- Python 3.x
- OpenCV (
cv2) - NumPy
pip install opencv-python numpyClick 4 points on the first video frame in the order:
- Top-left
- Top-right
- Bottom-right
- Bottom-left
These define the area for the perspective transformation.
- A top-down bird’s eye view window with real-time vehicle detection
- Original video with the selected ROI highlighted
- Foreground mask showing motion-based areas
- Press
Escto exit the program
Vehicles are classified using bounding box area and aspect ratio:
if area > 50000:
return "truck"
elif area > 7000:
return "Car"
else:
return "bike"Adjust these thresholds in the classify_vehicle() function for better results depending on your video input.
- Add vehicle tracking with unique IDs (e.g., using SORT or Deep SORT)
- Improve classification using a trained machine learning or deep learning model
- Count vehicles crossing a virtual line or zone
- Export analytics (e.g., vehicle count per type, estimated speed, timestamps, etc.)
This project is open-source and licensed under the MIT License.