-
Notifications
You must be signed in to change notification settings - Fork 2
/
predictlocally.py
43 lines (34 loc) · 1 KB
/
predictlocally.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
39
40
41
42
43
import cv2
import numpy as np
from keras.models import load_model
# Set the size of the input images
img_size = (224, 224)
# Load the trained model
model = load_model('yawn_detection_model.h5')
# Open the camera stream
cap = cv2.VideoCapture(0)
# Loop through the frames from the camera
while True:
# Read a frame from the camera
ret, frame = cap.read()
if not ret:
break
# Preprocess the frame
img = cv2.resize(frame, img_size)
img = np.expand_dims(img, axis=0)
img = img / 255.0
# Predict whether the frame contains a yawn or not
prediction = model.predict(img)
if prediction[0] > 0.5:
label = 'yawn'
else:
label = 'not yawn'
# Draw the label on the frame
cv2.putText(frame, label, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Show the frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the camera and close all windows
cap.release()
cv2.destroyAllWindows()