This project is a Jupyter notebook application designed to detect and count vehicles in a video stream. It uses OpenCV for image processing and a background subtractor algorithm to identify moving vehicles. The application draws bounding boxes around detected vehicles and counts them as they cross a predefined line in the video frame.
- π Vehicle Detection: Identifies and draws bounding boxes around moving vehicles.
- π Vehicle Counting: Counts vehicles as they cross a specified line in the video frame.
- β±οΈ Real-Time Processing: Processes video frames in real-time for live counting.
- π Python 3.x
- πΌοΈ OpenCV
- π’ NumPy
- π Jupyter
It's recommended to use a virtual environment to manage the dependencies for this project. Follow the steps below to set up the environment using conda.
-
Create a new conda environment:
conda create --name vehicle_counter python=3.8
-
Activate the environment:
conda activate vehicle_counter
-
Install the required packages:
pip install -r requirements.txt
To run the vehicle detection and counting notebook, follow these steps:
-
Ensure you have a video file named
video3.mp4in the same directory as your notebook, or modify the code to use a different video file. -
Launch Jupyter Notebook:
jupyter notebook-
Open the
main.ipynbnotebook and run all cells to start the vehicle detection and counting process. -
Run all the cells in the notebook to start the vehicle detection and counting process.
- π Background Subtraction: Uses cv2.bgsegm.createBackgroundSubtractorMOG() to segment moving objects (vehicles) from the background.
- βοΈ Contour Detection: Detects contours in the segmented frames and draws bounding boxes around the detected vehicles.
- π’ Vehicle Counting: Defines a counting line and increments the vehicle count whenever a detected vehicle crosses this line.
- π Center Function: Calculates the center of the bounding box for each detected vehicle.
-
Change Count Line Position: Modify the count_line_position variable to change the position of the counting line.
-
Adjust Rectangle Size: Modify the min_width_rectangle and min_height_rectangle variables to adjust the minimum size of detected vehicles.
-
Use Different Video Source: Change the path in cv2.VideoCapture("video3.mp4") to use a different video file or a webcam.
The script performs the following steps:
- Imports necessary libraries and initializes video capture.
- Sets parameters for vehicle detection and counting.
- Defines a background subtraction algorithm and contour detection.
- Implements real-time processing to count vehicles crossing a designated line.
Contributions are welcome! If you have any suggestions or improvements, please create a pull request or open an issue on GitHub.