let averageConfidence = .2
const maxPoseArrayLength = 10;
let startingPose = false;
let yellowGuideDotX = screenX * 0.351;
let yellowGuideDotY = screenY / 1.8;
let blueGuideDotX = screenX * 0.64;
let blueGuideDotY = screenY / 1.8;
createCanvas(screenX, screenY);
video = createCapture(VIDEO);
video.size(width, height);
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', function (results) {
melodyOne = loadSound('melodyOne.wav');
melodyTwo = loadSound('melodyTwo.wav');
snare = loadSound('snare.wav');
snareTwo = loadSound('snareTwo.wav');
brightkick = loadSound('brightkick.wav');
redMan4 = loadImage('redMan4.png');
console.log("modelReady")
background(120, 100, 80);
image(video, 0, 0, width, height);
function getBestPose(candidateResults) {
let currentBestResult = candidateResults[0];
if (currentBestResult === undefined) {
candidateResults.forEach(candidate => {
if (candidate.pose.score > currentBestResult.pose.score) {
currentBestResult = candidate;
return currentBestResult.pose;
function getWristPositionAverageRightWrist() {
recentPoses.forEach(recentP => {
averageRightX += recentP.rightWrist.x
averageRightY += recentP.rightWrist.y
averageRightX = averageRightX / recentPoses.length
averageRightY = averageRightY / recentPoses.length
return { rightX: averageRightX, rightY: averageRightY }
function getWristPositionAverageLeftWrist() {
recentPoses.forEach(recentP => {
averageLeftX += recentP.leftWrist.x
averageLeftY += recentP.leftWrist.y
averageLeftX = averageLeftX / recentPoses.length
averageLeftY = averageLeftY / recentPoses.length
return { leftX: averageLeftX, leftY: averageLeftY }
if (startingPose === true) {
image(redMan4, width / 4, height / 7, 0, 0)
ellipse(yellowGuideDotX, yellowGuideDotY, 20);
ellipse(blueGuideDotX, blueGuideDotY, 20);
const wristPointLeft = getWristPositionAverageLeftWrist();
const wristPointRight = getWristPositionAverageRightWrist();
let distanceBetweenYellowDots = (dist(yellowGuideDotX, yellowGuideDotY, wristPointRight.rightX, wristPointRight.rightY));
let distanceBetweenBlueDots = (dist(blueGuideDotX, blueGuideDotY, wristPointLeft.leftX, wristPointLeft.leftY));
if (distanceBetweenYellowDots < 10 && distanceBetweenBlueDots < 10) {
if (poses.length === 0) {
const bestPose = getBestPose(poses);
const wristPointLeft = getWristPositionAverageLeftWrist();
const wristPointRight = getWristPositionAverageRightWrist();
if (recentPoses.length > maxPoseArrayLength) {
&& bestPose.rightWrist) {
recentPoses.push(bestPose);
ellipse(wristPointLeft.leftX, wristPointLeft.leftY, 30);
&& bestPose.rightWrist) {
ellipse(wristPointRight.rightX, wristPointRight.rightY, 30);
function melodySounds() {
const wristPointLeft = getWristPositionAverageLeftWrist();
if (startingPose === true
&& wristPointLeft.leftY <= height / 3
&& !melodyOne.isPlaying()) {
if (startingPose === true) {
line(0, height / 3, width * 0.4, height / 3)
line(width * 0.6, height / 3, width, height / 3)
line(0, height * 0.7, width * 0.4, height * 0.7)
line(width * 0.6, height * 0.7, width, height * 0.7)
const wristPointRight = getWristPositionAverageRightWrist();
const wristPointLeft = getWristPositionAverageLeftWrist();
if (startingPose === true
&& wristPointRight.rightY <= height / 3
if (startingPose === true
&& wristPointRight.rightY >= height * 0.7
&& !brightkick.isPlaying()) {
if (startingPose === true
&& wristPointLeft.leftY >= height * 0.7
&& !snareTwo.isPlaying()) {