package ac.essex.ooechs.facedetection.util;

import ac.essex.ooechs.imaging.commons.util.ImageFilenameFilter;
import ac.essex.ooechs.imaging.commons.fast.IntegralImage;

import java.io.File;
import java.util.Vector;

/**
 * <p/>
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version,
 * provided that any use properly credits the author.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details at http://www.gnu.org
 * </p>
 *
 * @author Olly Oechsle, University of Essex, Date: 12-Jun-2008
 * @version 1.0
 */
public class DataSet {


    public static final int TRUE = 1;
    public static final int FALSE = 0;

    public File trueDirectory;
    public File falseDirectory;

    protected Vector<IntegralTrainingImage> images;

    public DataSet(String trueDirectory, String falseDirectory) {
        this(new File(trueDirectory), new File(falseDirectory));
    }

    public DataSet(File trueDirectory, File falseDirectory) {
        this.trueDirectory = trueDirectory;
        this.falseDirectory = falseDirectory;
    }

    public Vector<IntegralTrainingImage> getData() {
        if (images == null) {
            images = new Vector<IntegralTrainingImage>(500);
            getImageFiles(trueDirectory, images, TRUE);
            getImageFiles(falseDirectory, images, FALSE);
        }
        return images;
    }

    protected int getImageFiles(File directory, Vector<IntegralTrainingImage> trainingData, int classID) {

        if (!directory.exists()) throw new RuntimeException("Directory does not exist: " + directory.getAbsolutePath());
        int counter = 0;
        try {
            File[] trueFiles = directory.listFiles();
            for (int i = 0; i < trueFiles.length; i++) {
                File f = trueFiles[i];
                if (ImageFilenameFilter.isImage(f)) {
                    trainingData.add(new IntegralTrainingImage(f, classID));
                    counter++;
                }
            }
            System.out.println("\nLoaded: " + counter + " images.");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return counter;

    }

}
