-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
400 lines (343 loc) · 11.5 KB
/
index.html
File metadata and controls
400 lines (343 loc) · 11.5 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
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
<!doctype html>
<html lang="en">
<head>
<title>Face tracker</title>
<meta charset="utf-8">
<link href="/examples/styles/bootstrap.min.css" rel="stylesheet" type="text/css">
<style>
@import url(https://fonts.googleapis.com/css?family=Lato:300italic,700italic,300,700);
body {
font-family: 'Lato';
background-color: #f0f0f0;
margin: 0px auto;
max-width: 1150px;
}
#overlay {
position: absolute;
top: 0px;
left: 0px;
-o-transform : scaleX(-1);
-webkit-transform : scaleX(-1);
transform : scaleX(-1);
-ms-filter : fliph; /*IE*/
filter : fliph; /*IE*/
}
#videoel {
-o-transform : scaleX(-1);
-webkit-transform : scaleX(-1);
transform : scaleX(-1);
-ms-filter : fliph; /*IE*/
filter : fliph; /*IE*/
}
#container {
position : relative;
width : 370px;
/*margin : 0px auto;*/
}
#content {
margin-top : 70px;
margin-left : 100px;
margin-right : 100px;
max-width: 950px;
}
h2 {
font-weight : 400;
}
.nogum {
display : none;
}
.btn {
font-family: 'Lato';
font-size: 16px;
}
.hide {
display : none;
}
.nohide {
display : block;
}
#emotion_container {
width: 600px;
}
#emotion_icons {
height: 50px;
padding-left: 40px;
}
.emotion_icon {
width : 40px;
height : 40px;
margin-top: 5px;
/*margin-left : 13px;*/
margin-left : 18px;
}
#emotion_chart, #emotion_icons {
margin: 0 auto;
width : 500px;
}
#icon1, #icon2, #icon3, #icon4, #icon5, #icon6, #icon7 {
/*visibility : hidden;*/
visibility : visible;
}
/* d3 */
.bar {
fill : steelblue;
fill-opacity : .9;
}
</style>
<script>
// getUserMedia only works over https in Chrome 47+, so we redirect to https. Also notify user if running from file.
if (window.location.protocol == "file:") {
alert("You seem to be running this example directly from a file. Note that these examples only work when served from a server or localhost due to canvas cross-domain restrictions.");
} else if (window.location.hostname !== "localhost" && window.location.protocol !== "https:"){
window.location.protocol = "https";
}
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32642923-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div id="content">
<h2>Emotion SVM Classifier</h2>
<div id="emotion_container">
<div id="emotion_icons">
<img class="emotion_icon" id="icon1" src="/media/icon_anger.png">
<img class="emotion_icon" id="icon2" src="/media/icon_contempt.png">
<img class="emotion_icon" id="icon3" src="/media/icon_disgust.png">
<img class="emotion_icon" id="icon4" src="/media/icon_fear.png">
<img class="emotion_icon" id="icon5" src="/media/icon_happy.png">
<img class="emotion_icon" id="icon6" src="/media/icon_sadness.png">
<img class="emotion_icon" id="icon7" src="/media/icon_surprise.png">
</div>
<div id='emotion_chart'></div>
</div>
<div id="container">
<video id="videoel" width="400" height="300" preload="auto" loop playsinline autoplay>
</video>
<canvas id="overlay" width="400" height="300"></canvas>
</div>
<br/>
<input class="btn" type="button" value="wait, loading video" disabled="disabled" onclick="startVideo()" id="startbutton"></input>
<div id="text">
<div id="gum" class="gum">
<p>To try it out:
<ol>
<li>allow the page to use your webcamera</li>
<li>make sure that your face is clearly visible in the video, and click start</li>
<li>see the model fitted to your face</li>
<ol>
</p>
</div>
<div id="nogum" class="nogum">
<p>
There was some problem trying to capture your webcamera, please check that your browser supports WebRTC. Using a fallback video instead. To try it out:
<ol>
<li>click start</li>
<li>see the model fitted to the face</li>
</ol>
</p>
</div>
</div>
<script src="/js/libs/utils.js"></script>
<script src="/js/clmtrackr.js"></script>
<script src="/js/libs/Stats.js"></script>
<script src="/js/socket.io.js"></script>
<script src="/js/jquery-1.11.1.js"></script>
<script src="./js/libs/d3.min.js"></script>
<script>
var emotions = ['neutral', 'anger', 'contempt', 'disgust', 'fear', 'happy', 'sadness', 'surprise'];
var margin = {top : 20, right : 20, bottom : 10, left : 40},
width = 500 - margin.left - margin.right,
height = 100 - margin.top - margin.bottom;
var barWidth = 30;
var formatPercent = d3.format(".0%");
var x = d3.scale.linear()
.domain([0, 7]).range([margin.left, width+margin.left]);
var y = d3.scale.linear()
.domain([0,1]).range([0, height]);
var svg = d3.select("#emotion_chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
var blank_init = [];
for (var j = 0;j < emotions.length-1;j++) {
blank_init[j] = {"emotion" : emotions[j+1], "value" : 0.0};
}
svg.selectAll("rect").
data(blank_init).
enter().
append("svg:rect").
attr("x", function(datum, index) { return x(index); }).
attr("y", function(datum) { return height - y(datum.value); }).
attr("height", function(datum) { return y(datum.value); }).
attr("width", barWidth).
attr("fill", "#2d578b");
svg.selectAll("text.labels").
data(blank_init).
enter().
append("svg:text").
attr("x", function(datum, index) { return x(index) + barWidth; }).
attr("y", function(datum) { return height - y(datum.value); }).
attr("dx", -barWidth/2).
attr("dy", "1.2em").
attr("text-anchor", "middle").
text(function(datum) { return datum.value;}).
attr("fill", "white").
attr("class", "labels");
svg.selectAll("text.yAxis").
data(blank_init).
enter().append("svg:text").
attr("x", function(datum, index) { return x(index) + barWidth; }).
attr("y", height).
attr("dx", -barWidth/2).
attr("text-anchor", "middle").
attr("style", "font-size: 12").
text(function(datum) { return datum.emotion;}).
attr("transform", "translate(0, 18)").
attr("class", "yAxis");
var socket = io();
socket.emit('init', "Socket Initialized");
//RYAN listen for feature point response from node server and output response
socket.on('featurePointsResponse', function(prob){
var emotionData = []
for (var j=0; j< emotions.length-1; j++){
emotionData[j] = {"emotion" : emotions[j+1], "value" : prob[String(j+1)]};
}
for (var i = 0;i < emotionData.length;i++) {
if (emotionData[i].value > 0.3) {
document.getElementById('icon'+(i+1)).style.visibility = 'visible';
} else {
document.getElementById('icon'+(i+1)).style.visibility = 'hidden';
}
}
// update
var rects = svg.selectAll("rect")
.data(emotionData)
.attr("y", function(datum) { return height - y(datum.value); })
.attr("height", function(datum) { return y(datum.value); });
var texts = svg.selectAll("text.labels")
.data(emotionData)
.attr("y", function(datum) { return height - y(datum.value); })
.text(function(datum) { return datum.value.toFixed(1);});
// enter
rects.enter().append("svg:rect");
texts.enter().append("svg:text");
// exit
rects.exit().remove();
texts.exit().remove();
});
</script>
<script>
var vid = document.getElementById('videoel');
var vid_width = vid.width;
var vid_height = vid.height;
var overlay = document.getElementById('overlay');
var overlayCC = overlay.getContext('2d');
/*********** Setup of video/webcam and checking for webGL support *********/
function enablestart() {
var startbutton = document.getElementById('startbutton');
startbutton.value = "start";
startbutton.disabled = null;
}
var insertAltVideo = function(video) {
// insert alternate video if getUserMedia not available
if (supports_video()) {
if (supports_webm_video()) {
video.src = "./media/cap12_edit.webm";
} else if (supports_h264_baseline_video()) {
video.src = "./media/cap12_edit.mp4";
} else {
return false;
}
return true;
} else return false;
}
function adjustVideoProportions() {
// resize overlay and video if proportions of video are not 4:3
// keep same height, just change width
var proportion = vid.videoWidth/vid.videoHeight;
vid_width = Math.round(vid_height * proportion);
vid.width = vid_width;
overlay.width = vid_width;
}
function gumSuccess( stream ) {
// add camera stream if getUserMedia succeeded
if ("srcObject" in vid) {
vid.srcObject = stream;
} else {
vid.src = (window.URL && window.URL.createObjectURL(stream));
}
vid.onloadedmetadata = function() {
adjustVideoProportions();
vid.play();
}
vid.onresize = function() {
adjustVideoProportions();
if (trackingStarted) {
ctrack.stop();
ctrack.reset();
ctrack.start(vid);
}
}
}
function gumFail() {
// fall back to video if getUserMedia failed
insertAltVideo(vid);
document.getElementById('gum').className = "hide";
document.getElementById('nogum').className = "nohide";
alert("There was some problem trying to fetch video from your webcam, using a fallback video instead.");
}
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.msURL || window.mozURL;
// set up video
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({video : true}).then(gumSuccess).catch(gumFail);
} else if (navigator.getUserMedia) {
navigator.getUserMedia({video : true}, gumSuccess, gumFail);
} else {
insertAltVideo(vid);
document.getElementById('gum').className = "hide";
document.getElementById('nogum').className = "nohide";
alert("Your browser does not seem to support getUserMedia, using a fallback video instead.");
}
vid.addEventListener('canplay', enablestart, false);
//Code for face tracking
var ctrack = new clm.tracker();
ctrack.init();
var trackingStarted = false;
function startVideo() {
// start video
vid.play();
// start tracking
ctrack.start(vid);
trackingStarted = true;
// start loop to draw face
drawLoop();
}
function drawLoop() {
requestAnimFrame(drawLoop);
overlayCC.clearRect(0, 0, vid_width, vid_height);
if (ctrack.getCurrentPosition()) {
//pass facial feature points to server to classification
socket.emit('featurePoints', ctrack.getCurrentPosition());
ctrack.draw(overlay);
}
}
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
document.getElementById('container').appendChild( stats.domElement );
// update stats on every iteration
document.addEventListener('clmtrackrIteration', function(event) {
stats.update();
}, false);
</script>
</div>
</body>
</html>