Need help with camera and networking!

edited May 2015 in Library Questions

Hey! So, I'm a Mechatronics Engineering student from Brazil, and in our first project we're using Processing to design everything. So the idea here is to make a little wheeled robot with a camera that takes commands from a PC and sends back the camera feed (the robot itself will play the server, which will run on a CubieTruck linux board). I'm working now on getting the video transmitted. My teacher made a really rough draft just as a starting point for how it should go sending RGB values and using get() set() but it was really laggy and for some reason one color channel didn't work, making the image always a yellow tint.

Anyway, I rewrote it to use hex values and pixels[] instead and it seems to work wonders (it's nearly instant and colors work great), but when I set the resolution to be something like 100x100 or 200x200 it gets really buggy and messy:

Relevant code is also in the screenshot, the rest is pretty much empty (got it straight from the example, the UI part is in another program we'll merge later on). In this case, the image size was set to 130x130. Interestingly, this doesn't seem to happen much on lower resolution (like 40x40), or it does for a bit but quickly becomes stable. You might have noticed I had to put an (if input != null) in there. It was giving me null pointer exceptions, and I'm almost sure that's got something to do with the problem. It also crashed a couple of times, and opening the task manager revealed that the app (Java something) was using up over 800MB RAM, which seems like an indication of a memory leak. Might this be a problem with the underlying java?

Answers

  • edited May 2015

    You should post the whole code since we can't copy & paste it and no idea which libraries it's relying on!
    Read the article below in order to know how to post correct formatted text code in this forum:
    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

  • Okay, here it is. The server app (should be run first):

    import processing.net.*;
    import processing.video.*;
    
    Capture cam;
    Server s;
    Client c;
    String input;
    int res;
    
    void setup() 
    {
      size(300, 255);
      background(204);
      stroke(0);
      frameRate(5); // Slow it down a little
      s = new Server(this, 12345); // Start a simple server on a port
    
    
      res = 130;
    
      int x, y;
      size(300, 300);
    
      String[] cameras = Capture.list();
    
      if (cameras.length == 0) {
        println("There are no cameras available for capture.");
        exit();
      } else {
        println("Available cameras:");
        for (int i = 0; i < cameras.length; i++) {
          println(cameras[i]);
        }
    
        // The camera can be initialized directly using an 
        // element from the array returned by list():
        cam = new Capture(this, cameras[0]);
        cam.start();
      }
    }
    
    void draw() 
    {
      if (cam.available() == true) {
        cam.read();
      }
      image(cam, 0, 0, res, res);
    
      int x, y;
      loadPixels();
    
      for (x = 0; x < res; x++)
      {
        for (y = 0; y < res; y++)
        {
          s.write(hex(pixels[y*width+x]) + ' ');
        }
      }
      updatePixels();
    
      //delay(100);
    }
    

    And the client (should be run second):

    import processing.net.*;
    
    Client c;
    String input;
    int res;
    int x, y;
    void setup() 
    {
      res = 130;
      size(450, 255);
      background(204);
      stroke(0);
      frameRate(5); // Slow it down a little
      // Connect to the server's IP address and port
      c = new Client(this, "127.0.0.1", 12345); // Replace with your server's IP and port
    }
    
    void draw() 
    {
      // Receive data from server
      if (c.available() > 0) {
        int x, y;
        loadPixels();
    
    
    
        for (x = 0; x < res; x++)
        {
          for (y = 0; y < res; y++)
          {            
            input = c.readStringUntil(' ');
            if (input != null)
            {
              input = trim(input);
              pixels[y*width+x] = unhex(input);
            }
          }
        }
    
        updatePixels();
    
        //delay(100);
      }
    }
    
Sign In or Register to comment.