xxxxxxxxxx
83
let video;
let facemeshModel;
let face;
let brain;
let faceLabel = "N/A"; // Default label when no face is detected or classified
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.hide();
// Load the Facemesh model
facemeshModel = ml5.facemesh(video, modelReady);
facemeshModel.on('predict', gotFaces);
// Setup neural network
const options = {
inputs: 468 * 2, // Using x and y coordinates for each of the 468 facemesh points
outputs: 4, // Assuming 4 classes for classification
task: 'classification',
debug: true
};
brain = ml5.neuralNetwork(options);
// Load the trained model
const modelInfo = {
model: 'model2/model.json',
metadata: 'model2/model_meta.json',
weights: 'model2/model.weights.bin',
};
brain.load(modelInfo, brainLoaded);
}
function modelReady() {
console.log('Facemesh model ready');
}
function gotFaces(results) {
if (results.length > 0) {
face = results[0];
classifyFace();
}
}
function classifyFace() {
if (face) {
let inputs = [];
for (let i = 0; i < face.scaledMesh.length; i++) {
inputs.push(face.scaledMesh[i][0]); // x coordinate
inputs.push(face.scaledMesh[i][1]); // y coordinate
// Optionally, include z coordinate
// inputs.push(face.scaledMesh[i][2]);
}
brain.classify(inputs, gotResult);
}
}
function gotResult(error, results) {
if (results && results[0].confidence > 0.75) {
faceLabel = results[0].label.toUpperCase();
}
// Continue classification with the next frame
classifyFace();
}
function brainLoaded() {
console.log('Facemesh classification ready!');
}
function draw() {
push();
translate(video.width, 0);
scale(-1, 1); // Mirror the video feed
image(video, 0, 0, width, height);
fill(255, 0, 255);
noStroke();
textSize(64);
textAlign(CENTER, CENTER);
text(faceLabel, width / 2, height / 2);
pop();
}