p5(sketch => {
return
let video;
let t = 0;
const opciones = {
withLandmarks: true,
withDescriptors: false,
}
let faceapi;
let listo = false;
sketch.setup = function() {
sketch.createCanvas(640, 480);
video = sketch.createCapture(sketch.VIDEO);
video.hide();
sketch.frameRate(12);
sketch.textSize(40);
sketch.textAlign(sketch.CENTER);
faceapi = ml5.faceApi(video, opciones, () => {listo = true });
};
sketch.draw = function() {
if (listo) {
faceapi.detect(mostrarResultados);
} else {
sketch.text(`CARGANDO`, sketch.width / 2, sketch.height / 2);
}
};
function mostrarResultados(error, resultados) {
if (error) return
sketch.image(video, 0, 0);
if (resultados) {
if (resultados.length > 0) {
dibujarCajas(resultados);
}
}
faceapi.detect(mostrarResultados);
}
function dibujarCajas(resultados) {
for(let i = 0; i < resultados.length; i++){
const alignedRect = resultados[i].alignedRect;
const x = alignedRect._box._x
const y = alignedRect._box._y
const boxWidth = alignedRect._box._width
const boxHeight = alignedRect._box._height
sketch.noFill();
sketch.stroke(161, 95, 251);
sketch.strokeWeight(2);
sketch.rect(x, y, boxWidth, boxHeight);
}
}
})