forked from udacity/AIND-CV-Mimic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffdex.js
418 lines (367 loc) · 11.8 KB
/
affdex.js
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"use strict";
//Global namespace
var affdex = affdex || {};
affdex.version="3.2.583-b86b1d2"
affdex.getAffdexDotJsLocation = function() {
var scripts = document.getElementsByTagName('script');
var affdexJS = null;
for (var i = 0; i < scripts.length ; i++) {
if (scripts[i].src.match(/\/affdex\.js$/)) {
affdexJS = scripts[i].src.replace(/\/affdex\.js$/, '/');
}
}
return affdexJS;
};
affdex.FaceDetectorMode = {
LARGE_FACES: 0,
SMALL_FACES: 1
};
// Load worker from XHR url : src=https://gist.github.com/willywongi/5780151
function XHRWorker(url, ready, scope) {
/* This loads the source of the worker through a XHR call. This is possible since the server
from which we pull the worker source serves files with CORS (Access-Control-Allow-Origin: *).
From the source (responseText) we build an inline worker.
This works but we need to delegate using the worker when the resource is loaded (XHR call finishes)
*/
var oReq = new XMLHttpRequest();
oReq.addEventListener('load', function() {
var worker = new Worker(window.URL.createObjectURL(new Blob([this.responseText])));
if (ready) {
ready.call(scope, worker);
}
}, oReq);
oReq.open("get", url, true);
oReq.setRequestHeader('Access-Control-Allow-Origin', '*');
oReq.setRequestHeader('Access-Control-Allow-Headers', '*');
oReq.send();
}
affdex.Detector = function() {
var self = this;
//Public variables
self.processFPS = 30;
self.worker = null;
self.staticMode = false;
self.detectEmojis = false;
self.faceDetectorMode = affdex.FaceDetectorMode.LARGE_FACES;
self.isWorkerInitialized = false;
self.isRunning = false;
self.callbacks = {};
self.detectExpressions = {
smile: false, innerBrowRaise: false, browRaise: false,
browFurrow: false, noseWrinkle: false, upperLipRaise: false,
lipCornerDepressor: false, chinRaise: false, lipPucker: false,
lipPress: false, lipSuck: false, mouthOpen: false,
smirk: false, eyeClosure: false, attention: false,
lidTighten: false, jawDrop: false, dimpler: false,
eyeWiden: false, cheekRaise: false, lipStretch: false
};
self.detectAllExpressions = function() {
setValueForAllKeys(self.detectExpressions, true);
};
self.detectAppearance = {
gender: false, glasses: false, age: false, ethnicity: false
};
self.detectAllAppearance = function() {
setValueForAllKeys(self.detectAppearance, true);
};
self.detectEmotions = {
joy: false, sadness: false, disgust: false,
contempt: false, anger: false, fear: false,
surprise: false, valence: false, engagement: false
};
self.detectAllEmotions = function() {
setValueForAllKeys(self.detectEmotions, true);
};
self.detectAllEmojis = function() {
self.detectEmojis = true;
};
self.getCallback = function(event, status) {
var state = event + "Failure";
if (status) {
state = event + "Success";
}
if (self.callbacks[state]) {
return self.callbacks[state];
}
else {
return function() {};
}
};
var getActiveKeys = function (dictionary) {
var retArray = [];
for (var key in dictionary) {
if (dictionary[key]) {
retArray.push(key);
}
}
return retArray;
};
var setValueForAllKeys = function (dictionary, value) {
for (var key in dictionary) {
dictionary[key] = value;
}
};
var onWorkerReady = function(status) {
if(status) {
self.worker.postMessage ({
"message" : "start",
"metrics": {"expressions": getActiveKeys(self.detectExpressions),
"emotions": getActiveKeys(self.detectEmotions),
"appearance": getActiveKeys(self.detectAppearance)},
"detectEmojis": self.detectEmojis,
"faceMode": self.faceDetectorMode,
"staticMode": self.staticMode,
"processFPS": self.processFPS
});
}
else {
var msg = "Failed to Initialize the worker";
self.getCallback("onInitialize", status)(msg);
}
};
self.onWorkerMessage = function(evt) {
if (evt.data && evt.data.message) {
var message = evt.data.message;
var status = evt.data.status;
switch(message) {
case "ready":
onWorkerReady(status);
break;
case "started":
self.onInitialize(status);
break;
case "stopped":
self.onStopped(status);
break;
case "reset":
self.getCallback("onReset", status)();
break;
case "results":
self.onImageResults(status, evt.data);
break;
}
}
};
self.reset = function() {
if(self.isWorkerInitialized) {
self.worker.postMessage( { "message": "reset" } );
}
else {
self.getCallback("onReset", false)("Failed to reset the detector");
}
};
//Callback functions
self.addEventListener = function(event, fn) {
self.callbacks[event] = fn;
return self;
};
self.removeEventListener = function(event) {
delete self.callbacks[event];
return self;
};
self.onStopped = function(status) {
if(status) {
self.worker.terminate();
self.worker = null;
self.isWorkerInitialized = false;
self.isRunning = false;
}
self.getCallback("onStop", status)();
};
};
//Define the prototypes for the functions that are going to be overrridden
//By children classes
affdex.Detector.prototype.start = function() {
if (!this.isRunning) {
var url = affdex.getAffdexDotJsLocation();
XHRWorker(url+"affdex-worker.js", function(worker) {
this.worker = worker;
this.worker.onmessage = this.onWorkerMessage;
this.worker.postMessage( { "message": "ctor", "url": url} );
this.isRunning = true;
}, this);
}
else {
this.getCallback("onInitialize", false)("Failed to start the detector, it is already running");
}
};
affdex.Detector.prototype.onImageResults = function(status, data) {
if(status) {
this.getCallback("onImageResults", status)(data.faces, data.img, data.time);
}
else {
this.getCallback("onImageResults", status)(data.img, data.time, data.detail);
}
};
affdex.Detector.prototype.onInitialize = function(status) {
if (status) {
this.isWorkerInitialized = true;
}
this.getCallback("onInitialize", status)("Failed to Initialize the detectors");
};
affdex.Detector.prototype.stop = function() {
if (this.isWorkerInitialized) {
this.worker.postMessage( { "message": "stop" } );
}
else {
this.getCallback("onStop", false)("Failed to stop the detector, it is not running");
}
};
affdex.CameraDetector = function(element, imgW, imgH, faceMode) {
var self = this;
affdex.Detector.call(self);
var cameraStream = null;
var width = imgW || 640;
var height = imgH || 480;
var startTimeStamp = (new Date()).getTime() / 1000;
var docElement = element || document.createElement("div");
var canvasElement = null;
var canvasContext = null;
var adapterJSVersion = "adapter-1.4.0.js";
self.faceDetectorMode = (typeof faceMode == 'undefined') ? affdex.FaceDetectorMode.LARGE_FACES : faceMode;
var ctor = function() {
self.videoElement = document.createElement("video");
self.videoElement.id = "face_video";
self.videoElement.autoplay = true;
docElement.appendChild(self.videoElement);
startTimeStamp = (new Date()).getTime() / 1000;
canvasElement = document.createElement("canvas");
canvasElement.id = "face_video_canvas";
canvasElement.width = width;
canvasElement.height = height;
canvasElement.style.display = "none";
docElement.appendChild(canvasElement);
canvasContext = canvasElement.getContext('2d');
};
var dtor = function() {
if (self.videoElement) {
self.videoElement.remove();
}
if (canvasElement) {
canvasElement.remove();
}
self.videoElement = null;
canvasElement = null;
};
var captureImage = function() {
if (self.isWorkerInitialized && canvasElement) {
canvasContext.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasContext.drawImage(self.videoElement, 0, 0, width, height);
var imgData = canvasContext.getImageData(0, 0, canvasElement.width, canvasElement.height);
var currentTimeStamp = (new Date()).getTime() / 1000;
process(imgData, currentTimeStamp - startTimeStamp);
}
};
self.onWebcamReady = function(stream) {
cameraStream = stream;
self.videoElement.addEventListener("canplay", function() {
self.videoElement.play();
});
var playingFn = function() {
self.videoElement.removeEventListener("playing", self);
//Call parent function
affdex.Detector.prototype.start.apply(self);
};
self.videoElement.addEventListener("playing", playingFn);
self.videoElement.src = window.URL.createObjectURL(stream);
self.getCallback("onWebcamConnect", true)();
};
var require = function(rootDiv, file, success_callback, failure_callback) {
var node = document.createElement("script");
node.type = "text/javascript";
node.src = file;
node.onload = success_callback;
node.onerror = function(err) {
failure_callback("Error loading js file: " + file);
};
rootDiv.appendChild(node);
};
var process = function(img, timeStamp) {
if(self.isWorkerInitialized) {
self.worker.postMessage({
"message": "process",
"img": img,
"time": timeStamp
});
}
};
self._startCamera = function() {
navigator.mediaDevices.getUserMedia({
video: true,
audio: false
}).then(self.onWebcamReady).catch(self.getCallback("onWebcamConnect", false));
};
self.start = function() {
if(!self.isRunning) {
ctor();
var url = affdex.getAffdexDotJsLocation() + adapterJSVersion;
require(docElement, url, function() {
self._startCamera();
},
function() {
self.getCallback("onInitialize", false)("Unable to load adaptor.js to load the camera");
});
}
};
self.onInitialize = function(status) {
//Call parent function
affdex.Detector.prototype.onInitialize.call(self, status);
if (status) {
captureImage();
}
};
self.onImageResults = function(status, data) {
captureImage();
affdex.Detector.prototype.onImageResults.call(self, status, data);
};
self.stop = function() {
if (cameraStream && (typeof cameraStream.getTracks != "undefined")) {
var tracks = cameraStream.getTracks();
tracks.forEach(function(track, idx, arr) {
track.stop();
});
}
dtor();
//Call parent function
affdex.Detector.prototype.stop.call(self);
};
};
affdex.FrameDetector = function(faceMode) {
var self = this;
affdex.Detector.call(self);
self.faceDetectorMode = (typeof faceMode == 'undefined') ? affdex.FaceDetectorMode.LARGE_FACES : faceMode;
self.start = function() {
affdex.Detector.prototype.start.call(self);
};
self.onInitialize = function(status) {
//Call parent function
affdex.Detector.prototype.onInitialize.call(self, status);
};
self.onImageResults = function(status, data) {
affdex.Detector.prototype.onImageResults.call(self, status, data);
};
self.stop = function() {
affdex.Detector.prototype.stop.call(self);
};
};
affdex.FrameDetector.prototype.process = function(img, timeStamp) {
if(this.isWorkerInitialized) {
this.worker.postMessage({
"message": "process",
"img": img,
"time": timeStamp
});
}
else {
this.getCallback("onImageResults", false)(img, timeStamp, "the detector is not initialized");
}
};
affdex.PhotoDetector = function(faceMode) {
var self = this;
affdex.FrameDetector.call(self);
self.staticMode = true;
self.faceDetectorMode = (typeof faceMode == 'undefined') ? affdex.FaceDetectorMode.SMALL_FACES : faceMode;
self.process = function(img, timeStamp) {
affdex.FrameDetector.prototype.process.call(self, img, timeStamp);
};
};