xxxxxxxxxx
235
let video;
let poseNet;
let pose;
let skeleton;
let img
var w = window.innerWidth;
var h = window.innerHeight;
var pickedUp;
var left;
var right;
var shouldExit;
var inIkea;
var shouldMeatballs;
var shouldMeatball;
var shouldIkeaHat;
function setup() {
// IMAGES
//
exit = loadImage('exit.png');
// body parts
head = loadImage('head.png');
leftHand = loadImage('hand.png');
rightHand = loadImage('other-hand.png');
//hats
cowHat = loadImage('hats/cow-hat.png');
ikeaHat = loadImage('hats/Ikea-hat.png');
birthHat = loadImage('hats/birthday-boy.png');
clownHat = loadImage('hats/clown-hat.png');
witchHat = loadImage('hats/witch-hat.png');
// food
meatball = loadImage("food/meatball.png");
meatballs = loadImage("food/meatballs.png");
// backgrounds
ikeaBG = loadImage("backgrounds/IkeaBG.png");
createCanvas(640, 480);
background(0);
video=createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on('pose', gotPoses);
}
function gotPoses(poses) {
// console.log(poses);
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
}
}
function modelLoaded() {
console.log('poseNet ready');
}
function draw() {
// translate(video.width, 0);
// scale(-1, 1);
// image(video,0,0);
background(0);
if (pose) {
let eyeR=pose.rightEye;
let eyeL=pose.leftEye;
let earR=pose.rightEar;
let earL=pose.leftEar;
let nose=pose.nose;
let shoulderR=pose.rightShoulder;
let shoulderL=pose.leftShoulder;
let elbowR=pose.rightElbow;
let elbowL=pose.leftElbow;
let wristR=pose.rightWrist;
let wristL=pose.leftWrist;
let hipR=pose.rightHip;
let hipL=pose.leftHip;
let kneeR=pose.rightKnee;
let kneeL=pose.leftKnee;
let ankleR=pose.rightAnkle;
let ankleL=pose.leftAnkle;
let d=dist(eyeR.x, eyeR.y, eyeL.x, eyeL.y)*1.4;
let handL = dist(wristL.x, wristL.y, elbowL.x, elbowL.y)*1.2;
let handR = dist(wristR.x, wristR.y, elbowR.x, elbowR.y)*1.2;
// DO HAND ANGLING
// right
let wristElbowR = (wristR.x, elbowR.y, wristR.x, elbowR.y);
let elbowShoulderR = (shoulderR.x, elbowR.y, shoulderR.x, elbowR.y);
let wristShoulderR = (wristR.x, shoulderR.y, wristR.x, shoulderR.y);
let numerR = sq(wristElbowR) + sq(elbowShoulderR) - sq(wristShoulderR);
let denomR = 2 * wristElbowR * elbowShoulderR;
let angleR = acos(numerR/denomR);
// left
let wristElbowL = (wristL.x, elbowL.y, wristL.x, elbowL.y);
let elbowShoulderL = (shoulderL.x, elbowL.y, shoulderL.x, elbowR.y);
let wristShoulderL = (wristL.x, shoulderL.y, wristL.x, shoulderL.y);
let numerL = sq(wristElbowL) + sq(elbowShoulderL) - sq(wristShoulderL);
let denomL = 2 * wristElbowL * elbowShoulderL;
let angleL = acos(numerL/denomL);
fill(255,0,0);
imageMode(CORNER)
// Display Pose Points
// for (let i = 0; i < pose.keypoints.length; i++) {
// let x = pose.keypoints[i].position.x;
// let y = pose.keypoints[i].position.y;
// fill(255,255,255);
// ellipse(x,y,10,10);
// }
// Display Skeleton
// for (let i = 0; i < skeleton.length; i++) {
// let a = skeleton[i][0];
// let b = skeleton[i][1];
// strokeWeight(2);
// stroke(255);
// line(a.position.x, a.position.y,b.position.x,b.position.y);
// }
// Ikea
if(wristR.y < 175 && wristR.x < 175){
inIkea = true;
}
if((inIkea) && (wristL.x > 500 && wristL.y < 200)){
inIkea = false;
}
if(inIkea) {
// background(ikeaBG);
shouldExit = true;
// IKEA HAT CONTROLS
shouldIkeaHat = true;
// If hand picks hat up to end Ikea
// if(((wristR.x>pose.nose.x-d*.72 && (wristR.x<pose.nose.x+d*.72))&&(wristR.y>pose.nose.y-d*1.35 && (wristR.y<pose.nose.y+d*1.35)))&&(!shouldMeatball)) {
// pickedUp = true;
// }
// Ends Ikea when hat put away
// if(wristR.y < 175 && wristR.x < 175) {
// pickedUp = false;
// }
// MEATBALL CONTROLS
shouldMeatballs = true;
// If meatball picked up (left hand)
if((wristL.y>390&&wristL.y<410)&&(wristL.x>290&&wristL.x<310)) {
shouldMeatball = true;
left = true;
}
// If meatball picked up (right hand)
if((wristR.y>390&&wristR.y<410)&&(wristR.x>290&&wristR.x<310)) {
shouldMeatball = true;
right = true;
}
// Eating the meatball
if(shouldMeatball) {
// If left eats
if(dist((wristL.x-(handL*.15)-5), (wristL.y-(handL*.7)-10), pose.nose.x, pose.nose.y)<d/1.4) {
if(right) {
left = false;
} else {
left = false;
shouldMeatball = false;
}
}
// If right eats
if(dist((wristR.x-(handR*.15)-5), (wristR.y-(handR*.7)-10), pose.nose.x, pose.nose.y)<d/1.4) {
if(left) {
right = false;
} else {
right = false;
shouldMeatball = false;
}
}
}
} else {
// reset Ikea
let ikeaDist = dist(wristR.x, wristR.y, 30, 30);
image(ikeaHat, 0, 0, min(25000/ikeaDist, 125), min(20000/ikeaDist, 100));
shouldMeatballs = false;
shouldMeatball = false;
shouldIkeaHat = false;
pickedUp = false;
shouldExit = false;
}
// Body
imageMode(CENTER)
angleMode(DEGREES)
image(head, pose.nose.x, pose.nose.y, d*2,d*2);
image(leftHand, wristL.x, wristL.y, handL*.35, handL*.7);
image(rightHand, wristR.x, wristR.y, handR*.35, handR*.7);
// IMAGE BOOLS
if(shouldExit) {
image(exit, 600, 100, 80, 50);
}
if(shouldMeatballs){
image(meatballs, 320, 400, 150, 150);
}
if(shouldMeatball && left) {
image(meatball, wristL.x-handL*.15, wristL.y-handL*.25, 80, 80);
}
if(shouldMeatball && right) {
image(meatball, wristR.x-handR*.15, wristR.y-handR*.25, 80, 80);
}
if(shouldIkeaHat) {
if(pickedUp) {
image(ikeaHat, wristR.x, wristR.y-handR*.15,d*2.78, d*1.44);
} else {
image(ikeaHat, pose.nose.x, pose.nose.y-d,d*2.78, d*1.44);
}
}
}
}