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.image.processing.face.recognition;
031
032import java.io.DataInput;
033import java.io.DataOutput;
034import java.io.IOException;
035import java.util.ArrayList;
036import java.util.Collection;
037import java.util.List;
038import java.util.Set;
039
040import org.openimaj.feature.FeatureExtractor;
041import org.openimaj.image.processing.face.detection.DetectedFace;
042import org.openimaj.io.IOUtils;
043import org.openimaj.ml.annotation.Annotated;
044import org.openimaj.ml.annotation.IncrementalAnnotator;
045import org.openimaj.ml.annotation.RestrictedAnnotator;
046import org.openimaj.ml.annotation.ScoredAnnotation;
047
048/**
049 * A {@link FaceRecogniser} built on top of an {@link IncrementalAnnotator}.
050 * This class essentially adapts standard {@link IncrementalAnnotator} to work
051 * in the face recognition scenario.
052 * 
053 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
054 * 
055 * @param <FACE>
056 *            Type of {@link DetectedFace}
057 * @param <PERSON>
058 *            Type of object representing a person
059 */
060public class AnnotatorFaceRecogniser<FACE extends DetectedFace, PERSON>
061                extends
062                FaceRecogniser<FACE, PERSON>
063{
064        protected IncrementalAnnotator<FACE, PERSON> annotator;
065
066        protected AnnotatorFaceRecogniser() {
067        }
068
069        /**
070         * Construct with the given underlying annotator.
071         * 
072         * @param annotator
073         *            the annotator
074         */
075        public AnnotatorFaceRecogniser(IncrementalAnnotator<FACE, PERSON> annotator) {
076                this.annotator = annotator;
077        }
078
079        /**
080         * Convenience method to create {@link AnnotatorFaceRecogniser} instances
081         * from an annotator.
082         * 
083         * @param <FACE>
084         *            Type of {@link DetectedFace}
085         * @param <EXTRACTOR>
086         *            Type of {@link FeatureExtractor}
087         * @param <PERSON>
088         *            Type of object representing a person
089         * @param annotator
090         *            the annotator
091         * @return the new {@link AnnotatorFaceRecogniser} instance
092         */
093        public static <FACE extends DetectedFace, EXTRACTOR extends FeatureExtractor<?, FACE>, PERSON>
094                        AnnotatorFaceRecogniser<FACE, PERSON> create(
095                                        IncrementalAnnotator<FACE, PERSON> annotator)
096        {
097                return new AnnotatorFaceRecogniser<FACE, PERSON>(annotator);
098        }
099
100        @Override
101        public void readBinary(DataInput in) throws IOException {
102                annotator = IOUtils.read(in);
103        }
104
105        @Override
106        public byte[] binaryHeader() {
107                return "FREC".getBytes();
108        }
109
110        @Override
111        public void writeBinary(DataOutput out) throws IOException {
112                IOUtils.write(annotator, out);
113        }
114
115        @SuppressWarnings("unchecked")
116        @Override
117        public List<ScoredAnnotation<PERSON>> annotate(FACE object, Collection<PERSON> restrict) {
118                if (annotator instanceof RestrictedAnnotator) {
119                        return ((RestrictedAnnotator<FACE, PERSON>) annotator).annotate(object, restrict);
120                }
121
122                final List<ScoredAnnotation<PERSON>> pot = annotator.annotate(object);
123
124                if (pot == null || pot.size() == 0)
125                        return null;
126
127                final List<ScoredAnnotation<PERSON>> toKeep = new ArrayList<ScoredAnnotation<PERSON>>();
128
129                for (final ScoredAnnotation<PERSON> p : pot) {
130                        if (restrict.contains(p.annotation))
131                                toKeep.add(p);
132                }
133
134                return toKeep;
135        }
136
137        @Override
138        public List<ScoredAnnotation<PERSON>> annotate(FACE object) {
139                return annotator.annotate(object);
140        }
141
142        @Override
143        public void train(Annotated<FACE, PERSON> annotedImage) {
144                annotator.train(annotedImage);
145        }
146
147        @Override
148        public void train(Iterable<? extends Annotated<FACE, PERSON>> data) {
149                annotator.train(data);
150        }
151
152        @Override
153        public Set<PERSON> getAnnotations() {
154                return annotator.getAnnotations();
155        }
156
157        @Override
158        public void reset() {
159                annotator.reset();
160        }
161
162        @Override
163        public String toString() {
164                return String.format("AnnotatorFaceRecogniser[recogniser=%s]", this.annotator);
165        }
166}