xxxxxxxxxx
let faceapi;
let video;
let detections;
let pplCount = 0;
// by default all options are set to true
const detection_options = {
withLandmarks: true,
withDescriptors: false,
}
function setup() {
createCanvas(360 *2, 270 *2);
textAlign(CENTER);
// load up your video
video = createCapture(VIDEO);
video.size(width, height);
// video.hide(); // Hide the video element, and just show the canvas
faceapi = ml5.faceApi(video, detection_options, modelReady);
}
function modelReady() {
console.log('ready!');
console.log(faceapi);
faceapi.detect(gotResults);
}
function gotResults(err, result) {
if (err) {
console.log(err);
return
}
detections = result;
background(255);
image(video, 0, 0, width, height);
fill(255);
stroke(200);
strokeWeight(1);
textSize(15);
if (detections) {
if (detections.length > 0) {
// for = do it for every face detected
for (let i = 0; i < detections.length; i++) {
const eyeLeft = [detections[i].parts.leftEye[0]._x, detections[i].parts.leftEye[0]._y];
const eyeRight = [detections[i].parts.rightEye[0]._x, detections[i].parts.rightEye[0]._y];
// get face center x & y
const faceCenter = [detections[i].parts.nose[0]._x, detections[i].parts.nose[0]._y];
text("center", faceCenter[0], faceCenter[1]);
// get mouth landmarks
const mouth = detections[i].parts.mouth;
let mouthL = [mouth[0]._x, mouth[0]._y]; // left
let mouthR = [mouth[6]._x, mouth[6]._y]; // right
let mouthT = [mouth[3]._x, mouth[3]._y]; // center top
let mouthB = [mouth[9]._x, mouth[9]._y]; // center bottom
// draw important mouth landmarks
text("left", mouthL[0], mouthL[1]);
text("right", mouthR[0], mouthR[1]);
text("top", mouthT[0], mouthT[1]);
text("bottom", mouthB[0], mouthB[1]);
if (mouthL[1] <= mouthT[1] + 3 && mouthR[1] <= mouthT[1] + 3) {
text("smiling", faceCenter[0], faceCenter[1] - 50);
} else {
censor(mouthB, eyeLeft, eyeRight);
}
}
}
fill(20);
pplCount = detections.length;
text(pplCount, 20, 20);
}
faceapi.detect(gotResults);
}
function censor(mouthB, eyeLeft, eyeRight) {
var xleftEye = eyeLeft[0]; //umaendern zu x-Position vom linkem Auge!
var xrightEye = eyeRight[0]; //umaendern zu x-Position vom rechten Auge!
var yEye = eyeLeft[1]; //umaendern zu y-Position von einem Auge!
var ymouth = mouthB[1]; //umaendern zu y-Position vom Mund!
//img.loadPixels();
//clear();
//only take the space of the face to do this pixeling!!!!
for (var y = yEye; y < ymouth; y+= 10) {
for (var x = xleftEye; x < xrightEye; x+= 10) {
//vColorful option (takes a while to load!)
//var color = img.get(x, y);
//vBLACK AND WHITE OPTION(loads faster)v
// var color = img.pixels[(y * img.width + x) * 4];
noStroke();
fill( random(100, 200));
square(x, y, 10);
}
}
}