xxxxxxxxxx
113
var video;
var slider;
let seriously;
let poseNet;
let poses = [];
let numReadings = 100;
let readings = []; // the readings from the analog input
let readIndex = 0; // the index of the current reading
let total = 0; // the running total
let average = 0; // the average
let amount = 0;
function setup() {
canvas = createCanvas(640, 480, WEBGL);
canvas.position(windowWidth / 2 - 640 / 2, windowHeight / 2 - 480 / 2);
canvas.id("p5canvas");
video = createCapture(VIDEO);
video.size(640, 480);
video.id("p5video");
video.hide();
pixelDensity(1);
for (let thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
// Create a new poseNet method with a single detection
poseNet = ml5.poseNet(video);
// This sets up an event that fills the global variable "poses"
// with an array every time new poses are detected
poseNet.on("pose", function (results) {
poses = results;
});
seriously = new Seriously();
var target = seriously.target("#p5canvas");
let reformat = seriously.transform("reformat");
canvas.width = 640 * devicePixelRatio;
canvas.height = 480 * devicePixelRatio;
//set up reformat parameters
reformat.source = "#p5video";
reformat.width = canvas.width;
reformat.height = canvas.height;
blur = seriously.effect("blur");
blur.source = reformat;
blur.amount = "#amount";
target.source = blur;
seriously.go(function (now) {
// callback runs before render
let check = map(getDistanceBetweenEyes(), 50, 150, 0, 1);
if (check) {
amount = check;
}
blur.amount = amount;
});
}
function draw() {}
function getDistanceBetweenEyes() {
if (poses.length >= 1) {
if (
poses[0].pose.leftEye != undefined &&
poses[0].pose.rightEye != undefined
) {
if (
poses[0].pose.leftEye.confidence > 0.8 &&
poses[0].pose.rightEye.confidence > 0.8
) {
//Smoothing function
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = dist(
poses[0].pose.leftEye.x,
poses[0].pose.leftEye.y,
poses[0].pose.rightEye.x,
poses[0].pose.rightEye.y
);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
return average;
}
}
}
}