forked from curfewUnZipper/emotionDetectorPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
34 lines (25 loc) · 879 Bytes
/
camera.py
File metadata and controls
34 lines (25 loc) · 879 Bytes
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
import cv2
import numpy as np
import tensorflow as tf
# Load model
model = tf.keras.models.load_model("model.h5")
model.load_weights("modelWeights.weights.h5")
label_dict = {0:'Angry',1:'Disgust',2:'Fear',3:'Happy',4:'Neutral',5:'Sad',6:'Surprise'}
# Start webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = cv2.resize(gray, (48,48))
face_input = np.expand_dims(face, axis=0).reshape(1,48,48,1) / 255.0
prediction = model.predict(face_input)
emotion_index = np.argmax(prediction[0])
emotion = label_dict[emotion_index]
cv2.putText(frame, emotion, (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
cv2.imshow("Emotion Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()