xxxxxxxxxx
132
// p5.js + BRFv4 face tracker (via handsfree.js)
var myHandsfree;
let cowboy;
function preload() {
//cowboy = loadModel('cowboy2.obj');
eyelids = loadModel('eyelids3.obj');
hat = loadModel('hat.obj');
head = loadModel('head.obj');
apple = loadModel('apple.obj');
stem = loadModel('stem.obj');
eyes = loadModel('eyes.obj');
nose = loadModel('nose.obj');
}
//---------------------------------------------
function setup() {
const width = 500;
const height = width;
createCanvas(width, height, WEBGL);
var myConfig = {
hideCursor: true
};
myHandsfree = new Handsfree(myConfig);
myHandsfree.start();
}
//---------------------------------------------
function draw() {
background(0, 0, 0);
if (myHandsfree.isTracking) {
if (myHandsfree.pose.length > 0) {
var face0 = myHandsfree.pose[0].face;
var nPoints = face0.vertices.length;
//fill(255, 0, 0);
for (var i = 0; i < nPoints; i += 2) {
var x = face0.vertices[i + 0];
var y = face0.vertices[i + 1];
ellipse(x, y, 9, 9);
}
// Rotations of the head, in radians
var rx = face0.rotationX; // pitch
var ry = face0.rotationY; // yaw
var rz = face0.rotationZ; // roll
drawModel(rx, ry, rz, face0);
}
}
}
function drawModel(rx, ry, rz, face0) {
let locX = mouseX - height / 2;
let locY = mouseY - width / 2;
pointLight(255, 255, 255, locX, locY, 200);
background(0);
scale(30);
push();
rotateZ(radians(180));
rotateX(rx);
rotateY(-ry);
rotate(-rz);
noStroke();
//normalMaterial();
ambientMaterial(250);
fill(118, 209, 219);
model(head);
fill(130, 48, 88);
model(hat);
fill(252, 186, 3);
model(stem);
specularMaterial(250);
fill(0);
model(eyes);
ambientMaterial(250);
fill(252, 186, 3);
model(nose);
//specularMaterial(100);
fill(148, 37, 52);
model(apple);
ambientMaterial(250);
fill(118, 209, 219);
if (detectBlinks(face0)) {
model(eyelids);
}
pop();
}
function detectBlinks(face0) {
//Based on the work by Soukupová and Čech in their 2016 paper:
//Real-Time Eye Blink Detection using Facial Landmarks
//loop through eye points
//right eye points are 36-41
//find distance btw vertical eye points
//since the list of points goes by 2's (alternating x and y) we must double
A = dist(face0.vertices[74], face0.vertices[75],
face0.vertices[82], face0.vertices[83]);
B = dist(face0.vertices[76], face0.vertices[77],
face0.vertices[80], face0.vertices[81]);
//dist btw horizontal points
C = dist(face0.vertices[72], face0.vertices[73],
face0.vertices[78], face0.vertices[79]);
//ear aspect ratio
var ear = (A + B) / (2.0 * C);
//left eye points are 42-47
A2 = dist(face0.vertices[74], face0.vertices[75],
face0.vertices[82], face0.vertices[83]);
B2 = dist(face0.vertices[76], face0.vertices[77],
face0.vertices[80], face0.vertices[81]);
//dist btw horizontal points
C2 = dist(face0.vertices[72], face0.vertices[73],
face0.vertices[78], face0.vertices[79]);
//ear aspect ratio
var ear2 = (A2 + B2) / (2.0 * C2);
if (ear <= 0.1 || ear2 <= 0.2) {
return true;
}
return false;
}