package ac.essex.gp.ooechs.detection.evolved;

import ac.essex.gp.ooechs.detection.gp.DetectionProblem;
import ac.essex.gp.ooechs.detection.gp.HaarTrainingSet;
import ac.essex.gp.individuals.Individual;
import ac.essex.gp.problems.DataStack;
import ac.essex.ooechs.imaging.commons.HaarRegions;
import ac.essex.ooechs.adaboost.AdaBoostLearner;
import ac.essex.ooechs.adaboost.AdaBoostSample;

/**
 * <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: 22-Apr-2008
 * @version 1.0
 */
public class IndividualFaceDetector extends AdaBoostLearner {

    protected Individual ind;

    public IndividualFaceDetector(Individual ind) {
        this.ind = ind;
    }

    public int classify(AdaBoostSample adaBoostSample, Object object) {
        return (int) calculate((HaarRegions) adaBoostSample.getData());
    }

    public double calculate(HaarRegions image) {
        DataStack data = new DataStack();
        data.setHaar(image);
        double result =  ind.execute(data);
        return ind.getPCM().getClassFromOutput(result);
    }

    public void test(DetectionProblem learner) {

        int correct = 0, mistakes = 0, total = 0;
        int returnedTrue = 0, returnedFalse = 0;

        for (int i = 0; i < learner.trainingData.size(); i++) {
            HaarTrainingSet haarTrainingSet = learner.trainingData.elementAt(i);

            if (haarTrainingSet.classID == DetectionProblem.OBJECT1) {
                haarTrainingSet.image.makeWindowFillImage(DetectionProblem.WINDOWBLOCKSX, DetectionProblem.WINDOWBLOCKSY);
            } else {
                int x = (int) (Math.random() * (haarTrainingSet.image.getWidth() - DetectionProblem.WINDOW_WIDTH));
                int y = (int) (Math.random() * (haarTrainingSet.image.getHeight() - DetectionProblem.WINDOW_HEIGHT));
                haarTrainingSet.image.setWindowPosition(x, y, DetectionProblem.WINDOW_WIDTH, DetectionProblem.WINDOW_HEIGHT, DetectionProblem.WINDOWBLOCKSX, DetectionProblem.WINDOWBLOCKSY);
            }

            int result = (int) calculate(haarTrainingSet.image);

            if (haarTrainingSet.classID == DetectionProblem.OBJECT1) {
                if (result == haarTrainingSet.classID) {
                    // if it is a face and classifier was correct
                    correct++;
                    returnedTrue++;
                } else {
                    // if it is a face and classifier is incorrect
                    mistakes++;
                    returnedFalse++;
                }
            } else {
                // cached output expected class is NOT face

                if (result == haarTrainingSet.classID) {
                    // if it isn't a face and classifier is correct
                    correct++;
                    returnedFalse++;
                } else {
                    // if it isn't a face and classifier is incorrect.
                    mistakes++;
                    returnedTrue++;
                }
            }

            total++;

        }

        System.out.println("Total: " + total);
        System.out.println("Correct: " + correct);
        System.out.println("Mistakes: " + mistakes);
        System.out.println("Returned true: " + returnedTrue);
        System.out.println("Returned false: " + returnedFalse);

    }

}
