-
Notifications
You must be signed in to change notification settings - Fork 0
/
facialRec.py
265 lines (229 loc) · 7.59 KB
/
facialRec.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#from SimpleCV import Camera
import plotly.plotly as py
import plotly.graph_objs as go
import base64
import time
import os
import math
import glob
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from moviepy.editor import *
from PIL import Image
GOOGLE_APPLICATION_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS.json"
class audience_data:
def __init__(self, time, results):
self.time = time
self.results = results
def set_up_credentials():
dir_path = os.path.dirname(os.path.realpath(__file__))
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = dir_path + '\\' + GOOGLE_APPLICATION_CREDENTIALS
py.sign_in("pauilsuk2", "qK9hwZQGUWGVM90RkNad")
def get_vision_service():
credentials = GoogleCredentials.get_application_default()
return discovery.build('vision', 'v1', credentials=credentials)
def detect_face(image, max_results=10):
"""Uses the Vision API to detect faces in the given file.
Args:
face_file: A file-like object containing an image with faces.
Returns:
An array of dicts with information about the faces in the picture.
"""
image_content = image.read()
batch_request = [{
'image': {
'content': base64.b64encode(image_content).decode('utf-8')
},
'features': [{
'type': 'FACE_DETECTION',
'maxResults': max_results,
}]
}]
service = get_vision_service()
request = service.images().annotate(body={
'requests': batch_request,
})
response = request.execute()
if not response['responses']:
return[]
elif not response['responses'][0]:
return []
elif not response['responses'][0]['faceAnnotations']:
return []
return response['responses'][0]['faceAnnotations']
def audience_response(faces):
num_emotions = {'joy': 0,
'sorrow': 0,
'anger': 0,
'surprise': 0,
'neutral': 0}
total = 0
if not faces:
return num_emotions
for face in faces:
emotion = False
if face['joyLikelihood'] == "LIKELY" or face['joyLikelihood'] == "VERY_LIKELY":
num_emotions['joy'] += 1
emotion = True
if face['sorrowLikelihood'] == "LIKELY" or face['sorrowLikelihood'] == "VERY_LIKELY":
num_emotions['sorrow'] += 1
emotion = True
if face['angerLikelihood'] == "LIKELY" or face['angerLikelihood'] == "VERY_LIKELY":
num_emotions['anger'] += 1
emotion = True
if face['surpriseLikelihood'] == "LIKELY" or face['surpriseLikelihood'] == "VERY_LIKELY":
num_emotions['surprise'] += 1
emotion = True
if not emotion:
num_emotions['neutral'] += 1
total += 1
per_emotions = {}
for emotion, value in num_emotions.iteritems():
per_emotions[emotion] = value
return per_emotions
def getVideoClipResults(fileName, framerate):
pres = VideoFileClip("video.mp4")
i = 0
clip= []
while i/framerate < math.ceil(pres.duration):
filename_temp = 'frames\\frame%d.jpg' % i
pres.save_frame(filename_temp, i/framerate)
i += 1
samples = make_img_list_jpg()
final_results = []
for i in range(len(samples)):
image = open(samples[i], 'rb')
data = getResultsFromSample(image, float(i/framerate))
final_results.append(data)
return final_results
def getResultsFromSample(sampleImage, timeStamp):
faces = detect_face(sampleImage)
results = audience_response(faces)
print(results)
data = audience_data(timeStamp, results)
return data
def make_img_list_jpg():
#Make list of image filenames
image_list = []
for filename in glob.glob('frames\*.jpg'):
image_list.append(filename)
return image_list
def output_linegraph( audience_data = [] ):
x = []
joy = []
anger = []
surprise = []
sorrow = []
neutral = []
for datum in audience_data:
x.append(datum.time)
joy.append(datum.results["joy"])
anger.append(datum.results["anger"])
surprise.append(datum.results["surprise"])
sorrow.append(datum.results["sorrow"])
neutral.append(datum.results["neutral"])
traceJoy = go.Scatter(
x=x,
y=joy,
mode='lines+markers',
line=dict(
color="rgb(244, 223, 66)",
width=4),
name='Joyful'
)
traceAnger = go.Scatter(
x = x,
y = anger,
mode='lines+markers',
line=dict(
color="rgb(244, 66, 66)",
width=4),
name='Angry'
)
traceSurprise = go.Scatter(
x=x,
y=surprise,
mode='lines+markers',
line=dict(
color="rgb(167, 48, 232)",
width=4
),
name='Surprised'
)
traceSorrow = go.Scatter(
x=x,
y=sorrow,
mode='lines+markers',
line=dict(
color="rgb(66, 128, 244)",
width=4
),
name='Sad'
)
traceNeutral = go.Scatter(
x=x,
y=neutral,
mode='lines+markers',
line = dict(
color="rgb(168, 164, 170)",
width=4),
name='Neutral'
)
data = [traceJoy, traceAnger, traceSurprise, traceSorrow, traceNeutral]
layout = go.Layout(width=2592, height=640)
fig = go.Figure(data=data,layout=layout)
py.image.save_as(fig, filename=('emotionTimeSeries.png'))
def output_piegraph(audience_data, i):
res = audience_data.results
labels = res.keys()
values = res.values()
trace = go.Pie(labels=labels, values=values,
marker=dict(
colors=["rgb(244, 66, 66)", "rgb(244, 223, 66)", "rgb(66, 128, 244)", "rgb(168, 164, 170)",
"rgb(167, 48, 232)"]),
textinfo="none", sort=False, showlegend = False)
layout = go.Layout(width=501, height=501)
fig = {'data': [trace], 'layout': layout}
py.image.save_as(fig, filename=('pi\\' + str(i)+'.png'))
def make_img_list():
#Make list of image filenames
image_list = []
for filename in glob.glob('pi/*.png'):
image_list.append(filename)
return image_list
def make_int_list(framerate):
#Filename contains time stamp of each frame in half seconds (2 frames / sec)
len_list = []
for filename in glob.glob('pi/*.png'):
len_list.append(0.5)
print(len_list)
return len_list
def make_gif(im, length, FPS):
#Make video with the appropriate lengths of each picture
full = []
for i in range(len(length)):
for j in range(int(length[i]*FPS)):
full.append(im[i])
print(len(full))
ImageSequenceClip(full, fps=FPS).write_videofile('images.mp4', fps=FPS)
def final_vid():
pres = VideoFileClip("video.mp4")
pi = VideoFileClip("images.mp4")
graph = ImageClip("emotionTimeSeries.png", duration=pres.duration)
pi = pi.resize((225,225))
graph = graph.resize((1280,250))
pres = pres.resize((1280,1080))
CompositeVideoClip([pres,graph.set_pos(("center", "bottom")),pi.set_pos((1000,600))], size=(1280,1080)).write_videofile('d1.mp4',fps=60,preset='ultrafast')
if __name__ == "__main__":
set_up_credentials()
framerate = 2
final = getVideoClipResults("video.mp4", framerate)
print('Paul won')
for i in range(len(final)):
output_piegraph(final[i], i)
print('done')
output_linegraph(final)
print('Connor won')
make_gif(make_img_list(), make_int_list(framerate), framerate)
final_vid()
print('Grace won')