xxxxxxxxxx
189
let facemesh;
let video;
let predictions = [];
let margin = 50;
let dX=0,dY=0;//move the mask
let tX=0,tY=0;
let ease=1;
function mousePressed()
{
ease=-ease;
}
function setup()
{
createCanvas(800, 600);
video = createCapture(VIDEO);
video.size(640, 480);
facemesh = ml5.facemesh(video, modelReady);
// This sets up an event that fills the global variable "predictions"
// with an array every time new predictions are made
facemesh.on("predict", results => {
predictions = results;
});
// Hide the video element, and just show the canvas
video.hide();
}
function modelReady() {
console.log("Model ready!");
}
function draw()
{
drawBackground()
image(video, margin, margin, 640, 480);
// We can call both functions to draw all keypoints
drawKeypoints();
drawForeground();
}
function drawForeground()
{
cursor(CROSS);
let r=map((abs(dX)+abs(dY))/2,0,75,800,0)
//get red value when not at ease
let dRed=map(r,0,800,127,0)
if(ease==1)//ease foreground
{
fill(245,222,179,130);
noStroke();
circle(mouseX,mouseY,r/2);
}
if(ease==-1)//unease foreground
{
fill(125+dRed,0,0,2*dRed);
circle(0,0,r);
circle(width,0,r);
circle(0,height,r);
circle(width,height,r);
}
}
function drawBackground()
{
//dynamic size of the background dots
let sizeX=abs(dX);
let sizeY=abs(dY);
let size=(sizeX+sizeY)/2;
if(ease==1)
{
background("white");
let dGreen=map(size,0,75,0,110);
fill(61,145+dGreen,64,145+dGreen)
for(i=0;i<500;++i)
{
circle(random(width) , random(height), 85-size);
}
}
if(ease==-1)
{
background("black");
let dRed=map(size,0,75,0,127)
fill(125+dRed,0,0,2*dRed);
for(i=0;i<500;++i)
{
circle(random(width) , random(height), 10+size);
}
}
}
// A function to draw ellipses over the detected keypoints
function drawKeypoints()
{
//createCanvas(width,height);
//background(125,23,24,1);
for (let i = 0; i < predictions.length; i += 1)
{
const keypoints = predictions[i].scaledMesh;
if(key=="w")
{
tY=tY-3;
}
else if(key=="a")
{
tX=tX-3;
}
else if(key=="s")
{
tY=tY+3;
}
else if(key=="d")
{
tX=tX+3;
}
else
{
tX=0;
tY=0;
}
// Draw facial keypoints.
for (let j = 0; j < keypoints.length; j += 1)
{
const [x, y] = keypoints[j];
push()
translate(margin,margin)
//move the mask by mouse
if(mouseX<x)
{
dX=map(mouseX,0,x,-75,0);
}
else
{
dX=map(mouseX,x,width,0,75);
}
if(mouseY<y)
{
dY=map(mouseY,0,y,-75,0);
}
else
{
dY=map(mouseY,y,height,0,75);
}
if(ease==1)//face at ease
{
xX=x+tX;
yY=y+tY;
fill(176,224,230,120);
noStroke();
circle(xX,yY,10);
}
if(ease==-1)//face not ease
{
fill(random(255));
stroke(0);
square(x+dX , y+dY , 10);
}
pop()
}
}
}