xxxxxxxxxx
118
let video;
let model;
let faces = [];
let osc;
let soundFile, reverb;
let isPlaying = false;
function preload() {
soundFile = loadSound('sound/o.mp3');
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
video = createCapture(VIDEO, () => {
video.size(640, 480);
video.hide();
console.log(video);
});
// Load the model
facemesh.load().then(function(_model) {
model = _model;
});
frameRate(30);
reverb = new p5.Reverb();
soundFile.disconnect(); // so we'll only hear reverb...
soundFile.loop();
// connect soundFile to reverb, process w/
// 3 second reverbTime, decayRate of 2%
reverb.process(soundFile, 3, 2);
// osc = new p5.Oscillator();
// osc.setType('sine');
// osc.freq(440);
// osc.start();
// osc.amp(0);
}
// Detect faces using the video
function detect() {
model.estimateFaces(video.elt).then(function(_faces) {
faces = _faces;
});
}
function draw() {
background(220);
// Detect faces assuming the model has loaded
if (model) detect();
// console.log(faces.lenght);
// if (faces.length > 0) {
// playSound();
// } else {
// stopSound();
// }
// Move everything to the top-left corner
// WEBGL draws 0,0,0 in the center of the screen
push();
translate(-width / 2, -height / 2, 0);
// Make sure video is behind all the face points
push();
translate(0, 0, -35);
image(video, 0, 0);
// image(video, 0, 0, width, height);
pop();
// Iterate through the faces
for (let face of faces) {
let a = face.annotations.leftEyeUpper0[0][2];
let b = face.annotations.rightEyeUpper0[0][2];
let diff = abs(a - b);
let dryWet = constrain(map(diff, 0, 80, 0, 1), 0, 1);
reverb.drywet(dryWet);
// let amp = map(diff, 5, 80, 1, 0);
// //let amp = map(mouseX, 0, width, 0, 1);
// amp = max(amp, 0);
// //console.log(diff, amp);
// osc.amp(amp, 1);
let positions = face.scaledMesh;
//console.log(positions[0]);
// Each position is stored as an array of 3 numbers
// representing the x,y,z position of the mesh point.
// positions[0] = [35.345, 65.223, 123.11];
strokeWeight(10);
stroke('white');
for (let pos of positions) {
push();
// Flip the z-coordinate
translate(pos[0], pos[1], -pos[2]);
point(0, 0, 0);
pop();
}
}
pop();
}
function playSound() {
if (!isPlaying) {
isPlaying = true;
soundFile.loop();
}
}
function stopSound() {
if (isPlaying) {
isPlaying = false;
soundFile.stop();
}
}