xxxxxxxxxx
109
let facemesh;
let video;
let predictions = [];
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(width, height);
facemesh = ml5.facemesh(video, modelReady);
// This sets up an event that fills the global variable "predictions"
// with an array every time new predictions are made
facemesh.on("predict", (results) => {
predictions = results;
});
// Hide the video element, and just show the canvas
video.hide();
}
function modelReady() {
console.log("Model ready!");
}
function draw() {
image(video, 0, 0, width, height);
// We can call both functions to draw all keypoints
drawKeypoints();
detectBlink();
console.log(predictions);
}
// A function to draw ellipses over the detected keypoints
function detectBlink() {
for (let i = 0; i < predictions.length; i += 1) {
// eye array
const prediction = predictions[i];
let rightEyeUpper = prediction.annotations.rightEyeUpper1;
let rightEyeLower = prediction.annotations.rightEyeLower1;
let leftEyeUpper = prediction.annotations.leftEyeUpper1;
let leftEyeLower = prediction.annotations.leftEyeLower1;
// Calculate average distance between upper and lower eyelids for both eyes
let rightEyeGap = eyeGap(rightEyeUpper, rightEyeLower);
let leftEyeGap = eyeGap(leftEyeUpper, leftEyeLower);
// get box size
let topX = prediction.boundingBox.topLeft[i][0];
let topY = prediction.boundingBox.topLeft[i][1];
let bottomX = prediction.boundingBox.bottomRight[i][0];
let bottomY = prediction.boundingBox.bottomRight[i][1];
let boxSize = bottomY - topY;
let blinkThreshold = 140;
//console.log("Blink Threshold = " + blinkThreshold);
let rUp = prediction.mesh[386][0];
let rDown = prediction.mesh[374][0];
let rGap = rDown - rUp;
console.log("386 = " + rUp);
console.log("374 = " + rDown);
console.log("Gap = " + rGap);
rGap = map (rGap, 0.0, 1.5, 0, 100);
console.log("Gap Mapped = " + rGap);
if (abs(rGap) > blinkThreshold ) {
// Threshold for blink detection; adjust based on your observations
console.log("Blink detected");
} else {
//console.log("No Blink");
}
// let n = map(7, 0, 10, 0, 100);
let noseBottomX = prediction.annotations.noseBottom[i][0];
let noseBottomY = prediction.annotations.noseBottom[i][1];
console.log("NoseBX = " + noseBottomX);
console.log("NoseBY = " + noseBottomY);
textSize(36);
text("Nose Bottom X = " + floor(noseBottomX), 100,100);
}
}
function eyeGap(upper, lower) {
let distance = dist(upper[3][0], upper[3][1], lower[3][0], lower[3][1]);
//console.log(distance);
return distance;
}
function drawKeypoints(){
for (let i = 0; i < predictions.length; i += 1) {
const keypoints = predictions[i].scaledMesh;
// Draw facial keypoints.
for (let j = 0; j < keypoints.length; j += 1) {
const [x, y] = keypoints[j];
fill(0, 255, 0);
ellipse(x, y, 5, 5);
}
}
}