xxxxxxxxxx
126
let posenet;
let pose=[];
let myNose=null;
let myRightEye=null;
let myLeftEye=null;
let oNose=null;
let oRightEye=null;
let oLeftEye=null;
let cap = false;
let otherCap = false;
let tracker2 = false;
let pl5;
let otherPositions = [];
function setup() {
createCanvas(800, 800);
//this establishes our p5 library media
// and then we create the gotStream function
cap = createCapture(VIDEO, function (stream) {
p5l = new p5LiveMedia(this, "CAPTURE", stream, "andriProject");
p5l.on("stream", gotStream);
p5l.on("data", gotData);
});
cap.muted = true;
cap.hide();
poseNet = ml5.poseNet(cap, modelLoaded); // modelLoaded is a callback
poseNet.on('pose', gotPoses); // gotPoses is a callback
}
function gotPoses(poses) {
//console.log(poses);
if (poses.length > 0) {
pose = poses[0].pose;
//capturing nose
if (pose.nose.confidence> 0.8) {
myNose=pose.nose;
}
//capture left eye
if (pose.leftEye.confidence> 0.8) {
myLeftEye=pose.leftEye;
}
//capture right eye
if (pose.rightEye.confidence> 0.8) {
myRightEye=pose.rightEye;
}
//we want to send this only when we have all three
if ((myNose != null) && (myLeftEye!=null) && (myRightEye!=null)) {
p5l.send(JSON.stringify({leftEye: pose.leftEye, rightEye: pose.rightEye, nose: pose.nose}));
}
}
}
function modelLoaded() {
console.log('poseNet ready');
}
function gotData(d){
let o = JSON.parse(d)
// console.log(d);
oNose=o.nose;
oRightEye=o.rightEye;
oLeftEye=o.leftEye;
}
function draw() {
background(100);
if ((myNose != null) && (myLeftEye!=null) && (myRightEye!=null)) {
let eyesToEyes=(abs(myLeftEye.x-myRightEye.x));
let e2eh=eyesToEyes/2;
let e2eq=eyesToEyes/4;
let eyesToNose=abs(abs(myLeftEye.y-myRightEye.y)/2-myNose.y);
//image(cap, myNose.x-e2eh, myNose.y,eyesToEyes, eyesToEyes, myNose.x-e2eh, myNose.y,eyesToEyes,eyesToEyes);
image(cap, myLeftEye.x-e2eq, myLeftEye.y-e2eq,e2eh, e2eh, myLeftEye.x-e2eq, myLeftEye.y-e2eq,e2eh,e2eh);
image(cap, myRightEye.x-e2eq, myRightEye.y-e2eq,e2eh, e2eh, myRightEye.x-e2eq, myRightEye.y-e2eq,e2eh,e2eh);
}
//other person
if ((otherCap!=false)&&(oNose != null) && (oLeftEye!=null) && (oRightEye!=null)) {
console.log("nose x: " + oNose.x);
console.log("nose y: " + oNose.y);
let oeyesToEyes=(abs(oLeftEye.x-oRightEye.x));
let oe2eh=oeyesToEyes/2;
image(otherCap, 200,400);
}
}
function olddraw() {
background(100);
var positions = tracker.getCurrentPosition();
if (positions.length > 0) {
p5l.send(JSON.stringify({positions: positions}));
noStroke();
}
}
// We got a new stream!
function gotStream(stream, id) {
// This is just like a video/stream from createCapture(VIDEO)
otherCap = stream;
otherCap.hide();
}