-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py.txt
85 lines (61 loc) · 2.87 KB
/
main.py.txt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import numpy as np
import cv2
from keras.preprocessing import image
import winsound
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
#-----------------------------
#opencv initialization
face_cascade = cv2.CascadeClassifier('C:\\Users\\User\\tensorflow-101\\model\\haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
ret, img = cap.read()
if ret:
t_minus = cv2.cvtColor(cap.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cap.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cap.read()[1], cv2.COLOR_RGB2GRAY)
#-----------------------------
#face expression recognizer initialization
from keras.models import model_from_json
model = model_from_json(open("C:\\Users\\User\\tensorflow-101\\model\\facial_expression_model_structure.json", "r").read())
model.load_weights('C:\\Users\\User\\tensorflow-101\\model\\facial_expression_model_weights.h5') #load weights
#-----------------------------
emotions = ('angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral')
while(cap.isOpened()):
if ret:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#print(faces) #locations of detected faces
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) #draw rectangle to main image
detected_face = img[int(y):int(y+h), int(x):int(x+w)] #crop detected face
detected_face = cv2.cvtColor(detected_face, cv2.COLOR_BGR2GRAY) #transform to gray scale
detected_face = cv2.resize(detected_face, (48, 48)) #resize to 48x48
img_pixels = image.img_to_array(detected_face)
img_pixels = np.expand_dims(img_pixels, axis = 0)
img_pixels /= 255 #pixels are in scale of [0, 255]. normalize all pixels in scale of [0, 1]
predictions = model.predict(img_pixels) #store probabilities of 7 expressions
#find max indexed array 0: angry, 1:disgust, 2:fear, 3:happy, 4:sad, 5:surprise, 6:neutral
max_index = np.argmax(predictions[0])
emotion = emotions[max_index]
#write emotion text above rectangle
cv2.putText(img, emotion, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
#process on detected face end
#-------------------------
if cv2.countNonZero(diffImg(t_minus, t, t_plus)) >200000 and (emotion=='sad'or emotion=='angry'or emotion=='fear'or emotion=='surprise') :
duration = 1000 # millisecond
freq = 500 # Hz
winsound.Beep(freq, duration)
# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cap.read()[1], cv2.COLOR_RGB2GRAY)
cv2.imshow('img',img)
ret, img = cap.read()
if cv2.waitKey(1) & 0xFF == ord('q'): #press q to quit
break
#kill open cv things
cap.release()
cv2.destroyAllWindows()