-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
135 lines (103 loc) · 4.52 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from flask import Flask, render_template, Response
import cv2
import numpy as np
from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing import image
#load model
model = model_from_json(open("fer.json", "r").read())
#load weights
model.load_weights('fer.h5')
face_haar_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
app = Flask(__name__)
emotion_embed = 'none'
def set_emotion_embed(value):
global emotion_embed
emotion_embed = value
def get_emotion_embed():
print(emotion_embed)
return emotion_embed
camera = cv2.VideoCapture(0)
faceDetectedQueue = []
emotionDetectedQueue = []
def gen_frames(): # generate frame by frame from camera
while True:
# Capture frame by frame
success, frame = camera.read()
if not success:
break
else:
gray_img= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces_detected = face_haar_cascade.detectMultiScale(gray_img, 1.32, 5)
entered = False
for (x,y,w,h) in faces_detected:
entered = True
print('WORKING')
cv2.rectangle(frame,(x,y),(x+w,y+h),(10, 207, 57),thickness=7)
roi_gray=gray_img[y:y+w,x:x+h] #cropping region of interest i.e. face area from image
roi_gray=cv2.resize(roi_gray,(48,48))
img_pixels = image.img_to_array(roi_gray)
img_pixels = np.expand_dims(img_pixels, axis = 0)
img_pixels /= 255
#print(img_pixels.shape)
predictions = model.predict(img_pixels)
#find max indexed array
max_index = np.argmax(predictions[0])
emotions = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
predicted_emotion = emotions[max_index]
print(predicted_emotion)
cv2.putText(frame, predicted_emotion, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 71, 17), 2)
cv2.putText(frame, 'JESUS FUCKING CHRIST', (int(x-20), int(y-20)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 71, 17), 3)
#EMOTION DETECTED QUEUE
emotionDetectedQueue.append(predicted_emotion)
if len(emotionDetectedQueue ) > 10:
emotionDetectedQueue.pop(0)
#EMOTION DETECTED QUEUE
#print("STACK ")
#print(emotionDetectedQueue)
for emotion_element in emotions:
if allEmotionsAre(emotionDetectedQueue, emotion_element):
print("All emotions are " + emotion_element)
set_emotion_embed(emotion_element)
#if (all(emotionDetectedQueue == "neutral")):
# print("All emotions are neutral")
#FACE DETECTED QUEUE
faceDetectedQueue.append(entered)
#faceDetectedQueue.pop()
if len(faceDetectedQueue) > 25:
faceDetectedQueue.pop(0)
nofacesdetected = True
for item in faceDetectedQueue:
if item == True:
nofacesdetected = False
if nofacesdetected:
print("We should play music")
value = "stop"
set_emotion_embed(value)
resized_img = cv2.resize(frame, (1000, 700))
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
@app.route('/video_feed')
def video_feed():
#Video streaming route. Put this in the src attribute of an img tag
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
def allEmotionsAre(array, var):
same = True
for item in array:
if item != var:
same = False
return same
@app.route('/scanned', methods=['GET'])
def scannedEmotion():
return render_template('index.html', embed=emotion_embed)
@app.route('/getMood', methods=['GET'])
def testfn():
message = emotion_embed
return message # serialize and use JSON headers
@app.route('/')
def index():
embed_start = "none"
return render_template('index.html', embed = embed_start)
if __name__ == '__main__':
app.run(debug=True)