-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
242 lines (215 loc) · 5.85 KB
/
app.js
File metadata and controls
242 lines (215 loc) · 5.85 KB
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
const express = require('express'),
app = express(),
logger = require('morgan'),
errorHandler = require('errorhandler'),
debug = require('debug')('server'),
socketio = require('socket.io'),
http = require('http'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
utils = require('./app/src/utils'),
consts = require('./consts'),
axios = require('axios'),
BASE_PATH = `${__dirname}/app`,
ENV = process.env.NODE_ENV || 'development',
DEFAULT_PORT = 3001,
SOCKET_PORT = 8000,
EMOTION_FPS = 5; // return the analysed data after n frames
/* Configuration */
app.set('views', `${BASE_PATH}/views`)
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use('/assets', express.static(`${BASE_PATH}/public`))
if (ENV === 'development') {
console.log('DEVELOPMENT env')
app.use(errorHandler({dumpExceptions: true, showStack: true}))
app.use(logger('dev'))
} else {
console.log('PRODUCTION env')
app.use(errorHandler())
app.use(logger())
}
// app.locals = {
// accEmotions: {
// anger: 0,
// contempt: 0,
// disgust: 0,
// fear: 0,
// happiness: 0,
// neutral: 0,
// sadness: 0,
// surprise: 0
// },
// lecturer: {
// overall: {
// emotion: '',
// emotionData: []
// },
// lectures: [
// {
// date: '',
// emotion: '',
// emotionData: [],
// series: [
// {
// time: '',
// emotion: '',
// emotionData: []
// }
// ]
// }
// ]
// }
// }
app.locals = {
accEmotions: {
anger: 0,
contempt: 0,
disgust: 0,
fear: 0,
happiness: 0,
neutral: 0,
sadness: 0,
surprise: 0
},
lecturer: {
overall: {
emotion: ''
},
lecture: {
accFrames: EMOTION_FPS,
date: '',
emotion: '',
timeline: [],
count: 0,
avgcount: 0
}
}
}
/**
* Get port from environment and use it for Express.
*/
const PORT = utils.normalizePort(process.env.PORT || DEFAULT_PORT)
app.set('port', PORT)
/**
* Create HTTP server.
*/
const server = http.createServer(app)
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(PORT)
/**
* Server event handling
*/
server.on('error', (err) => {
throw err
})
server.on('listening', (err) => {
let addr = server.address()
let bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port
debug('DeepEval is alive on ' + bind)
})
/**
* Init websockets
*/
const io = socketio(server);
io.listen(SOCKET_PORT);
console.log('Listening on SOCKET_PORT ', SOCKET_PORT);
io.on('connection', (client) => {
client.on('imagePost', (imgData) => {
// console.log('timestamp:', imgData.timestamp)
axios({
method: 'post',
url: consts.endpoints.faceAPI,
data: imgData.uri,
headers: {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': process.env.SUB_KEY
},
params: {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'headPose,emotion,blur,exposure,noise',
},
})
.then(function (response) {
let faceData = response.data
if (faceData.length <= 0) {
// console.log(faceData.length)
return;
}
// Only include attentive faces
// let attentiveFaces = faceData.filter((item) => {
// return parseInt(item['headPose']['pitch']) == 0;
// });
let attentiveFaces = faceData
app.locals.lecturer.lecture.count++;
// console.log(attentiveFaces)
// console.log(app.locals.lecturer.lecture.count)
let maxEmotionMapping = {
anger: 1,
sadness: 2,
disgust: 3,
fear: 4,
neutral: 5,
contempt: 6,
surprise: 7,
happiness: 8,
}
let maximumEmotion = 0
let sum = {
anger: 0,
contempt: 0,
disgust: 0,
fear: 0,
happiness: 0,
neutral: 0,
sadness: 0,
surprise: 0,
}
let max = 0
function avg(count, x1, x2) {
return ((count-1)*x1+x2)/2
}
attentiveFaces.forEach(elem => {
for (let prop in elem['faceAttributes']['emotion']) {
app.locals.accEmotions[prop] = avg(app.locals.lecturer.lecture.count, app.locals.accEmotions[prop], elem['faceAttributes']['emotion'][prop])
// console.log(prop)
sum[prop] = sum[prop] + elem['faceAttributes']['emotion'][prop]
if(elem['faceAttributes']['emotion'][prop] > max) {
max = elem['faceAttributes']['emotion'][prop]
maximumEmotion = maxEmotionMapping[prop]
}
}
});
console.log(require('util').inspect(plotData, { depth: null }));
if (--app.locals.lecturer.lecture.accFrames < 0) {
let plotData = {
emotion: maximumEmotion,
timestamp: imgData.timestamp
}
app.locals.lecturer.lecture.timeline.append(plotData)
app.locals.lecturer.lecture.avgcount++;
app.locals.lecturer.lecture.emotion = app.locals.lecturer.overall.emotion = avg(app.locals.lecturer.lecture.avgcount, app.locals.lecturer.lecture.emotion, maximumEmotion)
// Reset the accumlated frames
app.locals.lecturer.lecture.accFrames = EMOTION_FPS
// send back off to client
client.emit('results', plotData)
}
})
.catch(function (error) {
// console.error(error);
});
});
});
// const websocket = socketio(server)
app.get('/', function (req, res) {
res.render('index')
})
module.exports = app