xxxxxxxxxx
269
/* Posenet and Gestures - Nadine, Priya, Jignesh
*/
let video;
let poseNet;
let poses = [];
// tracking nose
let noseX = 0;
let noseY = 0;
// tracking left eye
let lEyeX = 0;
let lEyeY = 0;
// tracking right eye
let rEyeX = 0;
let rEyeY = 0;
// tracking left wrist
let lWristX = 0;
let lWristY = 0;
// tracking right wrist
let rWristX = 0;
let rWristY = 0;
// tracking left shoulder
let lShldX = 0;
let lShldY = 0;
// tracking right shoulder
let rShldX = 0;
let rShldY = 0;
// tracking left elbow
let lElbX = 0;
let lElbY = 0;
// tracking right elbow
let rElbX = 0;
let rElbY = 0;
let dataServer;
let pubKey = 'pub-c-b5a5a079-abb5-4ab9-9d14-808586f68e46';
let subKey = 'sub-c-b29084da-0184-11ea-ac43-f2bd9f9a442a';
//assign channel name. can be anything
let channelName = "sayStuff";
let incomingText = ""; //variable that will hold the incoming message text
let whoAreYou;
//assign gesture variables
let gestureactive = false;
let currentGesture = "";
let prevGesture = "";
let gestureReceived;
let whosent;
let myid;
function setup() {
createCanvas(windowWidth, windowHeight);
background(255);
// posenet code
video = createCapture(VIDEO);
video.hide();
poseNet = ml5.poseNet(video, modelReady);
poseNet.on('pose', gotPoses);
function gotPoses(poses) {
if (poses.length > 0) {
// TRACKING POINTS WITH LERP
// nose
let nX = poses[0].pose.keypoints[0].position.x;
let nY = poses[0].pose.keypoints[0].position.y;
// left eye
let leX = poses[0].pose.keypoints[1].position.x;
let leY = poses[0].pose.keypoints[1].position.y;
// right eye
let reX = poses[0].pose.keypoints[2].position.x;
let reY = poses[0].pose.keypoints[2].position.y;
// left wrist
let lwX = poses[0].pose.keypoints[9].position.x;
let lwY = poses[0].pose.keypoints[9].position.y;
// right wrist
let rwX = poses[0].pose.keypoints[10].position.x;
let rwY = poses[0].pose.keypoints[10].position.y;
// left shoulder
let lsX = poses[0].pose.keypoints[5].position.x;
let lsY = poses[0].pose.keypoints[5].position.y;
// right shoulder
let rsX = poses[0].pose.keypoints[6].position.x;
let rsY = poses[0].pose.keypoints[6].position.y;
// left elbow
let lebX = poses[0].pose.keypoints[7].position.x;
let lebY = poses[0].pose.keypoints[7].position.y;
// right elbow
let rebX = poses[0].pose.keypoints[8].position.x;
let rebY = poses[0].pose.keypoints[8].position.y;
// motion smoothing using lerp
noseX = lerp(noseX, nX, 0.5);
noseY = lerp(noseY, nY, 0.5);
lEyeX = lerp(lEyeX, leX, 0.5);
lEyeY = lerp(lEyeY, leY, 0.5);
rEyeX = lerp(rEyeX, reX, 0.5);
rEyeY = lerp(rEyeY, reY, 0.5);
lWristX = lerp(lWristX, lwX, 0.5);
lWristY = lerp(lWristY, lwY, 0.5);
rWristX = lerp(rWristX, rwX, 0.5);
rWristY = lerp(rWristY, rwY, 0.5);
lShldX = lerp(lShldX, lsX, 0.5);
lShldY = lerp(lShldY, lsY, 0.5);
rShldX = lerp(rShldX, rsX, 0.5);
rShldY = lerp(rShldY, rsY, 0.5);
lElbX = lerp(lElbX, lebY, 0.5);
lElbY = lerp(lElbY, lebY, 0.5);
rElbX = lerp(rElbX, rebY, 0.5);
rElbY = lerp(rElbY, rebY, 0.5);
}
}
function modelReady() {
console.log('model ready');
}
// initialize pubnub
dataServer = new PubNub({
publish_key: pubKey,
subscribe_key: subKey,
ssl: true,
uuid: "Priya" // name of my UUID
});
myid = dataServer.getUUID();
console.log(myid);
//attach callbacks to the pubnub object to handle messages and connections
dataServer.addListener({
message: readIncoming
});
dataServer.subscribe({
channels: [channelName]
});
//create the text fields for the message to be sent
sendText = createInput();
sendText.position(5, height - 100);
whoAreYou = createInput('Name');
whoAreYou.position(5, height - 120);
sendButton = createButton('Post Message');
sendButton.position(sendText.x + sendText.width, height - 100);
sendButton.mousePressed(sendTheMessage);
fill(0, 100);
}
function draw()
//posenet draw
{
background(255);
//noStroke();
video.size(640, 480);
image(video, 0, 0);
fill(255, 0, 0);
stroke(255, 0, 0);
//track wrist positions for visual reference
ellipse(rWristX, rWristY, 10);
ellipse(lWristX, lWristY, 10);
line(rWristX, rWristY, lWristX, lWristY);
//GESTURES & TEXT RESPONSES
// hands join to create a blue rectangle
let handDist = dist(rWristX, rWristY, lWristX, lWristY);
if ((handDist <= 80) && (gestureactive == false)) {
fill(0, 0, 255);
rect(500, 100, 40, 30);
currentGesture = "Thank You";
if (prevGesture != currentGesture) {
sendTheMessage(currentGesture);
}
prevGesture = currentGesture;
gestureactive = true;
} else {
gestureactive = false;
}
// hand raise to create a red rectangle
if ((rWristY <= height/2) && (gestureactive == false)) {
fill(255, 0, 0);
rect(100, 100, 40, 30);
currentGesture = "Hello";
if (prevGesture != currentGesture) {
sendTheMessage(currentGesture);
}
prevGesture = currentGesture;
gestureactive = true;
} else {
gestureactive = false;
}
//Text style
noStroke();
fill(255, 0, 0); //read the color values from the message
textSize(25)
text(incomingText, 18, height / 2);
}
function sendTheMessage(greetingType) {
dataServer.publish({
channel: channelName,
message: {
greeting: greetingType
who: whoAreYou.value(),
messageText: sendText.value()
}
});
}
function readIncoming(inMessage) {
if (inMessage.channel == channelName) {
gestureReceived = inMessage.message.greeting;
textSize(30)
text(gestureReceived, width / 2, height / 2);
whosent = inMessage.publisher;
console.log(whosent + " said " + gestureReceived);
incomingText = inMessage.message.who + " : " + inMessage.message.messageText;
// randPosX = random(5, width - 100);
// randPosY = random(5, height - 20);
// randSize = random(10, 100);
}
}