xxxxxxxxxx
139
//instruction:
//Turn on camera, get closer to the camera you will see the sand come out
//Click on the screen if you want to save the image.
let video;
let poseNet;
let noseX = 0;
let noseY = 0;
let eyelX = 0;
let eyelY = 0;
var drawLength = 1500;
var noiseScale = 0.0005;
var strokeLength = 0.1;
var imgNames = ["NYC.jpg"];
var imgs = [];
var imgIndex = -1;
var frame;
function preload() {
for (let i = 0; i < imgNames.length; i++) {
let newImg = loadImage(imgNames[i]);
imgs.push(newImg);
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', gotPoses);
background(255);
changeImage();
}
function gotPoses(poses) {
// console.log(poses);
if (poses.length > 0) {
let nX = poses[0].pose.keypoints[0].position.x;
let nY = poses[0].pose.keypoints[0].position.y;
let eX = poses[0].pose.keypoints[1].position.x;
let eY = poses[0].pose.keypoints[1].position.y;
noseX = lerp(noseX, nX, 0.5);
noseY = lerp(noseY, nY, 0.5);
eyelX = lerp(eyelX, eX, 0.5);
eyelY = lerp(eyelY, eY, 0.5);
}
} //get the absolute distance between your nose and eyes
function modelReady() {
console.log('model ready');
}
function draw() {
let d = dist(noseX, noseY, eyelX, eyelY); //this reflect the distance between you and the camera
console.log(d);
if (frame > drawLength) {
return;
}
let img = imgs[imgIndex];
if (d > 100) {
img.loadPixels();
translate(width / 2 - img.width / 2, height / 2 - img.height / 2);
let count = map(frame, 0, drawLength, 2, 80);
for (let i = 0; i < count; i++) {
let x = int(random(img.width));
let y = int(random(img.height));
let index = (y * img.width + x) * 4;
let r = img.pixels[index];
let g = img.pixels[index + 1];
let b = img.pixels[index + 2];
let a = img.pixels[index + 3];
stroke(r, g, b, a);
let sw = map(frame, 0, drawLength, 0.5, 5);
strokeWeight(sw);
push();
translate(x, y)
let lengthVariation = random(0.75, 1.25);
line(0, 0, strokeLength * lengthVariation, 0);
pop();
}
frame++;
}
}
function changeImage() {
frame = 0;
noiseSeed(int(random(1000)));
imgIndex++;
if (imgIndex >= imgNames.length) {
imgIndex = 0;
}
}
function mousePressed() {
saveCanvas("Your Impression of NYC", "jpg");
}