xxxxxxxxxx
132
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(windowWidth, windowHeight);
//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)
oNose=o.nose;
oNose.x*=.5;
oNose.y*=.5;
oRightEye=o.rightEye;
oRightEye.x*=.5;
oRightEye.y*=.5;
oLeftEye=o.leftEye;
oLeftEye.x*=.5;
oLeftEye.y*=.5;
}
function drawFunnyFace(dx, dy, leftEyePos, rightEyePos, nosePos, capImage){
push();
if ((leftEyePos!=null) && (rightEyePos!=null) && (nosePos!=null) && (capImage)) {
translate(-leftEyePos.x+dx, -leftEyePos.y+dy);
scale(2,2);
let eyesToEyes=(abs(leftEyePos.x-rightEyePos.x));
let e2eh=eyesToEyes/2;
let e2eq=eyesToEyes/4;
let eyesToNose=abs(abs(leftEyePos.y-rightEyePos.y)/2-nosePos.y);
image(capImage, leftEyePos.x-e2eq, leftEyePos.y-e2eq,e2eh, e2eh, leftEyePos.x-e2eq, leftEyePos.y-e2eq,e2eh,e2eh);
image(capImage, rightEyePos.x-e2eq, rightEyePos.y-e2eq,e2eh, e2eh, rightEyePos.x-e2eq, rightEyePos.y-e2eq,e2eh,e2eh);
image(capImage, nosePos.x-e2eh, nosePos.y,eyesToEyes, eyesToEyes, nosePos.x-e2eh, nosePos.y,eyesToEyes,eyesToEyes);
}
pop();
}
function draw() {
background(0);
drawFunnyFace(100,100, myLeftEye,myRightEye, myNose, cap);
drawFunnyFace(300, 100, oLeftEye,oRightEye, oNose, otherCap);
console.log(otherCap.width, otherCap.height, cap.width, cap.height);
}
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();
}