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;
}