xxxxxxxxxx
131
let capture;
let tracker;
let sx
let sy
let w
let dx
let dy
let cg
let mic
let i;
function setup() {
// load p5 functions:
createCanvas(600, 450);
capture = createCapture(VIDEO);
capture.elt.setAttribute('playsinline', ''); // this line makes your program works on iPhone 11
capture.size(width, height);
capture.hide();
// load clmtrackr functions:
tracker = new clm.tracker(); // create a new clmtrackr object
tracker.init(); // initialize the object
tracker.start(capture.elt); // start tracking the video element capture.elt
cg=createGraphics(width,height)
pixelDensity(1)
//mic
mic = new p5.AudioIn();
mic.start();
}
function draw() {
image(capture, 0, 0, width, height);
let positions = tracker.getCurrentPosition();
// updates the tracker with current positions
background(0)
if (positions.length > 0) {
var array2D = [
[positions[23][0]-10,positions[24][1]-20,50,75], //lefteye
[positions[14][0]-10,positions[14][1]-20,450,75], //right face
[positions[0][0]-30,positions[0][1]-20,50,175],//leftface
[positions[30][0]-10,positions[29][1]-20,450,175],//righteye
[positions[35][0]-10,positions[39][1]-30,50,275], //nose
[positions[44][0],positions[46][1],450,275] //mouth
];
//console.log(array2D.length)
for (let i = 0; i < array2D.length; i++) {
//console.log(i)
sx = int(array2D[i][0])
sy = int(array2D[i][1])
w = 60
dx = int(array2D[i][2])
dy = int(array2D[i][3])
cg.copy(capture,sx,sy,w,w,dx,dy,w,w)
image(cg,0,0, width, height)
}
// draw face outline
noFill();
stroke(255);
beginShape();
for (let i = 0; i < positions.length; i++) {
vertex(positions[i][0], positions[i][1])
}
endShape();
//pixelgraph
// loadPixels()
// for (let y=0; y < height ; y++){
// for (let x=0; x < width; x++) {
// let index = (x + y * width) * 4
// fill(pixels[index], 0, 0)
// circle(100, 75, 30)
// fill(0,pixels[index + 1],0)
// circle(200, 75, 30)
// fill(0, 0, pixels[index + 2])
// circle(300, 75, 30)
// }
// }
//smile
push()
strokeWeight(3)
let mouthLeft = createVector(positions[44][0], positions[44][1]);
let mouthRight = createVector(positions[50][0], positions[50][1]);
let smile = mouthLeft.dist(mouthRight);
stroke(250)
circle(100, 75, smile/2)
//left eye
let eyeTop = createVector(positions[24][0], positions[24][1])
let eyeBot = createVector(positions[26][0], positions[26][1])
let eye = eyeTop.dist(eyeBot);
stroke(250,250,0)
circle(200, 75, eye*2)
//right brow
let browLeft = createVector(positions[18][0], positions[18][1])
let browRight = createVector(positions[15][0], positions[15][1])
let brow = browLeft.dist(browRight);
stroke(0, 0, 250)
circle(300, 75, brow/2)
pop()
//mic input
let vol = mic.getLevel();
console.log(vol)
let h = map(vol, 0, 1, 50, 500);
let p = map(vol, 0, 1, 0, 250)
fill(250,0,0)
stroke(250,0,0)
ellipse(positions[3][0]-50, positions[3][1], h);
}
}