How to save multiple gif files without overwriting it onto one?

edited February 2017 in Kinect

Hello,

So I created a code where a person can take a gif animation of themselves with an effect applied on the webcam. The problem is that the gif keeps overwriting the file and keeps replacing the same file. I tried so many things to try and get it to save into a new file, but nothing is working. Your help is much appreciated (and as soon as possible, our submission is soon!)

This is the code:

 import gifAnimation.*;
GifMaker gifExport;
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
import java.io.FilenameFilter;

int FRAME_RATE=30;
boolean record=false;
int frames=0;
int totalFrames = 120;
int frameLimit = 30;
int FRAMES_DURATION = 10;
int nbGif;
// Size of each cell in the grid
int cellSize = 8;
// Number of columns and rows in our system
int cols, rows;
// Variable for capture device
Capture video;
OpenCV opencv;
// Variable for capture device
int numPixels;
int[] previousFrame;



void setup() {
  size(640, 480);
  frameRate(24);
  cols = width / cellSize;
  rows = height / cellSize;
  colorMode(RGB, 128,128,128);

  // This the default video input, see the GettingStartedCapture 
  // example if it creates an error
  video = new Capture(this, width, height);
  opencv = new OpenCV(this, width, height);
  opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);  
  // Start capturing the images from the camera
  video.start();  

   numPixels = video.width * video.height;
  // Create an array to store the previously captured frame
  previousFrame = new int[numPixels];
  loadPixels();

  background(0);
}


void draw() { 
    nbGif = howManyGif();
  if (record) {
    recordGif();
  } else {
    effect();
  }

  /////////////////////FACE DETECTION////////////////////
  opencv.loadImage(video);
 noFill();
  stroke(0, 255, 0);
  strokeWeight(1);
  Rectangle[] faces = opencv.detect();
  println(faces.length);

  for (int i = 0; i < faces.length; i++) {
    println(faces[i].x + "," + faces[i].y);
 //rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
   effect();
  }

  }



void captureEvent(Capture c) {
  c.read();
}

  /////////////////////////EFFECT STARTS HERE///////////////////////////


  void effect() {

  //if (video.available()) {
    video.read();
    video.loadPixels();

    // Begin loop for columns
    for (int i = 0; i < cols; i++) {
      // Begin loop for rows
      for (int j = 0; j < rows; j++) {

        // Where are we, pixel-wise?
        int x = i*cellSize;
        int y = j*cellSize;
        int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image

        float r = red(video.pixels[loc]);
        float g = green(video.pixels[loc]);
        float b = blue(video.pixels[loc]);
        // Make a new color with an alpha component
        color c = color(r, g, b, 128);

        // Code for drawing a single rect
        // Using translate in order for rotation to work properly
        pushMatrix();
        translate(x+cellSize/2, y+cellSize/2);
        // Rotation formula based on brightness
        rotate((2 * PI * brightness(128) / 255.0));
        rectMode(CENTER);
        fill(c);
        stroke(0);
        // Rects are larger than the cell for some overlap
        rect(0, 0, cellSize+6, cellSize+6);
        popMatrix();
      }
    }
  }
  void recordGif() {

    int x = 0;
  if (record == true) {
    // CHECK NUMBER OF FILES IN 'IMG' DIRECTORY & CREATE NEW FILE
**    gifExport = new GifMaker(this, "img/image"+(nbGif+1)+".gif");
**    gifExport.setRepeat(0); // make it an "endless" animation

    // RECORD FRAMES UNTIL FRAMELIMIT
    for (frames=0; frames<frameLimit; frames++) {
      effect();
      gifExport.setDelay(FRAMES_DURATION);
      gifExport.addFrame();
      println("saving frame");
    } // end loop

    // STOP RECORDING AND SAVE FILE
    if (frames==frameLimit) {
      gifExport.finish();
**      println("img/file"+(nbGif+1)+".gif WAS SAVED - RE INITIALIZING");
**      noLoop();
    } // end if frameLimit
  } // end if launchRecording

  // RE INIT
  frames=0;
  record = false;
  loop();
  println("end record");
} // END RECORD GIF

////////////////////////////////////////// RETURN ALL FILES AS STRING ARRAY  
String[] listFileNames(String dir) {
  File file = new File(dir);
  if (file.isDirectory()) {
    String names[] = file.list();
    return names;
  } else {
    // If it's not a directory
    return null;
  }
}


  static final FilenameFilter FILTER = new FilenameFilter() {
  static final String NAME = "file", EXT = ".gif";

  @ Override boolean accept(File path, String name) {
    return name.startsWith(NAME) && name.endsWith(EXT);
  }
};
//////////////////////////////////////////////////////// HOW MANY GIFS
int howManyGif() {
  File dataFolder = dataFile("");
  String[] theList = dataFolder.list(FILTER);
  int fileCount = theList.length;
  return(fileCount);



  }
  void export() {
  if(frames < totalFrames) {
    gifExport.setDelay(20);
    gifExport.addFrame();
    frames++;
  } else {
    gifExport.finish();
    frames++;
    println("gif saved");
    exit();}
  }

void mouseReleased() {
    record = true;
}

Answers

  • edit post, hightlight code, press ctrl-o to format your code.

    what filename is being overwritten?

    i can't see you incrementing nbGif anywhere but then the code is all but unreadable until you format it.

  • edited February 2017

    Hey I changed it, now you can see where it has the * thats what I am having problems with, any idea?

  • what filename is being overwritten?

  • Perhaps you were supposed to be using nbGif++ and not nbGif+1.

  • It didn't work :( Any other suggestion?

  • The file name being overwritten if "File1", in a folder named "img"

  • then nbGifs is 0 and your file counting routine doesn't work.

  • you are counting files in the data directory

    File dataFolder = dataFile(""); // line 175
    

    you are saving files in the img directory

    // line 127
    gifExport = new GifMaker(this, "img/image"+(nbGif+1)+".gif");
    

    so you aren't counting the files you are saving, so the count is always 0

  • name mismatch here too

    gifExport = new GifMaker(this, "img/image"+(nbGif+1)+".gif");
    

    vs

    println("img/file"+(nbGif+1)+".gif WAS SAVED
    

    your filter is expecting the names to start with 'file'

  • what should I write inside the quotation marks her:

    File dataFolder = dataFile(""); // line 175

    I tried making the name the same and it doesnt work

Sign In or Register to comment.