Video capture at set interval

Hi all,

I am capturing video and displaying it on my screen. I am modifying the pixels but it's too taxing on my machine to have it running non-stop. (image every 5 seconds, etc)

How can I set the camera to capture at a set interval?

import processing.video.*;
Capture video;
int fps=30;

void setup() {
  size(1440/2, 900/2); 
  video = new Capture(this, width, height, fps);
  noStroke();
  smooth();
}
void draw() {
  background(0);
  if (video.available()) {    
    video.read();
  }
  video.loadPixels();
  for (int x = 0; x < video.width; x=x+5) {
    for (int y = 0; y < video.height; y=y+5) {
      color c = video.pixels[x+y*video.width];  
      fill(c);
      rect(x, y, 5, 5);
    }
  }
}

Answers

  • your frame rate for the camera is 30, but processing's draw runs at 60 by default. you could try adding frameRate(30); after your size(); function to sync with your capture fps.

  • Thanks but what if I want to capture an image from the camera once every 2 minutes?

  • Then you call video.read() only once per 2 minutes.

    Here's your source where I have added MyLapTimer class to make code more self-documenting and readable.

    import processing.video.*;
    Capture video;
    int fps=30;
    
    MyLapTimer myLapTimer;
    
    void setup() {
      size(1440/2, 900/2);
      //video = new Capture(this, width, height, fps);
      println(Capture.list());
      video = new Capture(this, Capture.list()[0]);
      noStroke();
      smooth();
      background(0);
      video.start();
      myLapTimer  = new MyLapTimer(true); // we set true, so that first cll to haveElapsedXXXX() always return true. 
    }
    void draw() {
    
      if ( myLapTimer.haveElapsedSeconds(2) ) {
        println("reading your camera and generating image");
        myLapTimer.restart();
                  if (video.available()) {    
                    video.read();
                  }
                  video.loadPixels();
                  background(0);
                  //image(video,0,0);
    
                  for (int x = 0; x < video.width; x=x+5) {
                    for (int y = 0; y < video.height; y=y+5) {
                      color c = video.pixels[x+y*video.width];  
                      fill(c);
                      rect(x, y, 5, 5);
                    }
                  }
      }
    }
    
    
    class MyLapTimer{
        private int startTime;
        private boolean firstRun  = true;
        private boolean firstRunAsElapsed = false;
    
        MyLapTimer(boolean vFirstRunAsElapsed){
           // set current time as start
            startTime = millis();
            firstRunAsElapsed = vFirstRunAsElapsed; 
        }
    
        void restart(){
           startTime = millis();
           firstRun = false;
        }
    
        boolean haveElapsedMillis(int millis){
            if ( firstRunAsElapsed && firstRun) {
                return true; 
            }
    
            int elapsedSinceLapStart = millis() - startTime;
            if ( elapsedSinceLapStart >= millis){
              return true;
            }
            else{
              return false; 
            }
        }
    
    
        boolean haveElapsedSeconds(int seconds){
           return haveElapsedMillis(seconds * 1000);
        }
    
    
        boolean haveElapsedMinutes(int mins){
           return haveElapsedSeconds(60 * mins);
        }
    
    }
    

    Also in your original source you forgot to call video.start() to initiate capturing from camera.

Sign In or Register to comment.