xxxxxxxxxx
345
//define variables
let facemesh;
let video;
let predictions = [];
var leftpixel = 0;
var middlepixel = 0;
var rightpixel = 0;
var ims = [];
var experiment = false;
var ready = false;
//set-up workspace and camera
function setup() {
createCanvas(480, 640);
video = createCapture(VIDEO);
video.size(width, height);
background(255);
textSize(18);
text("Please wait for the project to load...", 20, 20);
frameRate(10);
startimage= loadImage("start/startimage.png");
//load images
let images = 61;
for (let i = 1; i <= images; i++) {
path = 'images/' + str(i) + '.jpg';
loaded_image = loadImage(path);
ims.push(loaded_image);
}
//create facemesh
facemesh = ml5.facemesh(video, modelReady);
facemesh.on("predict", results => {
predictions = results;
});
// Hide the video element, and just show the canvas
video.hide();
}
//when model is ready get a message
function modelReady() {
console.log("Model ready!");
image(startimage,0,0,width, height*0.6);
ready = true;
}
//load starting image
function draw() {
if(experiment && ready)
// We can call this function to draw all keypoints
{drawKeypoints();}
}
function keyPressed() {
// This function is automatically called when a keyboard-key is pressed
if (keyCode == ENTER) {
if (!experiment) { // first time, after that ignore
frameCount = 0;
experiment = true;
console.log("experiment start");
background(128);
timestamp=millis();
}
}
}
// Measure difference between right ear to nose and left ear to nose
function drawKeypoints() {
var faceornot = 0;
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 keypoint = keypoints[j];
const [x,y] = keypoint;
//text(j,keypoint[0], keypoint[1])
if (j == 93)
{leftpixel = x;}
else if(j == 323)
{rightpixel = x;}
else if(j == 1)
{middlepixel = x;}
}
}
//calculate ratio between left and right part of the face
var testleft = (middlepixel - leftpixel);
var testright = (rightpixel - middlepixel);
var distance = testleft/testright;
//call image based on how far face is turned
if (distance> Math.pow(1.05, -1) && distance < Math.pow(1.05,1) )
{image(ims[0], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 1) && distance < Math.pow(1.05, 2))
{image(ims[1], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -1) && distance > Math.pow(1.05, -2))
{image(ims[1], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 2) && distance < Math.pow(1.05, 3))
{image(ims[2], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -2) && distance > Math.pow(1.05, -3))
{image(ims[2], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 3) && distance < Math.pow(1.05, 4))
{image(ims[3], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -3) && distance > Math.pow(1.05, -4))
{image(ims[3], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 4) && distance < Math.pow(1.05, 5))
{image(ims[4], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -4) && distance > Math.pow(1.05, -5))
{image(ims[4], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 5) && distance < Math.pow(1.05, 6))
{image(ims[5], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -5) && distance > Math.pow(1.05, -6))
{image(ims[5], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 6) && distance < Math.pow(1.05, 7))
{image(ims[6], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -6) && distance > Math.pow(1.05, -7))
{image(ims[6], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 7) && distance < Math.pow(1.05, 8))
{image(ims[7], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -7) && distance > Math.pow(1.05, -8))
{image(ims[8], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 8) && distance < Math.pow(1.05, 9))
{image(ims[8], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -8) && distance > Math.pow(1.05, -9))
{image(ims[9], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 9) && distance < Math.pow(1.05, 10))
{image(ims[9], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -9) && distance > Math.pow(1.05, -10))
{image(ims[10], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 10) && distance < Math.pow(1.05, 11))
{image(ims[10], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -10) && distance > Math.pow(1.05, -11))
{image(ims[11], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 11) && distance < Math.pow(1.05, 12))
{image(ims[11], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -11) && distance > Math.pow(1.05, -12))
{image(ims[12], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 12) && distance < Math.pow(1.05, 13))
{image(ims[12], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -12) && distance > Math.pow(1.05, -13))
{image(ims[13], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 13) && distance < Math.pow(1.05, 14))
{image(ims[13], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -13) && distance > Math.pow(1.05, -14))
{image(ims[14], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 14) && distance < Math.pow(1.05, 15))
{image(ims[14], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -14) && distance > Math.pow(1.05, -15))
{image(ims[15], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 15) && distance < Math.pow(1.05, 16))
{image(ims[1], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -15) && distance > Math.pow(1.05, -16))
{image(ims[15], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 16) && distance < Math.pow(1.05, 17))
{image(ims[16], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -16) && distance > Math.pow(1.05, -17))
{image(ims[16], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 17) && distance < Math.pow(1.05, 18))
{image(ims[17], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -17) && distance > Math.pow(1.05, -18))
{image(ims[17], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 18) && distance < Math.pow(1.05, 19))
{image(ims[18], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -18) && distance > Math.pow(1.05, -19))
{image(ims[18], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 19) && distance < Math.pow(1.05, 20))
{image(ims[19], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -19) && distance > Math.pow(1.05, -20))
{image(ims[19], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 20) && distance < Math.pow(1.05, 21))
{image(ims[20], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -20) && distance > Math.pow(1.05, -21))
{image(ims[20], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 21) && distance < Math.pow(1.05, 22))
{image(ims[21], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -21) && distance > Math.pow(1.05, -22))
{image(ims[21], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 22) && distance < Math.pow(1.05, 23))
{image(ims[22], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -22) && distance > Math.pow(1.05, -23))
{image(ims[22], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 23) && distance < Math.pow(1.05, 24))
{image(ims[23], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -23) && distance > Math.pow(1.05, -24))
{image(ims[23], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 24) && distance < Math.pow(1.05, 25))
{image(ims[24], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -24) && distance > Math.pow(1.05, -25))
{image(ims[24], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 25) && distance < Math.pow(1.05, 26))
{image(ims[25], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -25) && distance > Math.pow(1.05, -26))
{image(ims[25], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 26) && distance < Math.pow(1.05, 27))
{image(ims[26], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -26) && distance > Math.pow(1.05, -27))
{image(ims[26], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 27) && distance < Math.pow(1.05, 28))
{image(ims[27], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -27) && distance > Math.pow(1.05, -28))
{image(ims[27], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 28) && distance < Math.pow(1.05, 29))
{image(ims[28], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -28) && distance > Math.pow(1.05, -29))
{image(ims[28], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 29) && distance < Math.pow(1.05, 30))
{image(ims[29], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -29) && distance > Math.pow(1.05, -30))
{image(ims[29], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 30) && distance < Math.pow(1.05, 31))
{image(ims[30], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -30) && distance > Math.pow(1.05, -31))
{image(ims[30], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 31) && distance < Math.pow(1.05, 32))
{image(ims[31], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -31) && distance > Math.pow(1.05, -32))
{image(ims[31], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 32) && distance < Math.pow(1.05, 33))
{image(ims[32], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -32) && distance > Math.pow(1.05, -33))
{image(ims[32], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 33) && distance < Math.pow(1.05, 34))
{image(ims[33], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -33) && distance > Math.pow(1.05, -34))
{image(ims[33], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 34) && distance < Math.pow(1.05, 35))
{image(ims[34], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -34) && distance > Math.pow(1.05, -35))
{image(ims[34], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 35) && distance < Math.pow(1.05, 36))
{image(ims[35], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -35) && distance > Math.pow(1.05, -36))
{image(ims[35], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 36) && distance < Math.pow(1.05, 37))
{image(ims[36], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -36) && distance > Math.pow(1.05, -37))
{image(ims[36], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 37) && distance < Math.pow(1.05, 38))
{image(ims[37], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -37) && distance > Math.pow(1.05, -38))
{image(ims[37], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 38) && distance < Math.pow(1.05, 39))
{image(ims[38], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -38) && distance > Math.pow(1.05, -39))
{image(ims[38], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 39) && distance < Math.pow(1.05, 40))
{image(ims[39], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -39) && distance > Math.pow(1.05, -40))
{image(ims[39], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 40) && distance < Math.pow(1.05, 41))
{image(ims[40], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -40) && distance > Math.pow(1.05, -41))
{image(ims[40], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 41) && distance < Math.pow(1.05, 42))
{image(ims[41], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -41) && distance > Math.pow(1.05, -42))
{image(ims[41], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 42) && distance < Math.pow(1.05, 43))
{image(ims[42], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -42) && distance > Math.pow(1.05, -43))
{image(ims[42], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 43) && distance < Math.pow(1.05, 44))
{image(ims[43], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -43) && distance > Math.pow(1.05, -44))
{image(ims[43], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 44) && distance < Math.pow(1.05, 45))
{image(ims[44], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -44) && distance > Math.pow(1.05, -45))
{image(ims[44], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 45) && distance < Math.pow(1.05, 46))
{image(ims[45], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -45) && distance > Math.pow(1.05, -46))
{image(ims[45], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 46) && distance < Math.pow(1.05, 47))
{image(ims[46], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -46) && distance > Math.pow(1.05, -47))
{image(ims[46], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 47) && distance < Math.pow(1.05, 48))
{image(ims[47], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -47) && distance > Math.pow(1.05, -48))
{image(ims[47], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 48) && distance < Math.pow(1.05, 49))
{image(ims[48], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -48) && distance > Math.pow(1.05, -49))
{image(ims[48], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 49) && distance < Math.pow(1.05, 50))
{image(ims[49], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -49) && distance > Math.pow(1.05, -50))
{image(ims[49], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 50) && distance < Math.pow(1.05, 51))
{image(ims[50], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -50) && distance > Math.pow(1.05, -51))
{image(ims[50], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 51) && distance < Math.pow(1.05, 52))
{image(ims[51], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -51) && distance > Math.pow(1.05, -52))
{image(ims[51], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 52) && distance < Math.pow(1.05, 53))
{image(ims[52], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -52) && distance > Math.pow(1.05, -53))
{image(ims[52], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 53) && distance < Math.pow(1.05, 54))
{image(ims[53], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -53) && distance > Math.pow(1.05, -54))
{image(ims[53], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 54) && distance < Math.pow(1.05, 55))
{image(ims[54], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -54) && distance > Math.pow(1.05, -55))
{image(ims[54], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 55) && distance < Math.pow(1.05, 56))
{image(ims[55], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -55) && distance > Math.pow(1.05, -56))
{image(ims[55], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 56) && distance < Math.pow(1.05, 57))
{image(ims[56], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -56) && distance > Math.pow(1.05, -57))
{image(ims[56], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 57) && distance < Math.pow(1.05, 58))
{image(ims[57], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -57) && distance > Math.pow(1.05, -58))
{image(ims[57], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 58) && distance < Math.pow(1.05, 59))
{image(ims[58], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -58) && distance > Math.pow(1.05, -59))
{image(ims[58], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 59) && distance < Math.pow(1.05, 60))
{image(ims[59], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -59) && distance > Math.pow(1.05, -60))
{image(ims[59], 0, 0, width, height);}
else if( distance >= Math.pow(1.05, 60))
{image(ims[60], 0, 0, width, height);}
else if( distance <= Math.pow(1.05, -60))
{image(ims[60], 0, 0, width, height);}
}