Hi , I am trying exploring the ml5 library implementation of posenet. I am trying to implement a animation where a ball is rolling between the left ear and right ear in a straight line.
For this I have done the following
- extracted the ear co -ordinates
- Joined them with a line
- Created an ellipse ball at the right ear which is the starting point and a constant speed of 1
- I noticed that the ellipse that is created to move from left to right does not really move and seems to be fluctuating between at the ear keypoint.
I am not sure what is the reason and what could the best way to solve the problem and to me the logic seems to be correct and I am quite confused where I have gone wrong .
The sketch is below
let video;
let poseNet;
let pose;
let skeleton;
let endpoint
// Ball exepriments moving
var startpoint
var xspeed =1;
function setup() {
createCanvas(640, 480);
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() {
image(video, 0, 0);
if (pose ) {
let eyeR = pose.rightEye;
let eyeL = pose.leftEye;
let earL = pose.leftEar;
let earR = pose.rightEar;
let d = dist(eyeR.x, eyeR.y, eyeL.x, eyeL.y);
// Draw the two points , start point and end point
stroke(0,100,10);
ellipse(earR.x, earR.y,10,10)
ellipse(earL.x,earL.y,10,10)
//Draw a line between start and end point
stroke(255);
line(earR.x, earR.y, earL.x,earL.y);
// Ellipse / ball moves from left to right and right to left when it reaches endpoint
// Output the ball does not move .
startpoint = earR.x;
startpoint = earR.x +xspeed ;
/*
To move it back and forth
if (startpoint == eaR.x) {
xpeed *=-1
}
if (startpoint >= earR.x){
xspeed *= -1
}
*/
console.log(startpoint)
ellipse(startpoint, 100, 20,20) // Y cor ordinate is just to test
for (let i = 0; i < pose.keypoints.length; i++) {
let x = pose.keypoints[i].position.x;
let y = pose.keypoints[i].position.y;
//fill(0,255,0);
//ellipse(x,y,16,16);
}
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);
}
}
}
the index.html file
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/addons/p5.sound.min.js"></script>
<script src="https://unpkg.com/ml5@0.4.2/dist/ml5.min.js"></script>
<meta charset="utf-8" />
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>
Any tips / suggestions would be highly appreciated .