Lag and inconsistency with Kinect hand hover

edited August 2014 in Kinect

Hey guys!

I'm relatively new to Processing and have been working on a simple sketch using the SimpleOpenNI lib for Kinect. The sketch is intended to superimpose a scatter graph over top of the Kinect depth image as can be seen here. KinectHover However at the moment this is lagging substantially and pretty inconsistent. If anyone would like to suggest some changes that may fix this that would be absolutely amazing.

Kinect model: 1517 | Computer: Macbook Mountain Lion/Windows 7 dual-boot (using Windows for this). Thanks in advance, Ryan

import SimpleOpenNI.*;

//DECLARE 
ArrayList ballCollection; 
Table stateData;
int rowCount;
SimpleOpenNI kinect;
PVector convertedRightHand = new PVector();

//Network Display   Soma | Light Blue | Teal | Red | Lavender
color[] SomaScheme = {
  #FDE7DC, #74C8CC, #219399, #FF5C74
};
color[] palette = SomaScheme;
int SomaColour = palette[0];
int ellipseColour = palette[1];

float age;
float degree;
float circleSize = 20;
String stateName;


void setup() {
  size(600, 600);
  smooth();

  //INITIALISE
  //Initialise Table
  stateData = new Table("stateData.tsv"); 
  rowCount = stateData.getRowCount();

  //Initialise kinect
  kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
  // turn on user tracking
  kinect.enableUser();


  //Initialise ArrayList
  ballCollection = new ArrayList();

  //Provide values for x & y
  for (int row = 0; row < rowCount; row++) {
    float degree = stateData.getFloat(row, 3);
    float age = stateData.getFloat(row, 4);
    float ellipseX = map(age, 30, 41, 0, width); 
    float ellipseY = map(degree, 14, 47, 0, height);
    String stateName = stateData.getString(row, 1);
    for (int i = 0; i < rowCount; i++) { 
      Ball myBall = new Ball (ellipseX, ellipseY, circleSize, circleSize); 
      ballCollection.add(myBall);
    }
  }
}

void draw() {
  background(0);

  runKinect(); //handPosition

  //CALL FUNCTIONALITY
  for (int i = 0; i < ballCollection.size (); i++) { 
    Ball myBall = (Ball) ballCollection.get(i); 
    myBall.over();
    myBall.display();
  }
}

void onNewUser(SimpleOpenNI curContext, int userId)
{
  println("onNewUser - userId: " + userId);
  println("\tstart tracking skeleton");

  kinect.startTrackingSkeleton(userId);
}

void onLostUser(SimpleOpenNI curContext, int userId)
{
  println("onLostUser - userId: " + userId);
}

void onVisibleUser(SimpleOpenNI curContext, int userId)
{
  //println("onVisibleUser - userId: " + userId);
}

void runKinect() {
  ///Kinect 
  kinect.update();
  PImage depth = kinect.depthImage();
  image(depth, 0, 0);
  tint(255, 126);
  // make a vector of ints to store the list of users
  IntVector userList = new IntVector();
  // write the list of detected users
  // into our vector
  kinect.getUsers(userList);
  // if we found any users
  if (userList.size() > 0) {
    // get the first user
    int userId = userList.get(0);
    // if we’re successfully calibrated
    if ( kinect.isTrackingSkeleton(userId)) {
      // make a vector to store the left hand
      PVector rightHand = new PVector();
      // put the position of the left hand into that vector
      float confidence = kinect.getJointPositionSkeleton(userId, 
      SimpleOpenNI.SKEL_LEFT_HAND, 
      rightHand);
      // convert the detected hand position
      // to "projective" coordinates
      // that will match the depth image
      convertedRightHand = new PVector();
      kinect.convertRealWorldToProjective(rightHand, convertedRightHand);
      // and display it
      fill(255, 0, 0);
      ellipse(convertedRightHand.x, convertedRightHand.y, 10, 10);
    }
  }
}

Ball class: class Ball {

  // GLOBAL VARIABLES
  float x = 0; //declares these variables
  float y = 0;
  float circleSizeX;
  float circleSizeY;
  boolean over = false;
  boolean drag = false;


  //CONSTRUCTOR
  Ball(float tempX, float tempY, float tempcircleSizeX, float tempcircleSizeY) {  
    x = tempX; 
    y = tempY;
    circleSizeX = tempcircleSizeX;
    circleSizeY = tempcircleSizeY;
  }

  //FUNCTIONS
  void run() { 
    //display   
    display(); 
  }  

  void display() {   
    fill(palette[1]);
    if (over) {
      fill(palette[3]);
      //text(stateName, x, y+3);
    }
    ellipse(x, y, circleSizeX, circleSizeY);
  }

  void over() {
    //if (dist (x, y, mouseX, mouseY) < circleSize/2){
    if (dist (x, y, convertedRightHand.x, convertedRightHand.y) < circleSize/2){
    over = true;
    }
    else{
    over = false;
    }
  }
}
Tagged:

Answers

Sign In or Register to comment.