xxxxxxxxxx
235
let video;
let poseNet;
let pose;
let skeleton;
let img
var w = window.innerWidth;
var h = window.innerHeight;
var left;
var right;
var shouldExit;
var inIkea;
var shouldMeatballs;
var shouldMeatball;
var IkeaHatOn;
var inBirthday;
var shouldCake;
var shouldSlice;
var birthHatOn;
function setup() {
createCanvas(640, 480);
background(0);
video=createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelLoaded);
poseNet.on('pose', gotPoses);
// body parts
head = loadImage('head.png');
leftHand = loadImage('hand.png');
rightHand = loadImage('other-hand.png');
// exit sign
exit = loadImage('exit.png');
// ikea
ikeaHat = loadImage('hats/Ikea-hat.png');
meatball = loadImage("food/meatball.png");
meatballs = loadImage("food/meatballs.png");
// birthday
birthdayHat = loadImage("hats/birthday-boy.png");
pieceOfCake = loadImage("food/cakePiece.png");
cake = loadImage("food/cake.png");
}
function gotPoses(poses) {
if (poses.length > 0) {
pose = poses[0].pose;
skeleton = poses[0].skeleton;
}
}
function modelLoaded() {
console.log('poseNet ready');
}
function draw() {
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 elbowL=pose.leftElbow;
let elbowR=pose.rightElbow;
let wristL=pose.leftWrist;
let wristR=pose.rightWrist;
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;
fill(255,0,0);
imageMode(CORNER);
// CANVAS: 640X480
// IKEA
if((wristR.y < 150 && wristR.x < 150)&&(!inIkea)&&(!inBirthday)){
inIkea = true;
}
if((inIkea) && (wristL.x > 500 && wristL.y < 150)){
inIkea = false;
}
// BIRTHDAY
if((wristR.y < 150&&wristR.x > 490)&&(!inIkea)&&(!inBirthday)) {
inBirthday = true;
}
if((inBirthday) && (wristL.x > 500 && wristL.y < 150)){
inBirthday = false;
}
if(inIkea) {
shouldExit = true;
IkeaHatOn = true;
shouldMeatballs = true;
// pickup meatballs
if((wristL.y>350&&wristL.y<450)&&(wristL.x>270&&wristL.x<370)) {
shouldMeatball = true;
left = true;
}
if((wristR.y>350&&wristR.y<450)&&(wristR.x>270&&wristR.x<370)) {
shouldMeatball = true;
right = true;
}
// eat meatballs
if(shouldMeatball) {
if(right) {
if(dist((wristR.x+(handR*.15)), (wristR.y-(handR*.25)), pose.nose.x, pose.nose.y+(d/2))<(d/2)) {
right = false;
}
}
if(left) {
if(dist((wristL.x-(handL*.15)), (wristL.y-(handL*.25)), pose.nose.x, pose.nose.y+(d/2))<(d/2)) {
left = false;
}
}
}
}
else if(inBirthday) {
shouldExit = true;
birthHatOn = true;
shouldCake = true;
// pickup cake
if((wristL.y>350&&wristL.y<450)&&(wristL.x>270&&wristL.x<370)) {
shouldCake = true;
left = true;
}
if((wristR.y>350&&wristR.y<450)&&(wristR.x>270&&wristR.x<370)) {
shouldCake = true;
right = true;
}
// eat cake
if(shouldSlice) {
if(right) {
if(dist((wristR.x+(handR*.15)), (wristR.y-(handR*.25)), pose.nose.x, pose.nose.y+(d/2))<(d/2)) {
right = false;
}
}
if(left) {
if(dist((wristL.x-(handL*.15)), (wristL.y-(handL*.25)), pose.nose.x, pose.nose.y+(d/2))<(d/2)) {
left = false;
}
}
}
}
else {
shouldExit = false;
left = false;
right = false;
IkeaHatOn = false;
shouldMeatballs = false;
shouldMeatball = false;
birthHatOn = false;
shouldCake = false;
shouldSlice = false;
let ikeaDist = dist(wristL.x, wristL.y, 30, 30);
image(ikeaHat, 0, 0, min(25000/ikeaDist, 125), min(20000/ikeaDist, 100));
let birthDist = dist(wristR.x, wristR.y, 610, 30);
image(birthdayHat, 490, 0, min(30000/birthDist, 125), min(32000/birthDist, 100));
}
imageMode(CENTER)
angleMode(DEGREES)
// Body
image(head, pose.nose.x, pose.nose.y, d*2,d*2);
image(rightHand, wristR.x, wristR.y, handR*.35, handR*.7);
image(leftHand, wristL.x, wristL.y, handL*.35, handL*.7);
// Image Bools
if(shouldExit) {
image(exit, 580, 50, 80, 50);
}
// ikea
if(IkeaHatOn) {
image(ikeaHat, pose.nose.x, pose.nose.y-d,d*2.75, d*1.40);
}
if(shouldMeatballs){
image(meatballs, 320, 400, 150, 150);
}
if(shouldMeatball){
if(right) {
image(meatball, wristR.x+handR*.15, wristR.y-handR*.25, 80, 80);
}
if(left) {
image(meatball, wristL.x-handL*.15, wristL.y-handL*.25, 80, 80);
}
}
// birthday
if(birthHatOn) {
image(birthdayHat, pose.nose.x, pose.nose.y-d,d*2.75, d*1.40);
}
if(shouldCake){
image(cake, 320, 400, 150, 150);
}
if(shouldSlice){
if(right) {
image(pieceOfCake, wristR.x+handR*.15, wristR.y-handR*.25, 80, 80);
}
if(left) {
image(pieceOfCake, wristL.x-handL*.15, wristL.y-handL*.25, 80, 80);
}
}
}
}