AI Object Counter
1. Introduction
The AI Object Counter is a project designed to count objects in a live or recorded video stream. It uses computer vision techniques and deep learning models such as YOLO (You Only Look Once) for object detection. The detected objects are then counted in real time and displayed on the video feed.
2. Prerequisites
• Python: Install Python 3.x from the official Python
website.
• Required Libraries:
- opencv-python: Install using pip
install opencv-python
- numpy: Install using pip install
numpy
- torch: Install using pip install
torch
- torchvision: Install using pip
install torchvision
- yolov5: Clone from GitHub or install
relevant weights
• Basic understanding of object detection and YOLO architecture.
3. Project Setup
1. Create a Project Directory:
- Name your project folder, e.g., `AI_Object_Counter`.
- Inside this folder, create the Python script file (`object_counter.py`).
2. Install Required Libraries:
Ensure OpenCV, torch, and torchvision are installed using `pip`.
4. Writing the Code
Below is an example code snippet for the AI Object Counter:
import cv2
import torch
from torchvision import transforms
# Load the YOLO model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Using YOLOv5 small model
# Function to count objects in a frame
def count_objects(frame):
results = model(frame)
detected_objects = results.pred[0]
count = len(detected_objects)
return count, results
# Main function for object counting in a video stream
def main():
video_capture =
cv2.VideoCapture(0) # Change to a video
file path for recorded streams
while True:
ret, frame = video_capture.read()
if not ret:
break
object_count, results =
count_objects(frame)
# Annotate frame with object
count
annotated_frame =
results.render()[0]
cv2.putText(annotated_frame,
f'Objects Count: {object_count}', (10, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('AI Object Counter',
annotated_frame)
if cv2.waitKey(1) & 0xFF ==
ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
5. Key Components
• Object Detection: YOLOv5 model detects objects in each
frame of the video stream.
• Object Counting: Counts the detected objects and overlays the count on the
video feed.
• Real-Time Processing: Processes live or pre-recorded video streams.
6. Testing
1. Ensure the YOLOv5 model is properly loaded in the script.
2. Run the script:
python object_counter.py
3. Verify the real-time object count displayed on the video feed.
7. Enhancements
• Multi-Class Counting: Display counts for specific object
categories (e.g., cars, people).
• Performance Optimization: Use hardware acceleration (GPU) for faster
processing.
• Region-Specific Counting: Count objects only in specific areas of the frame.
8. Troubleshooting
• Detection Issues: Ensure the YOLO model is downloaded and
weights are correctly loaded.
• Video Input Errors: Verify the video source and ensure the camera is
functional.
• Performance Lag: Use a smaller YOLO model or reduce input resolution.
9. Conclusion
The AI Object Counter project demonstrates the application of real-time object detection and counting. It has potential use cases in traffic monitoring, warehouse management, and surveillance systems.