This project demonstrates vehicle detection and counting in a video using OpenCV and background subtraction. It processes a video file, detects moving vehicles, and counts them as they cross a defined line.
- Detects moving vehicles in a video stream
- Counts vehicles as they cross a virtual line
- Uses background subtraction and contour detection
- Displays real-time video with bounding boxes and counters
- Python 3.x
- OpenCV (
opencv-python,opencv-contrib-python) - NumPy
- Clone this repository or download the source code.
- Install the required Python packages:
pip install opencv-python opencv-contrib-python numpy
- Place your input video file as
video.mp4in the project directory (or update the filename invehicle.py). - Run the script:
python vehicle.py
- The script will display the video with detected vehicles and a counter.
vehicle.py- Main script for vehicle detection and countingvideo.mp4- Input video file (replace with your own video if needed)README.md- Project documentation
- The script uses background subtraction to detect moving objects (vehicles).
- Contours are found and filtered by size to identify vehicles.
- When a vehicle's center crosses a predefined line, the counter is incremented.
- To use a different video, change the filename in
vehicle.py:cap = cv2.VideoCapture('your_video.mp4')
- Adjust
count_line_position,min_width_react, andmin_height_reactfor different video perspectives or vehicle sizes.
Vehicles are classified into three types based on their size:
- Small
- Medium
- Large
The script displays counters for each type in real time.
To use your computer's webcam instead of a video file, change the following line in vehicle.py:
cap = cv2.VideoCapture(0)To exit the video window, press the Enter key.
- If the video window does not appear or only shows a black screen, check that
video.mp4exists and is a valid video file. - For webcam issues, ensure your camera is connected and accessible.
- If you see errors or nothing happens, run the script from the terminal and check for error messages.
This project is for educational purposes.