-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
40 lines (31 loc) · 1.18 KB
/
test.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
import cv2
import numpy as np
from keras.models import load_model
def model():
model=load_model('model_file_30epochs.h5')
video=cv2.VideoCapture(0)
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
labels_dict={0:'Angry',1:'Disgust', 2:'Fear', 3:'Happy',4:'Neutral',5:'Sad',6:'Surprise'}
while True:
ret,frame=video.read()
gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces= faceDetect.detectMultiScale(gray, 1.3, 3)
for x,y,w,h in faces:
sub_face_img=gray[y:y+h, x:x+w]
resized=cv2.resize(sub_face_img,(48,48))
normalize=resized/255.0
reshaped=np.reshape(normalize, (1, 48, 48, 1))
result=model.predict(reshaped)
label=np.argmax(result, axis=1)[0]
print(label)
# print(result)
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255), 1)
cv2.rectangle(frame,(x,y),(x+w,y+h),(50,50,255),2)
cv2.rectangle(frame,(x,y-40),(x+w,y),(50,50,255),-1)
cv2.putText(frame, labels_dict[label], (x, y-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,255,255),2)
cv2.imshow("Frame",frame)
k=cv2.waitKey(5)
if k==ord('q'):
break
video.release()
cv2.destroyAllWindows()