Saving frames when frameRate%15 is not working

edited September 2015 in Library Questions

Hello there,

I have this code that saves a frame from the webcam everytime frameRate%15 ==0, and I only want 30 frames total.

Whit a frameRate of 60fps I should have 4 frames per second, but is not the case (it's not saving any frame) and i don't know why, any ideas?

I'm using Processing 2.2.1

import processing.video.*;
Capture cam;
int frames = 0;

void setup() {
  size(640, 480);
  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]);
    }
    cam = new Capture(this, cameras[1]);
    cam.start();
  }
  background(0);
}

void draw() {
  if (mousePressed) {
    if (cam.available() == true) {
      cam.read();
      image(cam, 0, 0);
      if (frameCount % 15 == 0) {
        saveFrame("frames/" + frames + ".png"); //voy guardando frames
        frames++;
      }
      if (frames > 29) {
        frames = 0;
      }
    }
  } else {
    background(0);
  }

  print("frame count: ");
  print(frameCount);
  print("  |  framerate: ");
  println(frameRate);
}

Answers

  • Answer ✓

    The problem is you only save the frame when cam.available() == true. You don't always get a new image from the camera every time you call draw(). So frameCount % 15 == 0 will be false, more often than once every 15 frames. (Frames 0, 15, 30 etc may not be frames where a new image is available.)

    If you want to save one out of every 15 available frames, you can create a counter that counts just the available frames, and use the % operation on that counter.

    Or if you want to save (say) 4 frames per second, you can check the timestamp instead of the frameCount. Note that saving a frame is slow and will quite likely slowly down your code for that call to draw().

  • Answer ✓

    yeah, do this

    if (frameCount % 15 == 0) {
            saveFrame("frames/" + frames + ".png"); //voy guardando frames
            frames++;
          }
    

    outside the ifs

  • edited September 2015

    Now I get it, thanks billhsu and Chrisir!

    Just one question, when you say "Note that saving a frame is slow and will quite likely slowly down your code for that call to draw()." is there any way to save frames outside the draw?

  • hm...

    you could store the images in an ArrayList and when the movie is over write them to the hard drive from there.

  • thanks again! I'll try that later.

  • Note that the way you use saveFrame(), it could be replaced by frame(). saveFrame() is best used with a pattern numbering the frames automatically.

  • @PhiLho Do you mean using the line frame() instead of saveFrame() ?

    Like this?

    frame("frames/" + frames + ".png");

  • Ouch, I meant to use save() instead of saveFrame(). frame() is a different thing!

  • @PhiLho I got it ! thakns

Sign In or Register to comment.