User:Lucia Dossin/Protyping Lens Based/Kinect

From XPUB & Lens-Based wiki

Kinect

This assignment consisted on using one of the two sample codes provided by the tutors as basis for a Kinect project. One of the code samples would play one of four video loops, according to the Z position of user. In other words, the video loops would change if user would be closer or more distant from the kinect. The second code sample checked the X user position and displayed a static image (which was part of a sequence), according to that position.

I mixed both concepts (and code) and made the code display a static image (from a sequence) while checking user's Z position. So, in a setting where there are say 50 images, when user is close to the Kinect, image 1 will be displayed. When user is distant from it, image 50 will be displayed (and all possibilities in between).

The code:

#used random language description here, by the way
import SimpleOpenNI.*;
import java.awt.Color;
import java.util.Iterator;

SingleUserKinect kinect;

PVector userPosRealWorld = new PVector(); // 3d user position
boolean USE_KINECT = false;


float comZ; // Center of Mass Z
float minZ = 100;
float maxZ = 2500;

int numImages = 50; // total number of images

PImage[] images = new PImage[numImages]; // the images will be stored in this list

// setup is executed once, when the program started
void setup() {

  // size of the window
  size(1000,623);

  // set frame background to black (this is the background you'll see in Present Mode)
  frame.setBackground(new Color(0, 0, 0));
  // load the image sequence
  loadImageSequence();

  // user SingleUserKinect for tracking.
  kinect = new SingleUserKinect(this);
}

// draw is repeatedly executed, as fast as possible, or according to frameRate setting
void draw() {
  // update user position
  kinect.update();

  // set center of mass to the maximum value
  float comZ = maxZ;

  // if we are tracking a user, get the position of his Center of Mass  
  if (kinect.trackedUserId != 0) { 
    kinect.getCoM(userPosRealWorld);    
    comZ = userPosRealWorld.z;
    if (comZ > maxZ) {
      comZ = maxZ;
    }
    else if (comZ < minZ) {
      comZ = minZ;
    }
  }


  // map x position to an image in the sequence
  float maxRange = maxZ - minZ; // total range
  int slice_size = (int)maxRange/images.length; //1000 / 100 = 10
  float z = map(comZ, minZ, maxZ, 0, maxRange); // map center of mass to a positive range
  int slice = (int)(z / slice_size); //0 / 10 = 0
  if (slice >= numImages) slice = numImages-1;

  println("slice " + slice);
  // draw the image
  image(images[slice], 0, 0, width, height);
}

// load the image sequence
void loadImageSequence() {
  String filename = "";

  for (int i = 0; i < numImages; i++) {
    filename = "export/sm_img_" +nf(i, 3) + ".png";   
    images[i] = loadImage(filename);
  }
}


// -----------------------------------------------------------------
// SimpleOpenNI user events
// -----------------------------------------------------------------
// onNewUser is triggered when the kinect registers a new user
void onNewUser(SimpleOpenNI curContext, int userId)
{
  // let our SingleUserKinect Class take care of this
  kinect.registerNewUser(curContext, userId);
}

// onLostUser is triggered when the kinect deregisters a user
void onLostUser(SimpleOpenNI curContext, int userId)
{
  // let our SingleUserKinect Class take care of this
  kinect.deRegisterUser(curContext, userId);
}