This project uses the YOLO (You Only Look Once) object detection model to count vehicles in a video. The code processes each frame of the video to detect and track vehicles, providing a count of the detected objects within a defined region of interest (ROI).
- Detects and counts vehicles in a video using the YOLO model.
- Customizable region of interest (ROI) for counting objects.
- Visualizes object detection and tracking in real-time.
- Supports multiple object classes (e.g., vehicles, pedestrians).
- Outputs a processed video with counted objects and tracked trajectories.
- Python 3.7+
- OpenCV
- Ultralytics YOLO
- Clone this repository:
git clone https://github.com/your-username/vehicle-counting-yolo.git
- Navigate to the project directory:
cd vehicle-counting-yolo
- Install the required packages:
pip install -r requirements.txt
-
Place your video file in the
data/videos
directory. -
Modify the
video_path
variable in the code to point to your video file:cap = cv2.VideoCapture("data/videos/video.mp4")
-
Run the script:
python vehicle_counting.py
-
The processed video will be saved as
object_counting_output.avi
in the project directory.
- Loading the Model: The pre-trained YOLO model is loaded to detect objects in the video frames.
- Video Processing: The video is processed frame by frame to detect and track objects.
- Object Counting: Objects that pass through the defined region of interest (ROI) are counted.
- Output: The processed video with the counted objects and their tracked paths is saved and displayed.
- Region of Interest (ROI): Modify the
line_points
variable to change the area where objects are counted.line_points = [(170, 400), (1100, 400)] # Define your ROI points here
- Object Classes: You can specify which object classes to count using the
classes_to_count
variable.classes_to_count = [0, 2] # Modify the class IDs as needed
The processed video will display:
- Bounding boxes around detected objects.
- Trajectories of tracked objects.
- Count of objects crossing the defined ROI.