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.feature;
031
032import java.io.DataInput;
033import java.io.DataOutput;
034import java.io.IOException;
035
036import org.openimaj.feature.FeatureVectorProvider;
037import org.openimaj.feature.FloatFV;
038import org.openimaj.image.FImage;
039import org.openimaj.image.feature.FImage2FloatFV;
040import org.openimaj.image.processing.face.alignment.FaceAligner;
041import org.openimaj.image.processing.face.alignment.ScalingAligner;
042import org.openimaj.image.processing.face.detection.DetectedFace;
043import org.openimaj.io.IOUtils;
044
045/**
046 * A {@link FacialFeature} that is just the pixel values
047 * of a (possibly aligned) face detection. 
048 * 
049 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
050 *
051 */
052public class FaceImageFeature implements FacialFeature, FeatureVectorProvider<FloatFV> {
053        /**
054         * A {@link FacialFeatureExtractor} for producing {@link FaceImageFeature}s.
055         * 
056         * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
057         * 
058         * @param <T> Type of {@link DetectedFace}
059         */
060        public static class Extractor<T extends DetectedFace> implements FacialFeatureExtractor<FaceImageFeature, T> {
061                FaceAligner<T> aligner;
062                
063                /**
064                 * Construct with a {@link ScalingAligner} with its default resolution
065                 */
066                public Extractor() {
067                        this(new ScalingAligner<T>());
068                }
069                
070                /**
071                 * Construct with an aligner
072                 * @param aligner the aligner
073                 */
074                public Extractor(FaceAligner<T> aligner) {
075                        this.aligner = aligner;
076                }
077                
078                @Override
079                public FaceImageFeature extractFeature(T face) {
080                        FImage faceImage = aligner.align(face);
081                        FloatFV feature = FImage2FloatFV.INSTANCE.extractFeature(faceImage);
082                        
083                        return new FaceImageFeature(feature);
084                }
085
086                @Override
087                public void readBinary(DataInput in) throws IOException {
088                        String alignerClass = in.readUTF();
089                        aligner = IOUtils.newInstance(alignerClass);
090                        aligner.readBinary(in);
091                }
092
093                @Override
094                public byte[] binaryHeader() {
095                        return this.getClass().getName().getBytes();
096                }
097
098                @Override
099                public void writeBinary(DataOutput out) throws IOException {
100                        out.writeUTF(aligner.getClass().getName());
101                        aligner.writeBinary(out);
102                }
103        }
104
105        private FloatFV feature;
106        
107        /**
108         * Construct with the given feature
109         * 
110         * @param feature
111         */
112        public FaceImageFeature(FloatFV feature) {
113                this.feature = feature;
114        }
115
116        @Override
117        public void readBinary(DataInput in) throws IOException {
118                feature = new FloatFV();
119                feature.readBinary(in);
120        }
121
122        @Override
123        public byte[] binaryHeader() {
124                return this.getClass().getName().getBytes();
125        }
126
127        @Override
128        public void writeBinary(DataOutput out) throws IOException {
129                feature.writeBinary(out);
130        }
131
132        @Override
133        public FloatFV getFeatureVector() {
134                return feature;
135        }
136}