forked from wesbos/HTML5-Face-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
46 lines (38 loc) · 1.44 KB
/
scripts.js
File metadata and controls
46 lines (38 loc) · 1.44 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
var
// Store the first HTML5 video element in the document
video = document.querySelector('video'),
// We use this to time how long things are taking. Not that important..
time_dump = document.getElementById("elapsed_time"),
// Create a new image that will be our goofy glasses
glasses = new Image(),
// Store the canvas so we can write to it
canvas = document.getElementById("output"),
// Get the canvas 2d Context
ctx = canvas.getContext("2d");
// Finally set the source of our new glasses img element
glasses.src = "i/glasses.png";
function html5glasses() {
// Start the clock
var elapsed_time = (new Date()).getTime();
// Draw the video to canvas
ctx.drawImage(video, 0, 0, video.width, video.height, 0, 0, canvas.width, canvas.height);
// use the face detection library to find the face
var comp = ccv.detect_objects({ "canvas" : (ccv.pre(canvas)),
"cascade" : cascade,
"interval" : 5,
"min_neighbors" : 1 });
// Stop the clock
time_dump.innerHTML = "Process time : " + ((new Date()).getTime() - elapsed_time).toString() + "ms";
// Draw glasses on everyone!
for (var i = 0; i < comp.length; i++) {
ctx.drawImage(glasses, comp[i].x, comp[i].y,comp[i].width, comp[i].height);
}
}
/* Events */
video.addEventListener('play', function() {
vidInterval = setInterval(html5glasses,200);
});
video.addEventListener('ended', function() {
clearInterval(vidInterval);
time_dump.innerHTML = "finished";
});