Hi , I am exploring then ml5 and posenet . I am having some issues in calculating the displacement of keypoints ( nose , in my case) . I can get the keypoints logged out but my logic on calculating the displacement of nose keypoint in the x , y axis seem to be wrong . I am trying to calculate the velocity
from the formula (sqrt(x2-x1)²+(y2-ya)²)/dt
. Following is my code snippet .
let video;
let poseNet;
let poses = [];
let skeletons = [];
var displacementNoseX =[];
let noseX;
let noseY;
let pNoseX;
let pNoseY;
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(width, height);
pixelDensity(1);
pg = createGraphics(width, height);
// Create a new poseNet method with a single detection
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', function(results) {
poses = results;
});
// Hide the video element, and just show the canvas
video.hide();
}
function draw() {
image(video, 0, 0, width, height);
// We can call both functions to draw all keypoints and the skeletons
drawKeypoints();
//drawSkeleton();
}
// A function to draw ellipses over the detected keypoints
function drawKeypoints() {
// Loop through all the poses detected
for (let i = 0; i < min(poses.length, 1); i++) {
// For each pose detected, loop through all the keypoints
for (let j = 0; j < poses[i].pose.keypoints.length; j++) {
// A keypoint is an object describing a body part (like rightArm or leftShoulder)
let keypoint = poses[i].pose.keypoints[j];
// Only draw an ellipse is the pose probability is bigger than 0.2
if (keypoint.score > 0.2) {
if (j == 0) {
noseX = keypoint.position.x;
noseY = keypoint.position.y;
// displacement x2-x1
pNoseX = noseX;
pNoseY = noseY;
displacementNoseX = pNoseX-noseX
console.log(displacementNoseX)
// Results in 0 displacement
// Appending element in the array
//displacementNoseX = displacementNoseX.concat(displacementNoseX);
//Error
}
}
}
}
}
// A function to draw the skeletons
function drawSkeleton() {
// Loop through all the skeletons detected
for (let i = 0; i < poses.length; i++) {
// For every skeleton, loop through all body connections
for (let j = 0; j < poses[i].skeleton.length; j++) {
let partA = poses[i].skeleton[j][0];
let partB = poses[i].skeleton[j][1];
stroke(255, 0, 0);
line(partA.position.x, partA.position.y, partB.position.x, partB.position.y);
}
}
}
// The callback that gets called every time there's an update from the model
function gotPoses(results) {
poses = results;
}
function keyPressed() {
pg.clear();
}
function modelReady() {
select('#status').html('model Loaded');
}