Sensor data / Processing + Kinect

kinectSensorData
Utilisation dans Processing des données Kinect.

Sources:
kinectSensorData.pde / Processing 3.0.1  + Librairie Kinect4WinSDK / Kinect v1 modèle 1414 / KinectSDK v1.8

import kinect4WinSDK.Kinect;
import kinect4WinSDK.SkeletonData;
 
Kinect kinect;
ArrayList <SkeletonData> bodies;
 
void setup(){
  size(640, 480);
  background(0);
  kinect = new Kinect(this);
  smooth();
  bodies = new ArrayList<SkeletonData>();
}
 
void draw(){
  background(0);
  // Affichage de la vidéo Kinect
  image(kinect.GetImage(), 0, 0, width, height);
  
  // Affichage de la profondeur Kinect
  //image(kinect.GetDepth(), 0, 0, width, height);
  
  // Affichage du mask Kinect
  //image(kinect.GetMask(), 0, 0, width, height);
  
  // Affichage du squelette
  /**/for (int i=0; i<bodies.size (); i++){
    drawSkeleton(bodies.get(i));
  }
  
  // Affichage de la position
  /*for (int i=0; i<bodies.size (); i++){
    drawPosition(bodies.get(i));
  }*/
}
 
void drawPosition(SkeletonData _s){
  noStroke();
  fill(0, 100, 255);
  String s1 = str(_s.dwTrackingID);
  text(s1, _s.position.x*width, _s.position.y*height);
}
 
void drawSkeleton(SkeletonData _s){
  // Corps
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_HEAD, Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, Kinect.NUI_SKELETON_POSITION_SPINE);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT, Kinect.NUI_SKELETON_POSITION_SPINE);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT, Kinect.NUI_SKELETON_POSITION_SPINE);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SPINE, Kinect.NUI_SKELETON_POSITION_HIP_CENTER);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_HIP_CENTER, Kinect.NUI_SKELETON_POSITION_HIP_LEFT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_HIP_CENTER, Kinect.NUI_SKELETON_POSITION_HIP_RIGHT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_HIP_LEFT, Kinect.NUI_SKELETON_POSITION_HIP_RIGHT);
 
  // Bras gauche
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT, Kinect.NUI_SKELETON_POSITION_ELBOW_LEFT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_ELBOW_LEFT, Kinect.NUI_SKELETON_POSITION_WRIST_LEFT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_WRIST_LEFT, Kinect.NUI_SKELETON_POSITION_HAND_LEFT);
 
  // Bras droit
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT, Kinect.NUI_SKELETON_POSITION_ELBOW_RIGHT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_ELBOW_RIGHT, Kinect.NUI_SKELETON_POSITION_WRIST_RIGHT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_WRIST_RIGHT, Kinect.NUI_SKELETON_POSITION_HAND_RIGHT);
 
  // Jambe gauche
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_HIP_LEFT, Kinect.NUI_SKELETON_POSITION_KNEE_LEFT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_KNEE_LEFT, Kinect.NUI_SKELETON_POSITION_ANKLE_LEFT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_ANKLE_LEFT, Kinect.NUI_SKELETON_POSITION_FOOT_LEFT);
 
  // Jambe droite
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_HIP_RIGHT, Kinect.NUI_SKELETON_POSITION_KNEE_RIGHT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_KNEE_RIGHT, Kinect.NUI_SKELETON_POSITION_ANKLE_RIGHT);
  DrawBone(_s, Kinect.NUI_SKELETON_POSITION_ANKLE_RIGHT, Kinect.NUI_SKELETON_POSITION_FOOT_RIGHT);
}
 
void DrawBone(SkeletonData _s, int _j1, int _j2){
  noFill();
  stroke(255, 255, 0);
  if (_s.skeletonPositionTrackingState[_j1] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED && _s.skeletonPositionTrackingState[_j2] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED) {
    line(_s.skeletonPositions[_j1].x*width, _s.skeletonPositions[_j1].y*height, _s.skeletonPositions[_j2].x*width, _s.skeletonPositions[_j2].y*height);
  }
}
 
void appearEvent(SkeletonData _s){
  if (_s.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED){
    return;
  }
  synchronized(bodies){
    bodies.add(_s);
  }
}
 
void disappearEvent(SkeletonData _s){
  synchronized(bodies){
    for (int i=bodies.size()-1; i>=0; i--){
      if (_s.dwTrackingID == bodies.get(i).dwTrackingID){
        bodies.remove(i);
      }
    }
  }
}
 
void moveEvent(SkeletonData _b, SkeletonData _a){
  if (_a.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED) {
    return;
  }
  synchronized(bodies){
    for (int i=bodies.size()-1; i>=0; i--){
      if (_b.dwTrackingID == bodies.get(i).dwTrackingID){
        bodies.get(i).copy(_a);
        break;
      }
    }
  }
}