-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeople_counter.py
More file actions
50 lines (40 loc) · 1.68 KB
/
people_counter.py
File metadata and controls
50 lines (40 loc) · 1.68 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
import subprocess
from datetime import datetime
import os
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator
from ultralytics.solutions import object_counter
# Function to take a picture using libcamera
def take_picture(folder_path):
os.makedirs(folder_path, exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{folder_path}/image_{timestamp}.jpg"
command = ['libcamera-still', '-o', filename, '--nopreview', '--timeout', '500']
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode == 0:
print(f"Picture taken successfully and saved as {filename}!")
return filename
else:
print("Failed to take picture:", result.stderr)
return None
def count_people(image_path):
# Count people in the image
model = YOLO('yolov8n.pt') # Load the pretrained YOLOv8 model
results = model(image_path)
names = results[0].names # same as model.names
# store number of objects detected per class label
class_detections_values = []
for k, v in names.items():
class_detections_values.append(results[0].boxes.cls.tolist().count(k))
# create dictionary of objects detected per class
classes_detected = dict(zip(names.values(), class_detections_values))
return classes_detected['person']
# Path where images will be saved
images_folder = 'captured_images'
# Take a picture
image_path = take_picture(images_folder)
#image_path = "classroom.jpg"
if image_path:
# Count people in the image
people_count = count_people(image_path)
print(f"Number of people in the image: {people_count}")