My sketch runs slow when outputting the OSC to Max, im using kinect RGB and Depth cam

edited March 23 in Kinect

Im attempting to build a system for hand tracking using the depth camera and color tracking, and output the values to MaxMsp where I can use them for audio mappings for a college project. The sketch runs fine when I comment out my OSC send code but when sending the OSC, the rgb and depth stop, changing frame every thirty seconds or so.

//kinect
import org.openkinect.freenect.*;
import org.openkinect.freenect2.*;
import org.openkinect.processing.*;
import org.openkinect.tests.*;
//osc
import oscP5.*;
import netP5.*;


Kinect kinect;
OscP5 oscP5;
NetAddress myRemoteLocation;

PImage cam; 

//Color
color trackColor;
color trackColor2;
float threshold = 150;

void setup() {
  size(640, 480, P2D);
  kinect = new Kinect(this);
  kinect.initVideo();
  kinect.initDepth();
  background(255);

  //Color
  trackColor = color(255, 0, 0);
  trackColor2 = color(0, 255, 0);

  //osc
  myRemoteLocation = new NetAddress("127.0.0.1", 8000);
  oscP5 = new OscP5(this, 8000);
}



void draw() {
  image(kinect.getVideoImage(), 0, 0);
  PImage cam = kinect.getVideoImage();
  int[] depth = kinect.getRawDepth();




  float avgX1 = 0;
  float avgY1 = 0;
  float avgX2 = 0;
  float avgY2 = 0;

  int count1 = 0;
  int count2 = 0;


  for (int x = 0; x < kinect.width; x++ ) {
    for (int y = 0; y < kinect.height; y++ ) {
      int loc = x + y * kinect.width;
      // What is current color
      color currentColor = cam.pixels[loc];
      float rc = red(currentColor);
      float gc = green(currentColor);
      float bc = blue(currentColor);

      float r2 = red(trackColor);
      float g2 = green(trackColor);
      float b2 = blue(trackColor);

      float r3 = red(trackColor2);
      float g3 = green(trackColor2);
      float b3 = blue(trackColor2);

      float d = distSq(rc, gc, bc, r2, g2, b2);
      float e = distSq(rc, gc, bc, r3, g3, b3);

      if (d < threshold*threshold) {
        stroke(255);
        strokeWeight(1);
        point(x, y);
        avgX1 += x;
        avgY1 += y;
        count1++;
      }
      else if (e < threshold*threshold) {
        stroke(255);
        strokeWeight(1);
        point(x, y);
        avgX2 += x;
        avgY2 += y;
        count2++;
      }

    }
  }




  if (count1 > 0) { 
    avgX1 = avgX1 / count1;
    avgY1 = avgY1 / count1;

    fill(255, 0, 0);
    strokeWeight(4.0);
    stroke(0);
    ellipse(avgX1, avgY1, 24, 24);
  }
   if (count2 > 0) { 
    avgX2 = avgX2 / count2;
    avgY2 = avgY2 / count2;

    //green
    fill(0, 255, 0);
    strokeWeight(4.0);
    stroke(0);
    ellipse(avgX2, avgY2, 24, 24);
  }


  //DEPTH
    for (int x = 0; x < 640; x++) {
    for (int y = 0; y < 480; y++) {
      int offset = x + y * kinect.width;
      int dpth = depth[offset];

      if (!(dpth > 300 && dpth < 700)) {        
        cam.pixels[offset] = color(0);
      } else if (dpth > 300 && dpth < 700) {
      /*
        //OSC LEFT
        OscMessage leftXpos = new OscMessage("/leftXpos");
        OscMessage leftYpos = new OscMessage("/leftYpos");

        leftXpos.add(avgX1);
        leftYpos.add(avgY1);

        //OSC RIGHT
        OscMessage rightXpos = new OscMessage("/rightXpos");
        OscMessage rightYpos = new OscMessage("/rightYpos");

        rightXpos.add(avgX2);
        rightYpos.add(avgY2);

        oscP5.send(leftXpos, myRemoteLocation);
        oscP5.send(leftYpos, myRemoteLocation);

        oscP5.send(rightXpos, myRemoteLocation);
        oscP5.send(rightYpos, myRemoteLocation);
       */
      }

    }
  }



}

float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
  float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  return d;
}  

Answers

  • Edit post, highlight code, press Ctrl-o to format.

  • edited March 24 Answer ✓

    @kieran --

    Wow. Perhaps I am misreading this from a quick glance, but do you really need to send four OSC messages inside your inner xy loop?

    So potentially up to 4 msg * 640x480 pixels = 1,228,800 OSC messages?

    That seems like... a lot.

  • ah thankyou!! i understand my mistake now!

Sign In or Register to comment.