/* -------------------------------------------------------------------------- * SimpleOpenNI User Test * -------------------------------------------------------------------------- * Processing Wrapper for the OpenNI/Kinect library * http://code.google.com/p/simple-openni * -------------------------------------------------------------------------- * prog: Max Rheiner / Interaction Design / zhdk / http://iad.zhdk.ch/ * date: 02/16/2011 (m/d/y) * ---------------------------------------------------------------------------- */ import SimpleOpenNI.*; import oscP5.*; import netP5.*; OscP5 oscP5; NetAddress myRemoteLocation; SimpleOpenNI context; void setup() { context = new SimpleOpenNI(this); // enable depthMap generation context.enableDepth(); // enable skeleton generation for all joints context.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL); oscP5 = new OscP5(this,7110); myRemoteLocation = new NetAddress("127.0.0.1",7110); background(200,0,0); stroke(0,0,255); strokeWeight(3); smooth(); size(context.depthWidth(), context.depthHeight()); } void draw() { // update the cam context.update(); // draw depthImageMap image(context.depthImage(),0,0); // draw the skeleton if it's available if(context.isTrackingSkeleton(1)) drawSkeleton(1); sendOSCeleton(1); } // draw the skeleton with the selected joints void drawSkeleton(int userId) { // to get the 3d joint data /* PVector jointPos = new PVector(); context.getJointPositionSkeleton(userId,SimpleOpenNI.SKEL_NECK,jointPos); println(jointPos); */ context.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK); context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND); context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO); context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT); context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT); } void sendOSCeleton(int userId) { // to get the 3d joint data PVector jointPosRealWorld = new PVector(); PVector jointPosProjected = new PVector(); /* context.getJointPositionSkeleton(userId,SimpleOpenNI.SKEL_NECK,jointPos); println(jointPos); */ context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_HEAD, jointPosRealWorld); //context.convertRealWorldToProjective(jointPosRealWorld, jointPosProjected); JointSendOSC(userId, "head", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_NECK, jointPosRealWorld); JointSendOSC(userId, "neck", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_TORSO, jointPosRealWorld); JointSendOSC(userId, "torso", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, jointPosRealWorld); JointSendOSC(userId, "r_shoulder", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, jointPosRealWorld); JointSendOSC(userId, "r_elbow", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, jointPosRealWorld); JointSendOSC(userId, "r_hand", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, jointPosRealWorld); JointSendOSC(userId, "l_shoulder", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, jointPosRealWorld); JointSendOSC(userId, "l_elbow", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, jointPosRealWorld); JointSendOSC(userId, "l_hand", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HIP, jointPosRealWorld); JointSendOSC(userId, "r_hip", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, jointPosRealWorld); JointSendOSC(userId, "r_knee", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_ANKLE, jointPosRealWorld); JointSendOSC(userId, "r_ankle", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_FOOT, jointPosRealWorld); JointSendOSC(userId, "r_foot", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HIP, jointPosRealWorld); JointSendOSC(userId, "l_hip", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_KNEE, jointPosRealWorld); JointSendOSC(userId, "l_knee", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_ANKLE, jointPosRealWorld); JointSendOSC(userId, "l_ankle", jointPosRealWorld); context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_FOOT, jointPosRealWorld); JointSendOSC(userId, "l_foot", jointPosRealWorld); /* context.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK); context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND); context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO); context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE); context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT); context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE); context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT); */ } // ----------------------------------------------------------------- // SimpleOpenNI events void onNewUser(int userId) { println("onNewUser - userId: " + userId); println(" start pose detection"); context.startPoseDetection("Psi",userId); } void onLostUser(int userId) { println("onLostUser - userId: " + userId); } void onStartCalibration(int userId) { println("onStartCalibration - userId: " + userId); } void onEndCalibration(int userId, boolean successfull) { println("onEndCalibration - userId: " + userId + ", successfull: " + successfull); if (successfull) { println(" User calibrated !!!"); context.startTrackingSkeleton(userId); } else { println(" Failed to calibrate user !!!"); println(" Start pose detection"); context.startPoseDetection("Psi",userId); } } void onStartPose(String pose,int userId) { println("onStartPose - userId: " + userId + ", pose: " + pose); println(" stop pose detection"); context.stopPoseDetection(userId); context.requestCalibrationSkeleton(userId, true); } void onEndPose(String pose,int userId) { println("onEndPose - userId: " + userId + ", pose: " + pose); } void UserSendOSC(int userId, float posX, float posY, float posZ, float posXProjected, float posYProjected, float posZProjected){ OscBundle myBundle = new OscBundle(); OscMessage myMessage = new OscMessage("/user/"+userId); myMessage.add(posX); myMessage.add(posY); myMessage.add(posZ); myMessage.add(posXProjected); myMessage.add(posYProjected); myMessage.add(posZProjected); myBundle.add(myMessage); myMessage.clear(); myBundle.setTimetag(myBundle.now() + 1000); oscP5.send(myBundle, myRemoteLocation); myBundle.clear(); } void JointSendOSC(int userId, String joint, PVector jointPos){ float posX; float posY; float posZ; // Normalise joint x y to be between 0.0 - 1.0 and z to be between 0.0 - 7.0 (??) - a la OSCeleton posX = (1280-jointPos.x)/2560; posY=(960-jointPos.y)/1920; posZ=(jointPos.z*7.8125/10000); OscBundle myBundle = new OscBundle(); OscMessage myMessage = new OscMessage("/joint"); myMessage.add(joint); myMessage.add(userId); myMessage.add(posX); myMessage.add(posY); myMessage.add(posZ); //myMessage.add(posXProjected); //myMessage.add(posYProjected); //myMessage.add(posZProjected); myBundle.add(myMessage); myMessage.clear(); myBundle.setTimetag(myBundle.now() + 1000); oscP5.send(myBundle, myRemoteLocation); myBundle.clear(); }