Objective: Develop an AI system that can detect and count specific objects (e.g., cars, people, plants) from images or video footage.
Problem Statement
In many real-world applications, it is essential to monitor and analyze the presence of specific objects, such as vehicles, people, or equipment, within a given environment. Traditional methods of object detection often involve manual counting or rudimentary video surveillance, which can be time-consuming and prone to human error. This project aims to develop an AI-based object detection and counting system that uses computer vision and machine learning to detect, track, and count specific objects in real-time from video feeds. The solution will enhance the efficiency and accuracy of tasks such as traffic monitoring, event management, and inventory control, providing automated and reliable data collection.
Key Tools:
Features:
Python Code:
import cv2import torch
# Load the YOLO model (pretrained)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
# Initialize webcam
cap = cv2.VideoCapture(0)
# Object classes to track (example: person, car, etc.)
tracking_classes = ['person', 'car']
while True:
ret, frame = cap.read()
if not ret:
break
# Make predictions
results = model(frame)
# Parse the predictions
objects_detected = []
for obj in results.pred[0]:
if obj[5] in model.names: # Only track objects within known classes
obj_class = model.names[int(obj[5])]
if obj_class in tracking_classes:
x1, y1, x2, y2, conf, cls = obj
objects_detected.append(obj_class)
# Draw bounding box
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(frame, f'{obj_class} {conf:.2f}', (int(x1), int(y1) - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Display the detected object count
for obj_class in set(objects_detected):
print(f"{obj_class}: {objects_detected.count(obj_class)} detected")
# Show the frame
cv2.imshow('Object Detection', frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()