Advertisement
MattRichardson

Kinect Hand Tracking

Feb 4th, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import SimpleOpenNI.*;
  2. SimpleOpenNI kinect;
  3. float youAre = 0;
  4.  
  5. PImage handImage;
  6. int imageHeight = 1787;
  7. int imageWidth = 833;
  8.  
  9. void setup() {
  10.   kinect = new SimpleOpenNI(this);
  11.   kinect.enableDepth();
  12.   kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
  13.   kinect.setMirror(true);
  14.   kinect.enableRGB();
  15.  
  16.   size(640, 480);
  17.   smooth();
  18.  
  19.  
  20.   handImage = loadImage("Hand.png");
  21.  
  22. }
  23. void draw() {
  24.   kinect.update();
  25.   image(kinect.rgbImage(), 0, 0);
  26.   IntVector userList = new IntVector();
  27.   kinect.getUsers(userList);
  28.  
  29.   if (userList.size() > 0) {
  30.     int userId = userList.get(0);
  31.  
  32.    // if ( kinect.isTrackingSkeleton(userId)) {
  33.       //drawSkeleton(userId);
  34.    // }
  35.  
  36.     float rightArmAngle = getJointAngle(userId, SimpleOpenNI.SKEL_RIGHT_HAND, SimpleOpenNI.SKEL_RIGHT_ELBOW);
  37.     PVector handPos = new PVector();
  38.     kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, handPos);
  39.    
  40.     float newImageWidth = map(handPos.z, 500, 2000, imageWidth/6, imageWidth/11);
  41.     float newImageHeight = map(handPos.z, 500, 2000, imageHeight/6, imageHeight/11);
  42.    
  43.    
  44.      PVector convertedHandPos = new PVector();
  45.      kinect.convertRealWorldToProjective(handPos, convertedHandPos);
  46.    
  47.     pushMatrix();
  48.       translate(convertedHandPos.x, convertedHandPos.y);
  49.       rotate(rightArmAngle*-1);
  50.       rotate(PI/2);
  51.       image(handImage, -1 * (newImageWidth/2), 0 - (newImageHeight/2), newImageWidth, newImageHeight);
  52.     popMatrix();
  53.   }
  54. }
  55.  
  56. float getJointAngle(int userId, int jointID1, int jointID2) {
  57.   PVector joint1 = new PVector();
  58.   PVector joint2 = new PVector();
  59.   kinect.getJointPositionSkeleton(userId, jointID1, joint1);
  60.   kinect.getJointPositionSkeleton(userId, jointID2, joint2);
  61.   return atan2(joint1.y-joint2.y, joint1.x-joint2.x);
  62. }
  63.  
  64. // user-tracking callbacks!
  65. void onNewUser(int userId) {
  66.   println("start pose detection");
  67.   kinect.startPoseDetection("Psi", userId);
  68. }
  69. void onEndCalibration(int userId, boolean successful) {
  70.   if (successful) {
  71.     println(" User calibrated !!!");
  72.     kinect.startTrackingSkeleton(userId);
  73.   }
  74.   else {
  75.     println(" Failed to calibrate user !!!");
  76.     kinect.startPoseDetection("Psi", userId);
  77.   }
  78. }
  79. void onStartPose(String pose, int userId) {
  80.   println("Started pose for user");
  81.   kinect.stopPoseDetection(userId);
  82.   kinect.requestCalibrationSkeleton(userId, true);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement