-
Notifications
You must be signed in to change notification settings - Fork 8
/
video.js
104 lines (87 loc) · 2.72 KB
/
video.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
let videofeed;
let posenet;
let poses = [];
let started = false;
var audio = document.getElementById("audioElement");
// p5.js setup() function to set up the canvas for the web cam video stream
function setup() {
//creating a canvas by giving the dimensions
const canvas = createCanvas(500, 500);
canvas.parent("video");
videofeed = createCapture(VIDEO);
videofeed.size(width, height);
console.log("setup");
// setting up the poseNet model to feed in the video feed.
posenet = ml5.poseNet(videofeed);
posenet.on("pose", function (results) {
poses = results;
});
videofeed.hide();
noLoop();
}
// p5.js draw function() is called after the setup function
function draw() {
if (started) {
image(videofeed, 0, 0, width, height);
calEyes();
}
}
// toggle button for starting the video feed
function start() {
select("#startstop").html("stop");
document.getElementById("startstop").addEventListener("click", stop);
started = true;
loop();
}
// toggle button for ending the video feed
function stop() {
select("#startstop").html("start");
document.getElementById("startstop").addEventListener("click", start);
removeblur();
started = false;
noLoop();
}
// defining the parameters used for the posenet : the tracking of the eyes
var rightEye,
leftEye,
defaultRightEyePosition = [],
defaultLeftEyePosition = [];
//function to calculate the position of the various keypoints
function calEyes() {
for (let i = 0; i < poses.length; i++) {
let pose = poses[i].pose;
for (let j = 0; j < pose.keypoints.length; j++) {
let keypoint = pose.keypoints[j];
rightEye = pose.keypoints[2].position;
leftEye = pose.keypoints[1].position;
// keypoints are the points representing the different joints on the body recognized by posenet
while (defaultRightEyePosition.length < 1) {
defaultRightEyePosition.push(rightEye.y);
}
while (defaultLeftEyePosition.length < 1) {
defaultLeftEyePosition.push(leftEye.y);
}
// if the current position of the body is too far from the original position blur function is called
if (Math.abs(rightEye.y - defaultRightEyePosition[0]) > 20) {
blur();
}
if (Math.abs(rightEye.y - defaultRightEyePosition[0]) < 20) {
removeblur();
}
}
}
}
//function to blur the background and add audio effect
function blur() {
document.body.style.filter = "blur(5px)";
document.body.style.transition = "1s";
var audio = document.getElementById("audioElement");
console.log("change");
audio.play();
}
//function to remove the blur effect
function removeblur() {
document.body.style.filter = "blur(0px)";
var audio = document.getElementById("audioElement");
audio.pause();
}