-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
59 lines (40 loc) · 1.65 KB
/
app.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
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.preprocessing import image
import cv2
import numpy as np
class_labels=['Angry','Disgusted','Afraid','Happy','Neutral','Sad','Surprised']
face_classifier=cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
classifier = load_model('EmotionModel.h5')
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
ret, frame = cap.read()
labels=[]
if ret == False:
print("Can't receive frame (stream end?). Exiting ...")
break
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=face_classifier.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,250,0),2)
cv2.rectangle(frame, (x,y-40), (x+w, y), (0,200,0),-1)
roi_gray=gray[y:y+h,x:x+w]
roi_gray=cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)
if np.sum([roi_gray]) !=0:
roi=roi_gray.astype('float')/255.0
roi=img_to_array(roi)
roi=np.expand_dims(roi,axis=0)
preds=classifier.predict(roi)[0]
label=class_labels[preds.argmax()]
label_position=(x,y)
cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,255,255),2)
else:
cv2.putText(frame,'No face was found',(200,200),cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),2)
cv2.imshow('Emotion Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()