Skip to content

Commit f428584

Browse files
authored
emotion detection script
1 parent 4126a7e commit f428584

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

cv_cam_facial_expression.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Mon Mar 18 23:26:06 2019
4+
5+
@authors: jaydeep thik , Vasudev Purandare
6+
"""
7+
8+
from tensorflow import keras
9+
import numpy as np
10+
import cv2
11+
import time
12+
13+
#emotion = ['Anger', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
14+
emotion = ['Confused', 'Happy', 'Stressed', 'Tran']
15+
16+
17+
font = cv2.FONT_HERSHEY_SIMPLEX
18+
face_cas = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
19+
20+
def cam_run():
21+
lab = [-1]*20
22+
cam = cv2.VideoCapture(0)
23+
model = keras.models.load_model("new_model_f1.h5")
24+
model._make_predict_function()
25+
start = time.time()
26+
27+
while True:
28+
29+
ret, frame = cam.read()
30+
31+
if ret==True:
32+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
33+
#gray = cv2.flip(gray,1)
34+
faces = face_cas.detectMultiScale(gray, 1.3,5)
35+
36+
for (x, y, w, h) in faces:
37+
face_component = gray[y:y+h, x:x+w]
38+
fc = cv2.resize(face_component, (48, 48))
39+
#cv2.imshow("fc", face_component)
40+
inp = np.reshape(fc,(1,48,48,1)).astype(np.float32)
41+
inp = inp/255.
42+
prediction = model.predict_proba(inp)
43+
44+
em = emotion[np.argmax(prediction)]
45+
46+
del[lab[0]]
47+
lab.append(em)
48+
#print()
49+
if (lab.count("Stressed") + lab.count("Confused")) >15: # change
50+
cam.release()
51+
cv2.destroyAllWindows()
52+
keras.backend.clear_session()
53+
end = time.time() - start
54+
return "confused", end
55+
56+
score = np.max(prediction)
57+
cv2.putText(frame, em+" "+str(score*100)+'%', (x, y), font, 1, (0, 255, 0), 2)
58+
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
59+
cv2.imshow("image", frame)
60+
61+
if cv2.waitKey(1) == 27:
62+
break
63+
else:
64+
print ('Error')
65+
66+
cam.release()
67+
cv2.destroyAllWindows()
68+
keras.backend.clear_session()
69+
70+
71+
if __name__=="__main__":
72+
cam_run()

0 commit comments

Comments
 (0)