xxxxxxxxxx
142
let faceapi;
let video;
let detections;
// by default all options are set to true
const detection_options = {
withLandmarks: true,
withDescriptors: false,
}
function setup() {
createCanvas(460, 370);
colorMode(HSB);
// 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)
textAlign(RIGHT);
}
//CALLBACK FUNCTIONS:
function modelReady() {
console.log('ready!')
console.log(faceapi)
faceapi.detect(gotResults)
}
function gotResults(err, result) {
if (err) {
console.log(err)
return
}
//console.log(result)
detections = result;
// background(220);
background(255);
image(video, 0,0, width, height);
//if I found at least a face:
if (detections) {
if (detections.length > 0) {
//console.log(detections);
//drawBox(detections);
drawLandmarks(detections);
}
}
faceapi.detect(gotResults);
}
// FUNCTIONS:
function drawBox(detections){
for(let i = 0; i < detections.length; i++){
let alignedRect = detections[i].alignedRect;
let x = alignedRect._box._x
let y = alignedRect._box._y
let boxWidth = alignedRect._box._width
let boxHeight = alignedRect._box._height
noFill();
stroke(255, 0, 0);
strokeWeight(2);
rect(x, y, boxWidth, boxHeight);
}
}
function drawLandmarks(detections){
noFill();
stroke(255, 0, 0)
strokeWeight(2)
for(let i = 0; i < detections.length; i++){
let mouth = detections[i].parts.mouth;
let nose = detections[i].parts.nose;
let leftEye = detections[i].parts.leftEye;
let rightEye = detections[i].parts.rightEye;
let rightEyeBrow = detections[i].parts.rightEyeBrow;
let leftEyeBrow = detections[i].parts.leftEyeBrow;
drawPart(mouth, true);
// drawPart(nose, false);
// drawPart(leftEye, true);
// drawPart(leftEyeBrow, false);
// drawPart(rightEye, true);
// drawPart(rightEyeBrow, false);
}
}
function drawPart(feature, closed){
beginShape();
for(let i = 0; i < feature.length; i++){
let x = feature[i]._x
let y = feature[i]._y
//noFill();
//stroke (x,y,200);
console.log(feature);
//text([i],x,y);
//line(feature[15]._x, feature[15]._y,feature[17]._x,feature[17]._y);
mouthwide = dist(feature[15]._x, feature[15]._y,feature[17]._x,feature[17]._y);
if(mouthwide>5){
textSize(30);
fill(mouthwide*5, 100,100);
text("TALKING",130,100);
}
}
if(closed === true){
endShape(CLOSE);
} else {
endShape();
}
}