001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.docs.tutorial.adv.faces.pipeeigen;
031
032import java.io.IOException;
033
034import org.openimaj.data.dataset.GroupedDataset;
035import org.openimaj.data.dataset.ListDataset;
036import org.openimaj.data.dataset.VFSGroupDataset;
037import org.openimaj.experiment.dataset.split.GroupedRandomSplitter;
038import org.openimaj.feature.DoubleFVComparison;
039import org.openimaj.image.FImage;
040import org.openimaj.image.ImageUtilities;
041import org.openimaj.image.processing.face.alignment.FaceAligner;
042import org.openimaj.image.processing.face.alignment.IdentityAligner;
043import org.openimaj.image.processing.face.detection.DetectedFace;
044import org.openimaj.image.processing.face.detection.FaceDetector;
045import org.openimaj.image.processing.face.detection.IdentityFaceDetector;
046import org.openimaj.image.processing.face.recognition.EigenFaceRecogniser;
047import org.openimaj.image.processing.face.recognition.FaceRecognitionEngine;
048
049/**
050 * OpenIMAJ Hello world!
051 * 
052 */
053public class App {
054        /**
055         * Main method
056         * 
057         * @param args
058         * @throws IOException
059         */
060        public static void main(String[] args) throws IOException {
061                /*
062                 * Load the data, and create some training and test data
063                 */
064                final VFSGroupDataset<FImage> dataset =
065                                new VFSGroupDataset<FImage>("zip:http://datasets.openimaj.org/att_faces.zip",
066                                                ImageUtilities.FIMAGE_READER);
067
068                final GroupedRandomSplitter<String, FImage> splits = new GroupedRandomSplitter<String, FImage>(dataset, 5, 0, 5);
069                final GroupedDataset<String, ListDataset<FImage>, FImage> training = splits.getTrainingDataset();
070                final GroupedDataset<String, ListDataset<FImage>, FImage> testing = splits.getTestDataset();
071
072                /*
073                 * Configure recogniser
074                 */
075                final FaceAligner<DetectedFace> aligner = new IdentityAligner<DetectedFace>();
076                final FaceDetector<DetectedFace, FImage> detector = new IdentityFaceDetector<FImage>();
077
078                final EigenFaceRecogniser<DetectedFace, String> recogniser =
079                                EigenFaceRecogniser.create(100, aligner, 1, DoubleFVComparison.EUCLIDEAN);
080
081                final FaceRecognitionEngine<DetectedFace, String> engine =
082                                new FaceRecognitionEngine<DetectedFace, String>(detector, recogniser);
083
084                /*
085                 * Train
086                 */
087                engine.train(training);
088
089                /*
090                 * Now we can test our performance on the test set
091                 */
092                double correct = 0, incorrect = 0;
093                for (final String truePerson : testing.getGroups()) {
094                        for (final FImage face : testing.get(truePerson)) {
095                                System.out.println(engine.recogniseBest(face));
096                                final String bestPerson = engine.recogniseBest(face).get(0).secondObject().annotation;
097
098                                System.out.println("Actual: " + truePerson + "\tguess: " + bestPerson);
099
100                                if (truePerson.equals(bestPerson))
101                                        correct++;
102                                else
103                                        incorrect++;
104                        }
105                }
106
107                System.out.println("Accuracy: " + (correct / (correct + incorrect)));
108        }
109}