-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_counting.py
38 lines (28 loc) · 953 Bytes
/
object_counting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('yolov8n.pt')
# Initialize the video capture object
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open video stream.")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
# Perform inference on the frame
results = model(frame)
# Count the number of detected objects
num_objects = len(results[0].boxes)
# Annotate the frame with the count
annotated_frame = results[0].plot()
cv2.putText(annotated_frame, f'Count: {num_objects}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Display the annotated frame
cv2.imshow('YOLOv8 Object Counting', annotated_frame)
# Exit on pressing 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()