KINECT2進数

Post date: 2013/07/01 10:57:17

/* --------------------------------------------------------------------------
 * SimpleOpenNI demo "KINECT2進数" 複数で遊べます ((c)2013 T.Takaishi) 
 * -------------------------------------------------------------------------- 
 * Processing Wrapper for the OpenNI/Kinect library 
 * http://code.google.com/p/simple-openni 
 * -------------------------------------------------------------------------- 
 */  
import SimpleOpenNI.*; 
SimpleOpenNI  context; 
boolean flagascii=false; 
void setup() { 
  context = new SimpleOpenNI(this); 
  context.setMirror(true); 
  context.enableDepth(); 
  context.enableUser(SimpleOpenNI.SKEL_PROFILE_UPPER); // Skelton の上半身のみ使用 
  background(200,0,0); 
  noStroke(); 
  smooth(); 
  size(context.depthWidth(), context.depthHeight()); 
} 

void draw() { 
  // update the cam 
  context.update(); 
  image(context.depthImage(),0,0); 
  getBin(); 
}
 
void getBin() { 
  int[] userList = context.getUsers(); 
  int[] digit =new int[2]; // 2進数2桁  
  int num10 = 0;     // 10進数 
  String strnum2=""; // 2進数の文字列 
  for(int i=0;userList.length;i++)   { 
    if(context.isTrackingSkeleton(userList[i])) 
    { 
      // Get joint positions 
      PVector pos_RightHand = new PVector(); 
      get_RealJointPosition(userList[i],SimpleOpenNI.SKEL_RIGHT_HAND,pos_RightHand); 
      PVector pos_RightShoulder = new PVector(); 
      get_RealJointPosition(userList[i],SimpleOpenNI.SKEL_RIGHT_SHOULDER,pos_RightShoulder); 
      PVector pos_LeftHand = new PVector(); 
      get_RealJointPosition(userList[i],SimpleOpenNI.SKEL_LEFT_HAND,pos_LeftHand); 
      PVector pos_LeftShoulder = new PVector(); 
      get_RealJointPosition(userList[i],SimpleOpenNI.SKEL_LEFT_SHOULDER,pos_LeftShoulder); 
      PVector pos_Torso = new PVector(); 
      get_RealJointPosition(userList[i],SimpleOpenNI.SKEL_TORSO,pos_Torso); 
      // Make digits (左上が (0,0)) 
      if(pos_RightHand.y < pos_RightShoulder.y) digit[0]=1; else digit[0]=0; 
      if(pos_LeftHand.y < pos_LeftShoulder.y) digit[1]=1; else digit[1]=0; 
      // Draw colored circle on hands 
      drawCircle(pos_RightHand, digit[0], 20); 
      drawCircle(pos_LeftHand, digit[1], 20); 
      // Draw numbers on torso 
      drawNum(pos_Torso, i, 80); 
      // Calculate number and make string 
      num10=num10*4+digit[1]*2+digit[0]; 
      strnum2=strnum2+str(digit[1])+str(digit[0]); 
    } 
  } 
  strnum2 = strnum2+"="+str(num10); 
  if(flagascii) // Display ASCII code 
  { 
    if(userList.length > 3) // 2^6=64, 'A'=65 
      strnum2 = strnum2+" ["+(char)num10+"]"; 
    else 
      strnum2 = strnum2+" ["+(char)(num10+'A'-1)+"]"; 
  } 
  fill(2555,255,255); 
  textSize(80); 
   text(strnum2,60,100); 
} 
 
void get_RealJointPosition(int userId, int type, PVector pos) { 
      PVector jointPos = new PVector(); 
      context.getJointPositionSkeleton(userId,type,jointPos); 
      context.convertRealWorldToProjective(jointPos,pos); 
} 
 
void drawCircle(PVector pos, int num, int size) { 
  pushMatrix(); 
  translate(pos.x,pos.y); // translate to the position of the Hand 
  if(num==1) fill(255,0,0); else fill(0,255,0); 
  ellipse(0,0,size,size); 
  popMatrix(); 
} 
 
void drawNum(PVector pos, int num, int size) { 
  fill(0,0,0); 
  textSize(size); 
  text(str(num),pos.x,pos.y); 
} 
 
void keyPressed() { 
  switch(key) 
  { 
    case 'a': // Switch for displaying ASCII code 
      if(!flagascii) flagascii=true; else flagascii=false; 
      break; 
    case 'q': 
      exit(); 
      break; 
  } 
} 
 
// ----------------------------------------------------------------- 
// SimpleOpenNI events 

void onNewUser(int userId) { 
  context.requestCalibrationSkeleton(userId,true); 
} 
 
void onEndCalibration(int userId, boolean successfull) { 
    if (successfull) 
   { 
     println("  User calibrated !!!");
     context.startTrackingSkeleton(userId); 
   } 
}