Created
February 4, 2012 21:52
-
-
Save mrichardson23/1740436 to your computer and use it in GitHub Desktop.
Kinect Foam Hand Tracking 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleOpenNI.*; | |
SimpleOpenNI kinect; | |
float youAre = 0; | |
PImage handImage; | |
int imageHeight = 1787; | |
int imageWidth = 833; | |
void setup() { | |
kinect = new SimpleOpenNI(this); | |
kinect.enableDepth(); | |
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL); | |
kinect.setMirror(true); | |
kinect.enableRGB(); | |
size(640, 480); | |
smooth(); | |
handImage = loadImage("Hand.png"); | |
} | |
void draw() { | |
kinect.update(); | |
image(kinect.rgbImage(), 0, 0); | |
IntVector userList = new IntVector(); | |
kinect.getUsers(userList); | |
if (userList.size() > 0) { | |
int userId = userList.get(0); | |
// if ( kinect.isTrackingSkeleton(userId)) { | |
//drawSkeleton(userId); | |
// } | |
float rightArmAngle = getJointAngle(userId, SimpleOpenNI.SKEL_RIGHT_HAND, SimpleOpenNI.SKEL_RIGHT_ELBOW); | |
PVector handPos = new PVector(); | |
kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, handPos); | |
PVector convertedHandPos = new PVector(); | |
kinect.convertRealWorldToProjective(handPos, convertedHandPos); | |
float newImageWidth = map(convertedHandPos.z, 500, 2000, imageWidth/6, imageWidth/12); | |
float newImageHeight = map(convertedHandPos.z, 500, 2000, imageHeight/6, imageHeight/12); | |
pushMatrix(); | |
translate(convertedHandPos.x, convertedHandPos.y); | |
rotate(rightArmAngle*-1); | |
rotate(PI/2); | |
image(handImage, -1 * (newImageWidth/2), 0 - (newImageHeight/2), newImageWidth, newImageHeight); | |
popMatrix(); | |
} | |
} | |
float getJointAngle(int userId, int jointID1, int jointID2) { | |
PVector joint1 = new PVector(); | |
PVector joint2 = new PVector(); | |
kinect.getJointPositionSkeleton(userId, jointID1, joint1); | |
kinect.getJointPositionSkeleton(userId, jointID2, joint2); | |
return atan2(joint1.y-joint2.y, joint1.x-joint2.x); | |
} | |
// user-tracking callbacks! | |
void onNewUser(int userId) { | |
println("start pose detection"); | |
kinect.startPoseDetection("Psi", userId); | |
} | |
void onEndCalibration(int userId, boolean successful) { | |
if (successful) { | |
println(" User calibrated !!!"); | |
kinect.startTrackingSkeleton(userId); | |
} | |
else { | |
println(" Failed to calibrate user !!!"); | |
kinect.startPoseDetection("Psi", userId); | |
} | |
} | |
void onStartPose(String pose, int userId) { | |
println("Started pose for user"); | |
kinect.stopPoseDetection(userId); | |
kinect.requestCalibrationSkeleton(userId, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment