package textures;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import org.apache.log4j.Logger;
import processing.core.PApplet;
import processing.core.PImage;
import servers.FaceDetection;
import codeanticode.glgraphics.GLTexture;
public class FacesLoader {
public static Logger logger = Logger.getLogger(FacesLoader.class);
private PApplet parent;
// faces information
private Rectangle[] faces;
// ram place to jpg images
private PImage faceImage;
// opengl processed textures
private ArrayList<GLTexture> texturePool;
// temporal face texture
private GLTexture faceTexture ;
private GLTexture scaledFace ;
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
public FacesLoader(){
texturePool = new ArrayList<GLTexture>();
}
public FacesLoader(PApplet parent){
this.parent = parent;
texturePool = new ArrayList<GLTexture>();
}
public void resetTextures(){
//texturePool = new ArrayList<GLTexture>();
for(int i = 0 ; i< texturePool.size() ; i++){
texturePool.remove(i);
}
logger.info("NEW FACES POOL CRATED");
}
/////////////////////////////////////////////////////////////////
File dir ;
PImage scaledFaceImage = new PImage(512, 512);
public synchronized void importImages(String folder){
logger.info(">>> IMPORTING IMAGES ");
// open folder
dir = new File(folder);
logger.info("opening folder: " + folder);
// get .ana files
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".ana");
}
};
String[] children = dir.list(filter);
logger.info("folder has " + children.length + " files");
String filename;
// filter out images where no face
// were recognized
if (children != null){
for (int i = 0; i < children.length; i++) {
// Get filename of .ana file
filename = children[i];
// read that file
BufferedReader anaFile;
try {
anaFile = new BufferedReader(new FileReader(folder + "/" + filename));
// test if we can open the file
try {
if (!anaFile.ready()) throw new IOException();
} catch (IOException e) {
System.err.println("CAN'T OPEN FILE: " + folder + "/" + filename );
e.printStackTrace();
}
// if file opened...
// read just the first line
String line = anaFile.readLine();
if (line != null && !line.matches("none")){
// keep the face data and
// removes '[' and ']'
String faceData = (line.split("java.awt.Rectangle"))[1];
faceData = faceData.substring(1, faceData.length() - 1);
logger.info("face @: " + faceData);
// split in each of the params.
String[] parameters = faceData.split(",");
int[] values = new int[parameters.length];
// parse int value for x, y , w, h
for(int j = 0 ; j < parameters.length ; j++){
String t = parameters[j].split("=")[1];
values[j] = Integer.valueOf(t);
};
// System.out.println(faceData);
// we have a front face?
// load as texture
PImage faceImage = parent.loadImage(folder + "/" + filename.replaceAll(".ana", ".jpg"));
logger.info("loading image: " + filename);
// texturePool.add(new GLTexture(parent));
// PImage scaledFaceImage = new PImage(512, 512);
scaledFaceImage.copy(faceImage, 0, 0, faceImage.width, faceImage.height,
0, 0, scaledFaceImage.width , scaledFaceImage.height);
//FaceImage
// texturePool.get(texturePool.size() - 1).putPixelsIntoTexture(faceImage, values[0], values[1], values[2], values[3]);
GLTexture temp = new GLTexture(parent);
temp.putPixelsIntoTexture(faceImage, values[0], values[1], values[2], values[3]);
texturePool.add(temp);
logger.info("texture added: " + filename);
}
anaFile.close();
} catch (FileNotFoundException e) {
logger.error("FILE NOT FOUND: " + e);
} catch (IOException e) {
logger.error("I/O EXCEPTION: " + e);
}
logger.info("IMAGES LOADED OK: " + filename);
}
} else {
logger.info("folder has no files");
}
}
public GLTexture getImage(int id){
return texturePool.get(id);
}
public int getSize() {
// TODO Auto-generated method stub
return texturePool.size();
}
public boolean hasFaces(){
if (texturePool.size() > 0 ) return true;
return false;
}
}