
Download the above Grasshopper definition here: skeleton.gh
import KinectPV2.KJoint;
import KinectPV2.*;
import oscP5.*;
import netP5.*;
NetAddress myBroadcastLocation;
OscMessage myMessage;
OscP5 oscP5;
KinectPV2 kinect;
float sc = 800;
void setup() {
size(400, 600, P3D);
perspective(PI/3.0,(float)width/height,1,100000);
oscP5 = new OscP5(this,6880);
myBroadcastLocation = new NetAddress("localhost",1200);
kinect = new KinectPV2(this);
kinect.enableSkeletonColorMap(true);
kinect.enableColorImg(true);
kinect.init();
}
void draw() {
background(0);
myMessage = new OscMessage("/skeleton");
//image(kinect.getColorImage(), 0, 0, width, height);
kinect.enableSkeleton3DMap(true);
ArrayList skeletonArray = kinect.getSkeleton3d();
pushMatrix();
translate(width/2,height/2);
rotateX(PI);
//individual JOINTS
for (int i = 0; i < skeletonArray.size(); i++) {
KSkeleton skeleton = (KSkeleton) skeletonArray.get(i);
if (skeleton.isTracked()) {
KJoint[] joints = skeleton.getJoints();
color col = skeleton.getIndexColor();
fill(col);
stroke(col);
drawBody(joints);
//draw different color for each hand state
drawHandState(joints[KinectPV2.JointType_HandRight]);
drawHandState(joints[KinectPV2.JointType_HandLeft]);
}
}
popMatrix();
fill(255);
text(frameRate, 50, 50);
}
//DRAW BODY
void drawBody(KJoint[] joints) {
drawBone(joints, KinectPV2.JointType_Head, KinectPV2.JointType_Neck);
drawBone(joints, KinectPV2.JointType_Neck, KinectPV2.JointType_SpineShoulder);
drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_SpineMid);
drawBone(joints, KinectPV2.JointType_SpineMid, KinectPV2.JointType_SpineBase);
drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_ShoulderRight);
drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_ShoulderLeft);
drawBone(joints, KinectPV2.JointType_SpineBase, KinectPV2.JointType_HipRight);
drawBone(joints, KinectPV2.JointType_SpineBase, KinectPV2.JointType_HipLeft);
// Right Arm
drawBone(joints, KinectPV2.JointType_ShoulderRight, KinectPV2.JointType_ElbowRight);
drawBone(joints, KinectPV2.JointType_ElbowRight, KinectPV2.JointType_WristRight);
drawBone(joints, KinectPV2.JointType_WristRight, KinectPV2.JointType_HandRight);
drawBone(joints, KinectPV2.JointType_HandRight, KinectPV2.JointType_HandTipRight);
drawBone(joints, KinectPV2.JointType_WristRight, KinectPV2.JointType_ThumbRight);
// Left Arm
drawBone(joints, KinectPV2.JointType_ShoulderLeft, KinectPV2.JointType_ElbowLeft);
drawBone(joints, KinectPV2.JointType_ElbowLeft, KinectPV2.JointType_WristLeft);
drawBone(joints, KinectPV2.JointType_WristLeft, KinectPV2.JointType_HandLeft);
drawBone(joints, KinectPV2.JointType_HandLeft, KinectPV2.JointType_HandTipLeft);
drawBone(joints, KinectPV2.JointType_WristLeft, KinectPV2.JointType_ThumbLeft);
// Right Leg
drawBone(joints, KinectPV2.JointType_HipRight, KinectPV2.JointType_KneeRight);
drawBone(joints, KinectPV2.JointType_KneeRight, KinectPV2.JointType_AnkleRight);
drawBone(joints, KinectPV2.JointType_AnkleRight, KinectPV2.JointType_FootRight);
// Left Leg
drawBone(joints, KinectPV2.JointType_HipLeft, KinectPV2.JointType_KneeLeft);
drawBone(joints, KinectPV2.JointType_KneeLeft, KinectPV2.JointType_AnkleLeft);
drawBone(joints, KinectPV2.JointType_AnkleLeft, KinectPV2.JointType_FootLeft);
drawJoint(joints, KinectPV2.JointType_HandTipLeft);
drawJoint(joints, KinectPV2.JointType_HandTipRight);
drawJoint(joints, KinectPV2.JointType_FootLeft);
drawJoint(joints, KinectPV2.JointType_FootRight);
drawJoint(joints, KinectPV2.JointType_ThumbLeft);
drawJoint(joints, KinectPV2.JointType_ThumbRight);
drawJoint(joints, KinectPV2.JointType_Head);
oscP5.send(myMessage, myBroadcastLocation);
}
//draw joint
void drawJoint(KJoint[] joints, int jointType) {
pushMatrix();
translate(joints[jointType].getX()*sc, joints[jointType].getY()*sc, joints[jointType].getZ()*sc);
ellipse(0, 0, 25, 25);
popMatrix();
}
//draw bone
void drawBone(KJoint[] joints, int jointType1, int jointType2) {
pushMatrix();
translate(joints[jointType1].getX()*sc, joints[jointType1].getY()*sc, joints[jointType1].getZ()*sc);
ellipse(0, 0, 25, 25);
popMatrix();
line(joints[jointType1].getX()*sc, joints[jointType1].getY()*sc, joints[jointType1].getZ()*sc, joints[jointType2].getX()*sc, joints[jointType2].getY()*sc, joints[jointType2].getZ()*sc);
myMessage.add(joints[jointType1].getX());
myMessage.add(joints[jointType1].getY());
myMessage.add(joints[jointType1].getZ());
myMessage.add(joints[jointType2].getX());
myMessage.add(joints[jointType2].getY());
myMessage.add(joints[jointType2].getZ());
}
//draw hand state
void drawHandState(KJoint joint) {
noStroke();
handState(joint.getState());
pushMatrix();
translate(joint.getX()*sc, joint.getY()*sc, joint.getZ()*sc);
ellipse(0, 0, 70, 70);
popMatrix();
}
/*
Different hand state
KinectPV2.HandState_Open
KinectPV2.HandState_Closed
KinectPV2.HandState_Lasso
KinectPV2.HandState_NotTracked
*/
void handState(int handState) {
switch(handState) {
case KinectPV2.HandState_Open:
fill(0, 255, 0);
break;
case KinectPV2.HandState_Closed:
fill(255, 0, 0);
break;
case KinectPV2.HandState_Lasso:
fill(0, 0, 255);
break;
case KinectPV2.HandState_NotTracked:
fill(255, 255, 255);
break;
}
}