Skip to content

ITP is #1 (Kinect Hand Tracking)

January 30, 2012

I decided I wanted to use the Z axis to set the size of the hand to make it a little more realistic as it moved to and from the camera. The next step is to rotate the hand so that if I wave it back and forth, it doesn’t stay upright (which looks a bit weird). I would have to know the position of the elbow as well in order to do that. Code below:

import SimpleOpenNI.*;

SimpleOpenNI context;
boolean handsTrackFlag = false;
PVector handVec = new PVector();

PImage b;

int imageHeight = 1787;
int imageWidth = 833;

void setup() {
  size(640, 480);  // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem
  //size(1024,768,OPENGL);
  context = new SimpleOpenNI(this);
  context.setMirror(false);
  context.enableDepth();
  context.enableRGB(); //get color camera
  //unfortunately have to track guestures to get hands
  context.enableGesture();
  context.enableHands();
  context.addGesture("RaiseHand");
  fill(255, 0, 0);
  b = loadImage("Hand.png");
}
void draw() {
  // update the cam
  context.update();
  //paint the image
  image(context.rgbImage(), 0, 0, width, height);
  if (handsTrackFlag) 
  {
    PVector myPositionScreenCoords  = new PVector(); //storage device
    //convert the weird kinect coordinates to screen coordinates.
    context.convertRealWorldToProjective(handVec, myPositionScreenCoords);
    // depth 500 - 1400
    float newImageWidth = map(myPositionScreenCoords.z, 500, 1400, imageWidth/6, imageWidth/13);
    float newImageHeight = map(myPositionScreenCoords.z, 500, 1400, imageHeight/6, imageHeight/13);
    //ellipse(myPositionScreenCoords.x, myPositionScreenCoords.y, imagesize, imagesize);
    image(b, myPositionScreenCoords.x - (newImageWidth/2), myPositionScreenCoords.y - (newImageHeight/2) - (newImageHeight/6), newImageWidth, newImageHeight);
    println(myPositionScreenCoords.z);
  }
}
// ----------------------------------------------------------------- // hand events
void onCreateHands(int handId, PVector pos, float time) {
  println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);
  handsTrackFlag = true;
  handVec = pos;
}
void onUpdateHands(int handId, PVector pos, float time) {
  //println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
  //store the location of the hand in a vector object
  handVec = pos;
}
void onDestroyHands(int handId, float time) {
  println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
  handsTrackFlag = false;
  //go back to looking for the guesture that gave you hand.
  context.addGesture("RaiseHand");
}
// ----------------------------------------------------------------- // gesture events
void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition) {
  println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);
  //stop looking for the gesture
  context.removeGesture(strGesture);
  //use the location of this guesture tell you where to start tracking the hand
  context.startTrackingHands(endPosition);
}
void onProgressGesture(String strGesture, PVector position, float progress) {
  //println("onProgressGesture - strGesture: " + strGesture + ", position: " + position + ", progress:" + progress);
}
// ----------------------------------------------------------------- // Keyboard event
void keyPressed() {
  switch(key)
  {
  case ' ':
    context.setMirror(!context.mirror());
    break;
  }
}

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: