xxxxxxxxxx
105
// MOOD TRACKER CAMERA
var capture; // this is the video camera
var capimg;
var theblur;
var a = 0.05; // copy ratio
var b = 1.0 - a; // blur ratio
var tracker;
var pos;
function setup() {
createCanvas(550, 420); // correct portions with my webcam
pixelDensity(1);
capture = createCapture({
audio: false,
video: {
width: width,
height: height
}
}, function() {
console.log("capture ready at:" + millis())
});
capture.elt.setAttribute('playsinline', '');
theblur = createImage(width, height);
capture.size(width, height);
capture.hide();
tracker = new clm.tracker();
tracker.init();
tracker.start(capture.elt);
}
function draw() {
//background(220);
//loadPixels();
capimg = capture.get(); // copying the video
/* source: https://idmnyu.github.io/p5.js-image/VidBlur/index.html */
if (capimg.width > 0) {
capimg.loadPixels();
theblur.loadPixels();
for (var i = 0; i < theblur.pixels.length; i += 4) { // iterate through red values
theblur.pixels[i] = a * capimg.pixels[i] + b * theblur.pixels[i];
theblur.pixels[i + 1] = a * capimg.pixels[i + 1] + b * theblur.pixels[i + 1];
theblur.pixels[i + 2] = a * capimg.pixels[i + 2] + b * theblur.pixels[i + 2];
theblur.pixels[i + 3] = capimg.pixels[i + 3];
}
theblur.updatePixels();
image(theblur, 0, 0, width, height);
}
/* source: https://editor.p5js.org/kylemcdonald/sketches/BJOcyD9hm */
pos = tracker.getCurrentPosition();
if (pos.length > 0) { // smile meter
var mouthLeft = createVector(pos[44][0], pos[44][1]);
var mouthRight = createVector(pos[50][0], pos[50][1]);
var smile = mouthLeft.dist(mouthRight);
//console.log(smile);
var browDistance = pos[18][0] - pos[22][0];
//console.log(browDistance);
var mouthOpen = pos[57][1] - pos[60][1];
//console.log(mouthOpen);
textSize(32);
// Change filter/ tint based on mood
if (smile < 75 && browDistance >= 45 && mouthOpen < 5) {
tint(0, 153, 204, 126); // neutral
text('Neutral', width/2, height/2);
fill(0, 102, 153);
}
else if (smile > 75 && browDistance >= 45) {
tint(255, 255, 0, 126); // happy
text('Happy', width/2, height/2);
fill(255, 255, 0);
}
else if (smile < 75 && browDistance < 45 && mouthOpen < 5) {
tint(255, 0, 0, 126); // angry
text('Angry', width/2, height/2);
fill(255, 0, 0);
}
else if (mouthOpen > 5 && smile < 75) {
tint(0, 255, 0, 126); // angry
text('Surprised', width/2, height/2);
fill(0, 255, 0);
}
}
}