xxxxxxxxxx
190
let video;
let bodyPose;
let poses = [];
let connections;
let gameState = 0;
let shrimpAlert = false;
let meme;
let shimpCount = 0
let startTime = 0
let shimpTime = 0
class slouch {
// all y distances int
constructor(nose, l_eye, r_eye, l_sh, r_sh) {
this.eyesToNose = nose - ((l_eye+r_eye)/2)
this.noseToShoulder = ((l_sh+r_sh)/2) - nose
this.ratio = this.eyesToNose / this.noseToShoulder
this.idealRatio = 0.5
}
checkRatio() {
// console.log(this.eyesToNose, this.noseToShoulder)
// console.log(this.ratio)
if (this.ratio > this.idealRatio) {
shrimpAlert = true
startTime = millis()
meme.play()
shimpCount += 0.08
} else {
shrimpAlert = false
}
}
}
function preload() {
// Load the bodyPose model
bodyPose = ml5.bodyPose();
meme = loadSound('/audio/meme.mp3');
shimp = loadImage('/assets/shimp.png');
}
function setup() {
// createCanvas(1000, 700);
createCanvas(640, 480);
// Create the video and hide it
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
// Start detecting poses in the webcam video
bodyPose.detectStart(video, gotPoses);
// Get the skeletal connection information
connections = bodyPose.getConnections();
}
function draw() {
if (gameState==0) homeScreen()
else if (gameState==1) drawBody();
}
function homeScreen() {
fill(color(232, 146, 150))
rect(0, 0, width, height);
push()
//button action
if (mouseX>width/2-100 && mouseX<width/2+120 && mouseY>height-90 && mouseY<height-30) {
fill(color(102, 146, 10))
if (mouseIsPressed) {
gameState=1
shimpTime = millis();
}
} else {
}
//button
push()
stroke(0, 0, 0);
rect(width/2-100,height-90, 220, 50)
pop()
//shrimp
image(shimp, width/2-150, height/2-150, 300, 280);
// text
fill(color(0,0,0))
textSize(20)
text('shimp: stop slouching', width/2-90, 60);
text('begin the judgment', width/2-80, height-60);
pop()
}
function drawBody() {
// Draw the webcam video
image(video, 0, 0, width, height);
// top text
fill(color(255,255,255))
rect(0, 0, width, 40)
fill(color(0,0,0))
textSize(13)
shimpTime = round((millis() - startTime)/1000)
image(shimp, 10, 10, 20, 20);
text("time since last shimp: "+ shimpTime +" s", 40, 25);
text("go work. we will alert if u shlrimp", width/2-80, 25);
text("shumps: "+ round(shimpCount), width-80, 25);
// // Draw the skeleton connections
// for (let i = 0; i < poses.length; i++) {
// let pose = poses[i];
// for (let j = 0; j < connections.length; j++) {
// let pointAIndex = connections[j][0];
// let pointBIndex = connections[j][1];
// let pointA = pose.keypoints[pointAIndex];
// let pointB = pose.keypoints[pointBIndex];
// // Only draw a line if both points are confident enough
// if (pointA.confidence > 0.1 && pointB.confidence > 0.1) {
// stroke(255, 0, 0);
// strokeWeight(2);
// line(pointA.x, pointA.y, pointB.x, pointB.y);
// }
// }
// }
// Draw all the tracked landmark points
for (let i = 0; i < poses.length; i++) {
let pose = poses[i];
let slouch1 = new slouch(pose.nose.y, pose.left_eye.y, pose.right_eye.y, pose.left_shoulder.y, pose.right_shoulder.y)
slouch1.checkRatio()
if (shrimpAlert) {
//shrimp
image(shimp, width/2-150, height/2-150, 300, 280);
// text
fill(color(0,0,0))
textSize(20)
text('YOU ARE SHRUMP. SIT UP, SLOUCHY', width/2-180, 80);
}
//draw keypoints
// for (let j = 0; j < pose.keypoints.length; j++) {
// let keypoint = pose.keypoints[j];
// // Only draw a circle if the keypoint's confidence is bigger than 0.1
// if (keypoint.confidence > 0.1) {
// fill(0, 255, 0);
// noStroke();
// circle(keypoint.x, keypoint.y, 10);
// }
// }
}
}
// Callback function for when bodyPose outputs data
function gotPoses(results) {
// Save the output to the poses variable
poses = results;
// for (let i = 0; i < poses.length; i++) {
// console.log(poses[i])
//nose - x, y, confidence
// left_eye & right_eye, normalized
// lefts
// }
}