Skip to content

"Detect and count vehicles in videos using OpenCV for accurate analysis in traffic management and monitoring systems."

Notifications You must be signed in to change notification settings

HarshitWaldia/TrafficTally

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš— TrafficTally : Vehicle Detection and Counting Notebook πŸš™

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.

Features

  • 🚘 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.

Requirements

  • 🐍 Python 3.x
  • πŸ–ΌοΈ OpenCV
  • πŸ”’ NumPy
  • πŸ““ Jupyter

Installattion

Setting Up the Environment

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.

  1. Install Miniconda or Anaconda if you haven't already.

  2. Create a new conda environment:

    conda create --name vehicle_counter python=3.8
  3. Activate the environment:

    conda activate vehicle_counter
  4. Install the required packages:

    pip install -r requirements.txt

Usage

To run the vehicle detection and counting notebook, follow these steps:

  1. Ensure you have a video file named video3.mp4 in the same directory as your notebook, or modify the code to use a different video file.

  2. Launch Jupyter Notebook:

jupyter notebook
  1. Open the main.ipynb notebook and run all cells to start the vehicle detection and counting process.

  2. Run all the cells in the notebook to start the vehicle detection and counting process.

Explanation of Key Components

  • πŸ” 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.

Customization

  • 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.

Code Explanation

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

Contributions are welcome! If you have any suggestions or improvements, please create a pull request or open an issue on GitHub.

About

"Detect and count vehicles in videos using OpenCV for accurate analysis in traffic management and monitoring systems."

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published