-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
59 lines (45 loc) · 1.52 KB
/
Copy pathpredict.py
File metadata and controls
59 lines (45 loc) · 1.52 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
51
52
53
54
55
56
57
58
59
import os
from ultralytics import YOLO
import cv2
# Load the trained model
model_path = os.path.join('.', 'runs', 'detect', 'train3', 'weights', 'last.pt')
model = YOLO(model_path)
# Set detection threshold
threshold = 0.3
# Open camera
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
while True:
# Read a frame from the camera
ret, frame = cap.read()
if not ret:
print("Failed to grab frame")
break
# Run inference
results = model(frame)[0]
# Process detections
for result in results.boxes.data.tolist():
x1, y1, x2, y2, score, class_id = result
# Check if detected class is a person and above threshold
if model.names[int(class_id)] == 'person' and score > threshold:
# Draw bounding box
cv2.rectangle(frame,
(int(x1), int(y1)),
(int(x2), int(y2)),
(0, 255, 0), 4)
# Add label
label = f'Person: {score:.2f}'
cv2.putText(frame, label,
(int(x1), int(y1 - 10)),
cv2.FONT_HERSHEY_SIMPLEX,
0.9, (0, 255, 0), 3,
cv2.LINE_AA)
# Display the frame
cv2.imshow('Person Detection', frame)
# Break loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()