xxxxxxxxxx
105
let facemesh;
let video;
let predictions = [];
let canvas_w = 800;
let canvas_h = 600;
let video_w = 640;
let video_h = 480;
let margin_left = 50;
let margin_top = 50;
function setup() {
createCanvas(canvas_w, canvas_h);
video = createCapture(VIDEO);
video.size(video_w, video_h);
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();
background("purple");
}
function modelReady() {
console.log("Model ready!");
}
function draw() {
drawBackground();
image(video, margin_left, margin_top, video_w, video_h);
// We can call both functions to draw all keypoints
drawKeypoints();
drawForeground();
}
function star(x, y, radius1, radius2, npoints) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius2;
let sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}
function drawForeground() {
//circle(20, 20, random(width));
translate(width/2, height/2);
rotate(map(mouseX, 0, width, 0, 360) / 3.0);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 5);
rotate(map(mouseY, 0, width, 0, 180) / 3.5);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 6);
translate(width / 2, height / 2);
rotate(map(mouseY, 0, width, 0, 360) / 4.0);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 7);
rotate(map(mouseX, 0, width, 0, 180) / 4.5);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 8);
translate(width / 2, height / 2);
rotate(map(mouseX, 0, width, 0, 360) / 5.0);
star(100, 100, map(mouseX, 0, width, 0, 60), map(mouseY, 0, width, 0, 100), 9);
}
function drawBackground() {
background("rgb(0,0,0)");
rotate(map(mouseX, 0, width, 0, 360) / 0);
star(0, 0, map(mouseX, 0, width, 0, 400), map(mouseY, 0, width, 0, 600), 9);
star(800, 0, map(mouseX, 0, width, 0, 200), map(mouseY, 0, width, 0, 400), 8);
star(0, 600, map(mouseX, 0, width, 0, 200), map(mouseY, 0, width, 0, 400), 9);
star(800, 600, map(mouseX, 0, width, 0, 300), map(mouseY, 0, width, 0, 400), 9);
//rect(random(width), 0, map(mouseX, 0, width, 10, 100), height);
}
// A function to draw ellipses over the detected keypoints
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];
push();
translate(margin_left, margin_top);
fill(255, 255, 255);
star(x, y, map(mouseX, 0, width, 0, 10),map(mouseY, 0, width, 0, 10),4);
pop();
}
}
}