xxxxxxxxxx
let img;
let faceapi;
let parts;
let video;
let pg, mask;
function preload() {
//img = loadImage("image.jpg");
/*img = createImg('https://thispersondoesnotexist.com/image?.jpg',
'Portrait'
);
img.hide();
print(img);*/
}
function setup() {
createCanvas(640, 480);
background(100,2);
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
//img.resize(height, height);
//faceapi = ml5.faceApi(modelLoaded);
faceapi = ml5.faceApi(video, modelLoaded);
}
function draw() {
noTint();
image(video, 0, 0);
if (parts != null) {
//print("parts");
/*
bounds = boundingbox(parts.leftEyebrow);
lebimg = video.get(bounds[0], bounds[1], bounds[2], bounds[3]);
image(lebimg, bounds[0], bounds[1]);
bounds = boundingbox(parts.rightEyebrow);
rebimg = video.get(bounds[0], bounds[1], bounds[2], bounds[3]);
image(rebimg, bounds[0], bounds[1]);
*/
tint(255, 200, 200);
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.leftEyebrow.length; i++)
text(i, parts.leftEyebrow[i]._x, parts.leftEyebrow[i]._y);
//ellipse(parts.mouth[i]._x, parts.mouth[i]._y, 5, 5);
// vertex(parts.mouth[i]._x, parts.mouth[i]._y);
endShape(CLOSE);
*/
/*
lebimg = video.get(parts.leftEyebrow[2]._x-100, parts.leftEyebrow[2]._y-100, 200, 200);
pg = createGraphics(lebimg.width, lebimg.height);
pg.fill(255);
//pg.noStroke();
//pg.stroke(0);
//pg.strokeWeight(4);
pg.ellipse(lebimg.width/2, lebimg.height/2+10, 40, 20);
masked = lebimg.get();
masked.mask(pg);
image(masked, parts.leftEyebrow[2]._x-100, parts.leftEyebrow[2]._y-100);
rebimg = video.get(parts.rightEyebrow[2]._x-100, parts.rightEyebrow[2]._y-100, 200, 200);
pg = createGraphics(rebimg.width, rebimg.height);
pg.fill(255);
//pg.noStroke();
//pg.stroke(0);
//pg.strokeWeight(4);
pg.ellipse(rebimg.width/2, rebimg.height/2+10, 40, 20);
masked = rebimg.get();
masked.mask(pg);
image(masked, parts.rightEyebrow[2]._x-100, parts.rightEyebrow[2]._y-100);
*/
leimg = video.get(parts.leftEye[2]._x-100, parts.leftEye[2]._y-100, 200, 200);
pg = createGraphics(leimg.width, leimg.height);
pg.fill(255);
//pg.noStroke();
//pg.stroke(0);
//pg.strokeWeight(4);
pg.ellipse(leimg.width/2, leimg.height/2+10, 40, 20);
masked = leimg.get();
masked.mask(pg);
image(masked, parts.leftEye[2]._x-100, parts.leftEye[2]._y-100);
/*
reimg = video.get(parts.rightEye[2]._x-100, parts.rightEye[2]._y-100, 200, 200);
pg = createGraphics(reimg.width, reimg.height);
pg.fill(255);
//pg.noStroke();
//pg.stroke(0);
//pg.strokeWeight(4);
pg.ellipse(reimg.width/2, reimg.height/2+10, 40, 20);
masked = reimg.get();
masked.mask(pg);
image(masked, parts.rightEye[2]._x-100, parts.rightEye[2]._y-100);
nimg = video.get(parts.nose[3]._x-100, parts.nose[3]._y-100, 200, 200);
pg = createGraphics(nimg.width, nimg.height);
pg.fill(255);
//pg.noStroke();
//pg.stroke(0);
//pg.strokeWeight(4);
pg.ellipse(nimg.width/2, nimg.height/2, 50, 40);
masked = nimg.get();
masked.mask(pg);
image(masked, parts.nose[3]._x-100, parts.nose[3]._y-100);
mimg = video.get(parts.mouth[18]._x-100, parts.mouth[18]._y-100, 200, 200);
pg = createGraphics(mimg.width, mimg.height);
pg.fill(255);
//pg.noStroke();
//pg.stroke(255);
//pg.strokeWeight(4);
pg.ellipse(mimg.width/2, mimg.height/2, 70, 30);
masked = mimg.get();
masked.mask(pg);
image(masked, parts.mouth[18]._x-100, parts.mouth[18]._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]);
}