-
Notifications
You must be signed in to change notification settings - Fork 48
/
model.py
65 lines (50 loc) · 1.76 KB
/
model.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import cv2
import numpy as np
from keras.preprocessing import image
import warnings
warnings.filterwarnings("ignore")
# rom keras.preprocessing.image import load_img, img_to_array
from keras.models import load_model
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import statistics as st
# load model
model = load_model("final_model.h5")
face_haar_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
i=0
GR_dict={0:(0,255,0),1:(0,0,255)}
model = tf.keras.models.load_model('final_model.h5')
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
output=[]
cap = cv2.VideoCapture(0)
while (i<=50):
ret, img = cap.read()
#gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(img,1.05,5)
for x,y,w,h in faces:
face_img = img[y:y+h,x:x+w]
resized = cv2.resize(face_img,(224,224))
reshaped=resized.reshape(1, 224,224,3)/255
predictions = model.predict(reshaped)
# find max indexed array
max_index = np.argmax(predictions[0])
emotions = ('angry', 'disgust', 'fear', 'happy', 'sad', 'neutral', 'surprise')
predicted_emotion = emotions[max_index]
output.append(predicted_emotion)
cv2.rectangle(img,(x,y),(x+w,y+h),GR_dict[1],2)
cv2.rectangle(img,(x,y-40),(x+w,y),GR_dict[1],-1)
cv2.putText(img, predicted_emotion, (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)
i = i+1
cv2.imshow('LIVE', img)
key = cv2.waitKey(1)
if key == 27:
cap.release()
cv2.destroyAllWindows()
break
print(output)
cap.release()
cv2.destroyAllWindows()
final_output = st.mode(output)
final_output