geovannefarell

kinect

Jul 19th, 2013
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. import SimpleOpenNI.*;
  2. import processing.opengl.*;
  3. import processing.serial.*;
  4.  
  5. SimpleOpenNI kinect;
  6. Serial myPort;
  7.  
  8. // NITE
  9. XnVSessionManager sessionManager;
  10. XnVPointControl pointControl;
  11. XnVCircleDetector circleDetector;
  12.  
  13. // Font for text on screen
  14. PFont font;
  15.  
  16. // Variable to define different modes
  17. int mode = 0;
  18.  
  19. // Variables for Hand Detection
  20. boolean handsTrackFlag = true;
  21. PVector screenHandVec = new PVector();
  22. PVector handVec = new PVector();
  23. ArrayList<PVector>    handVecList = new ArrayList<PVector>();
  24. int handVecListSize = 30;
  25.  
  26. // Variables for Channel and Volume Control
  27. float rot;
  28. float prevRot;
  29. float rad;
  30. float angle;
  31. PVector centerVec = new PVector();
  32. PVector screenCenterVec = new PVector();
  33. int changeChannel;
  34. int channelTime;
  35.  
  36. void setup()
  37. {
  38.   // Simple-openni object
  39.   kinect = new SimpleOpenNI(this);
  40.   kinect.setMirror(true);
  41.   // enable depthMap generation, hands + gestures
  42.   kinect.enableDepth();
  43.   kinect.enableGesture();
  44.   kinect.enableHands();
  45.  
  46.   // setup NITE
  47.   sessionManager = kinect.createSessionManager("Wave", "RaiseHand");
  48.  
  49.   // Setup NITE.s Hand Point Control
  50.   pointControl = new XnVPointControl();
  51.   pointControl.RegisterPointCreate(this);
  52.   pointControl.RegisterPointDestroy(this);
  53.   pointControl.RegisterPointUpdate(this);
  54.  
  55.   // Setup NITE's Circle Detector
  56.   circleDetector = new XnVCircleDetector();  
  57.   circleDetector.RegisterCircle(this);
  58.   circleDetector.RegisterNoCircle(this);
  59.  
  60.   // Add the two to the session  
  61.   sessionManager.AddListener(circleDetector);
  62.   sessionManager.AddListener(pointControl);
  63.  
  64.   // Set the sketch size to match the depth map
  65.   size(kinect.depthWidth(), kinect.depthHeight());
  66.   smooth();
  67.  
  68.   // Initialize Font
  69.   font = loadFont("SansSerif-12.vlw");
  70.  
  71.   //Initialize Serial Communication
  72.   String portName = Serial.list()[0]; // This gets the first port on your computer.
  73.   myPort = new Serial(this, portName, 9600);
  74. }
  75.  
  76. ////////////////////////////////////////////////////////////////////////////////////////////
  77.  
  78. // XnVPointControl callbacks
  79.  
  80. void onPointCreate(XnVHandPointContext pContext)
  81. {
  82.   println("onPointCreate:");
  83.   handsTrackFlag = true;
  84.   handVec.set(pContext.getPtPosition().getX(), pContext.getPtPosition().getY(), pContext.getPtPosition().getZ());
  85.   handVecList.clear();
  86.   handVecList.add(handVec.get());
  87. }
  88.  
  89. void onPointDestroy(int nID)
  90. {
  91.   println("PointDestroy: " + nID);
  92.   handsTrackFlag = false;
  93. }
  94.  
  95. void onPointUpdate(XnVHandPointContext pContext)
  96. {
  97.   handVec.set(pContext.getPtPosition().getX(), pContext.getPtPosition().getY(), pContext.getPtPosition().getZ());
  98.   handVecList.add(0, handVec.get());
  99.   if (handVecList.size() >= handVecListSize)
  100.   { // remove the last point
  101.     handVecList.remove(handVecList.size()-1);
  102.   }
  103. }
  104.  
  105. // XnVCircleDetector callbacks
  106.  
  107. void onCircle(float fTimes, boolean bConfident, XnVCircle circle)
  108. {
  109.   println("onCircle: " + fTimes + " , bConfident=" + bConfident);
  110.   rot = fTimes;
  111.   angle = (fTimes % 1.0f) * 2 * PI - PI/2 ;
  112.   centerVec.set(circle.getPtCenter().getX(), circle.getPtCenter().getY(), handVec.z);
  113.   kinect.convertRealWorldToProjective(centerVec, screenCenterVec);
  114.   rad = circle.getFRadius();
  115.   mode = 1;
  116. }
  117.  
  118. void onNoCircle(float fTimes, int reason)
  119. {
  120.   println("onNoCircle: " + fTimes + " , reason= " + reason);  
  121.   mode = 0;
  122. }
  123.  
  124. // Draw and other Functions
  125.  
  126. void draw()
  127. {
  128.   background(0);
  129.   // Update Kinect data
  130.   kinect.update();
  131.   // update NITE
  132.   kinect.update(sessionManager);
  133.  
  134.   // draw depthImageMap
  135.   image(kinect.depthImage(), 0, 0);
  136.  
  137.   // Switch between modes
  138.   switch(mode) {
  139.     case 0: // Waiting Mode
  140.     checkSpeed(); // Check the speed of the hand
  141.     if (handsTrackFlag) drawHand(); // Draw the hand if it's been initialized
  142.     break;
  143.  
  144.     case 1: // Volume Control Mode
  145.     // Display the volume control
  146.     volumeControl();
  147.     break;
  148.  
  149.     case 2: // Channel Change Mode
  150.     channelChange(changeChannel);// draw the change channel simbol
  151.     // Add one to the timer
  152.     channelTime++;
  153.     // If the timer gets to 10, reset the counter and go back to waiting mode (0)
  154.     if (channelTime>10) {
  155.       channelTime = 0;
  156.       mode = 0;
  157.     }
  158.     break;
  159.   }
  160. }
  161.  
  162. // This will draw the channel simbol on screen and send the change channel signal to Arduino
  163. void channelChange(int sign) {
  164.   String channelChange;
  165.   pushStyle();
  166.   // If we are changing to the next channel
  167.   if (sign==1) {
  168.     stroke(255, 0, 0);
  169.     fill(255, 0, 0);
  170.     // Send the signal only if it's the first loop
  171.     if (channelTime == 0)myPort.write(1);
  172.     textAlign(LEFT);
  173.     channelChange = "Next Channel";
  174.   }
  175.   // Else, we are changing to the previous channel
  176.   else {
  177.     stroke(0, 255, 0);
  178.     fill(0, 255, 0);
  179.     // Send the signal only if it's the first loop
  180.     if (channelTime == 0)myPort.write(2);
  181.     textAlign(RIGHT);
  182.     channelChange = "Previous Channel";
  183.   }
  184.  
  185.   // Draw an arrow on screen
  186.   strokeWeight(10);
  187.   pushMatrix();
  188.   translate(width/2,height/2);
  189.   line(0,0,sign*200,0);
  190.   triangle(sign*200,20,sign*200,-20,sign*250,0);
  191.   textFont(font,20);
  192.   text(channelChange,0,40);
  193.   popMatrix();
  194.   popStyle();
  195. }
  196.  
  197. // Check if the hand movement matches what we want
  198. void checkSpeed() {
  199.   // Checkl only if we have two positions, so we can calculate the speed
  200.   if (handVecList.size()>1) {
  201.     // Check the distance between the two last hand positions
  202.     PVector vel = PVector.sub(handVecList.get(0), handVecList.get(1));
  203.     // If the distance is greater than 50 on the x-axis
  204.     if (vel.x>50) {
  205.       mode = 2;
  206.       changeChannel = 1;
  207.     }
  208.     // If the distance is lesser than -50 on the x-axis
  209.     else if (vel.x<-50) {
  210.       changeChannel = -1;
  211.       mode = 2;
  212.     }
  213.   }
  214. }
  215.  
  216. // This will display the colume control gizmo and send the signal to Arduino
  217. void volumeControl() {
  218.  
  219.   String volumeText = "You Can Now Change the Volume";
  220.   fill(150);
  221.   ellipse(screenCenterVec.x, screenCenterVec.y, 2*rad, 2*rad);
  222.   fill(255);
  223.   if (rot>prevRot) {
  224.     fill(0, 0, 255);
  225.     volumeText = "Volume Level Up";
  226.     myPort.write(3);
  227.   }
  228.   else {
  229.     fill(0, 255, 0);
  230.     volumeText = "Volume Level Down";
  231.     myPort.write(4);
  232.   }
  233.   prevRot = rot;
  234.   text(volumeText, screenCenterVec.x, screenCenterVec.y);
  235.   line(screenCenterVec.x, screenCenterVec.y, screenCenterVec.x+rad*cos(angle), screenCenterVec.y+rad*sin(angle));
  236. }
  237.  
  238. // Draw the hand on screen
  239. void drawHand() {
  240.  
  241.   stroke(255, 0, 0);
  242.  
  243.   pushStyle();
  244.   strokeWeight(6);
  245.   kinect.convertRealWorldToProjective(handVec, screenHandVec);
  246.   point(screenHandVec.x, screenHandVec.y);
  247.   popStyle();
  248.  
  249.   noFill();
  250.   Iterator itr = handVecList.iterator();
  251.   beginShape();
  252.   while ( itr.hasNext ())
  253.   {
  254.     PVector p = (PVector) itr.next();
  255.     PVector sp = new PVector();
  256.     kinect.convertRealWorldToProjective(p, sp);
  257.     vertex(sp.x, sp.y);
  258.   }
  259.   endShape();
  260. }
Advertisement
Add Comment
Please, Sign In to add comment