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

import ac.essex.ooechs.imaging.commons.evolved.ObjectDetector;
import ac.essex.ooechs.imaging.commons.ImageWindow;
import ac.essex.ooechs.adaboost.AdaBoostSolution;
import ac.essex.gp.ooechs.detection.gp.DetectionProblem;

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

/**
 * Bird Search GUI. Evaluates the algorithms for evolving bird recognition software.
 *
 * @author Olly Oechsle, University of Essex, Date: 19-Sep-2006
 * @version 1.0 28th April 2008
 */
public class BirdSearchGUI extends WindowSearchGUI {

    protected ObjectDetector leftFaces, rightFaces;

    public static void main(String[] args) throws Exception {

        AdaBoostSolution eyeSolution = AdaBoostSolution.load(new File("/home/ooechs/Desktop/birds/toucan/eyes.solution"));
        //AdaBoostSolution eyeSolution = AdaBoostSolution.load(new File("/home/ooechs/Desktop/birds/owl/eyes.solution"));

        AdaBoostSolution rightFacesSolution = AdaBoostSolution.load(new File("/home/ooechs/Desktop/birds/mandarin/right-faces.solution"));

        AdaBoostSolution leftFacesSolution = AdaBoostSolution.load(new File("/home/ooechs/Desktop/birds/mandarin/left-faces.solution"));

        new BirdSearchGUI(new File("/home/ooechs/Desktop/birds/toucan"), getDetector(eyeSolution), getDetector(rightFacesSolution), getDetector(leftFacesSolution));

    }

    public BirdSearchGUI(File imageDirectory, ObjectDetector eyeDetector, ObjectDetector rightFaces, ObjectDetector leftFaces) {

        super(imageDirectory, eyeDetector, "Bird Search GUI");

        this.rightFaces = rightFaces;

        this.leftFaces = leftFaces;

    }

    public void run() {

        super.run();

        Vector<ImageWindow> faceWindows = new Vector<ImageWindow>();

        for (int i = 0; i < windows.size(); i++) {
            ImageWindow eye = windows.elementAt(i);

            Vector<ImageWindow> leftWindows = new Vector<ImageWindow>();
            Vector<ImageWindow> rightWindows = new Vector<ImageWindow>();

            for (int height = 20; height < 80; height += 10) {
                for (int width = 20; width < 100; width +=10) {

                    // right face, eye is in the top left
                    haar.setWindowPosition(eye.left, eye.top, width, height, 10, 10);

                    int rightDecision = (int) rightFaces.calculate(haar);

                    if (rightDecision != DetectionProblem.NOT_OBJECT) {
                        rightWindows.add(new ImageWindow(eye.left, eye.top, width, height));
                    }

                    // left face, eye is in the top left
                    haar.setWindowPosition(eye.left - width + eye.width, eye.top, width, height, 10, 10);

                    int leftDecision = (int) leftFaces.calculate(haar);

                    if (leftDecision != DetectionProblem.NOT_OBJECT) {
                        leftWindows.add(new ImageWindow(eye.left - width + eye.width, eye.top, width, height));
                    }

                }
            }

            //if (leftWindows.size() == 0 && rightWindows.size() == 0) {
                if (leftWindows.size() > rightWindows.size())  {
                    ImageWindow smallestLeft = getSmallest(leftWindows);
                    if (smallestLeft != null) faceWindows.add(smallestLeft);
                } else {
                    ImageWindow smallestRight = getSmallest(rightWindows);
                    if (smallestRight != null) faceWindows.add(smallestRight);
                }
            //}

        }

        windows.addAll(faceWindows);

    }

    public ImageWindow getSmallest(Vector<ImageWindow> windows) {
        ImageWindow smallest = null;
        for (int i = 0; i < windows.size(); i++) {
            ImageWindow imageWindow = windows.elementAt(i);
            if (smallest == null || imageWindow.size() < smallest.size()) {
                smallest = imageWindow;
            }
        }
        return smallest;
    }

}
