🔢👁️ Real-Time Object Detection and Counting
In this project, I aimed to understand how to count objects in computer vision tasks. I decided to delve deeper into this topic as it has numerous applications and is crucial for counting things/objects in videos. The system I developed is able to detects objects and also counts them as they move through the video. Additionally, it saves screenshots of the detected objects along with their timestamps.
What is Object Counting?
Object counting is a computer vision technique that involves identifying and counting objects in images or video frames. This project uses YOLOv8 to detect objects and track their movement across lines to count them.
How Counting is Performed
The counting mechanism works by defining two lines (L1 and L2) in the video frame. The center point of the bounding box of each detected object is tracked. As this center point moves across these lines, the direction of movement is determined, and the object is counted based on the order in which the lines are crossed:
- Going Down:
- If the center point of an object’s bounding box crosses line L1 first and then crosses line L2, it is counted as moving “down”.
- Going Up:
- If the center point of an object’s bounding box crosses line L2 first and then crosses line L1, it is counted as moving “up”.
This approach ensures that each object is counted based on its movement direction, providing an accurate count for both directions.
The system also saves images of the detected objects in the directory specified, including the timestamp, to help with further analysis:
It is also applicable to other use cases, such as counting people (just change the value of CLASS_TO_DETECT
inside .env file):
The high-level sequence of the steps is:
- Object Detection:
- Utilize YOLO for detecting objects in each video frame.
- Object Tracking:
- Tracks the detected objects’ positions across frames to maintain consistent IDs.
- Counting:
- Counts objects as they cross predefined lines.
🏗️ Implementation Components
I used the following components:
- YOLOv8: A state-of-the-art object detection model.
- OpenCV: A library for real-time computer vision.
- Custom Object Tracker: A class implementing the tracking algorithm to maintain object IDs across frames.
🔗 GitHub Repository
Visit the project repository here for accessing the codebase (if you enjoyed this content, please consider leaving a star ⭐).
🛠️ Setup and Local Deployment
- Clone the repository and navigate to the project directory:
1 2
git clone https://github.com/enricollen/object-detection-counting cd object-detection-counting
- Create a virtual environment:
1 2
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
- Install the required libraries:
1
pip install -r requirements.txt
- Run the application:
1
python main.py
- View the results:
- The processed video frames will be displayed, and images of detected objects will be saved in the specified folder.
🚀 Future Improvements
Here are some ideas for future improvements:
- Improve tracking algorithm for better accuracy.
- Add support for multiple object classes to track simultaneously.
📹 Demo Video
Watch the demo video below to see the object detection and counting system in action: