xxxxxxxxxx
let img;
let faceapi;
let parts;
let video;
function preload() {
img = loadImage("image.jpg");
/*img = createImg('https://thispersondoesnotexist.com/image?.jpg',
'Portrait'
);
img.hide();
print(img);*/
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
//img.resize(height, height);
//faceapi = ml5.faceApi(modelLoaded);
faceapi = ml5.faceApi(video, modelLoaded);
}
function draw() {
//image(img, 0, 0);
if (parts != null) {
//print("parts");
bounds = boundingbox(parts.leftEye);
leimg = video.get(bounds[0], bounds[1], bounds[2], bounds[3]);
image(leimg, bounds[0], bounds[1]);
bounds = boundingbox(parts.rightEye);
reimg = video.get(bounds[0], bounds[1], bounds[2], bounds[3]);
image(reimg, bounds[0], bounds[1]);
bounds = boundingbox(parts.nose);
nimg = video.get(bounds[0], bounds[1], bounds[2], bounds[3]);
//image(nimg, bounds[0], bounds[1]);
bounds = boundingbox(parts.mouth);
mimg = video.get(bounds[0]-50, bounds[1]-50, bounds[2]+50, bounds[3]+50);
//image(mimg, bounds[0], bounds[1]);
/*
stroke(255, 0, 100);
beginShape();
for (i = 0; i < parts.mouth.length; i++)
text(i, parts.mouth[i]._x, parts.mouth[i]._y);
//ellipse(parts.mouth[i]._x, parts.mouth[i]._y, 5, 5);
// vertex(parts.mouth[i]._x, parts.mouth[i]._y);
endShape(CLOSE);
*/
nimg = video.get(parts.nose[2]._x-100, parts.nose[2]._y-100, 200, 200);
pg = createGraphics(nimg.width, nimg.height);
pg.fill(255);
pg.noStroke();
pg.ellipse(nimg.width/2, nimg.height/2, 150, 150);
masked = nimg.get();
masked.mask(pg);
image(masked, parts.nose[2]._x-100, parts.nose[2]._y-100);
/*
pg = createGraphics(mimg.width, mimg.height);
pg.fill(255);
pg.noStroke();
for (i = 0; i < parts.mouth.length; i++)
pg.ellipse(parts.mouth[i]._x-bounds[0], parts.mouth[i]._y-bounds[1], 50, 50);
masked = mimg.get();
masked.mask(pg);
image(masked, bounds[0], bounds[1]);
//image(pg, bounds[0], bounds[1]);
*/
}
}
function modelLoaded() {
print("Model loaded");
faceapi.detect(gotResults);
}
function gotResults(err, result) {
parts = result[0].parts;
faceapi.detect(gotResults);
}
function boundingbox(coords) {
let xmin = 10000;
let ymin = 10000;
let xmax = -10000;
let ymax = -10000;
for (i = 0; i < coords.length; i++) {
if (coords[i]._x > xmax)
xmax = coords[i]._x;
if (coords[i]._x <= xmin)
xmin = coords[i]._x;
if (coords[i]._y > ymax)
ymax = coords[i]._y;
if (coords[i]._y <= ymin)
ymin = coords[i]._y;
}
return ([xmin, ymin, xmax - xmin, ymax - ymin]);
}