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.detection;
031
032import java.util.List;
033
034import org.openimaj.data.dataset.GroupedDataset;
035import org.openimaj.data.dataset.ListBackedDataset;
036import org.openimaj.data.dataset.ListDataset;
037import org.openimaj.data.dataset.MapBackedDataset;
038import org.openimaj.image.Image;
039
040/**
041 * Convenience methods for dealing with face detections in datasets and lists of
042 * images.
043 * 
044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
045 * 
046 */
047public class DatasetFaceDetector {
048        private DatasetFaceDetector() {
049        }
050
051        /**
052         * Apply a face detector to all the images in the given dataset, choosing
053         * only the biggest face if multiple are found.
054         * 
055         * @param <PERSON>
056         *            Type representing a person
057         * @param <IMAGE>
058         *            Type of image
059         * @param <FACE>
060         *            Type of {@link DetectedFace} extracted
061         * @param input
062         *            The input dataset
063         * @param detector
064         *            The face detector
065         * @return a dataset of detected faces.
066         */
067        public static <PERSON, IMAGE extends Image<?, IMAGE>, FACE extends DetectedFace>
068                        GroupedDataset<PERSON, ListDataset<FACE>, FACE>
069                        process(GroupedDataset<PERSON, ? extends ListDataset<IMAGE>, IMAGE> input, FaceDetector<FACE, IMAGE> detector)
070        {
071                final MapBackedDataset<PERSON, ListDataset<FACE>, FACE> output = new MapBackedDataset<PERSON, ListDataset<FACE>, FACE>();
072
073                for (final PERSON group : input.getGroups()) {
074                        final ListBackedDataset<FACE> detected = new ListBackedDataset<FACE>();
075                        final ListDataset<IMAGE> instances = input.getInstances(group);
076
077                        for (int i = 0; i < instances.size(); i++) {
078                                final IMAGE img = instances.getInstance(i);
079                                final List<FACE> faces = detector.detectFaces(img);
080
081                                if (faces == null || faces.size() == 0) {
082                                        System.err.println("There was no face detected in " + group + " instance " + i);
083                                        // detected.add(null);
084                                        continue;
085                                }
086
087                                if (faces.size() == 1) {
088                                        detected.add(faces.get(0));
089                                        continue;
090                                }
091
092                                detected.add(getBiggest(faces));
093                        }
094
095                        output.getMap().put(group, detected);
096                }
097
098                return output;
099        }
100
101        /**
102         * Apply a face detector to all the images in the given dataset, choosing
103         * only the biggest face if multiple are found.
104         * 
105         * @param <IMAGE>
106         *            Type of image
107         * @param <FACE>
108         *            Type of {@link DetectedFace} extracted
109         * @param instances
110         *            The input faces
111         * @param detector
112         *            The face detector
113         * @return a dataset of detected faces.
114         */
115        public static <IMAGE extends Image<?, IMAGE>, FACE extends DetectedFace>
116                        ListDataset<FACE>
117                        process(List<IMAGE> instances, FaceDetector<FACE, IMAGE> detector)
118        {
119                final ListBackedDataset<FACE> detected = new ListBackedDataset<FACE>();
120
121                for (int i = 0; i < instances.size(); i++) {
122                        final IMAGE img = instances.get(i);
123                        final List<FACE> faces = detector.detectFaces(img);
124
125                        if (faces == null || faces.size() == 0) {
126                                System.err.println("There was no face detected in instance " + i);
127                                // detected.add(null);
128                                continue;
129                        }
130
131                        if (faces.size() == 1) {
132                                detected.add(faces.get(0));
133                                continue;
134                        }
135
136                        detected.add(getBiggest(faces));
137                }
138
139                return detected;
140        }
141
142        /**
143         * Get the biggest face (by area) from the list
144         * 
145         * @param <FACE>
146         *            Type of {@link DetectedFace}
147         * @param faces
148         *            the list of faces
149         * @return the biggest face or null if the list is null or empty
150         */
151        public static <FACE extends DetectedFace> FACE getBiggest(List<FACE> faces) {
152                if (faces == null || faces.size() == 0)
153                        return null;
154
155                int biggestIndex = 0;
156                double biggestSize = faces.get(0).bounds.calculateArea();
157
158                for (int i = 1; i < faces.size(); i++) {
159                        final double sz = faces.get(i).bounds.calculateArea();
160                        if (sz > biggestSize) {
161                                biggestSize = sz;
162                                biggestIndex = i;
163                        }
164                }
165
166                return faces.get(biggestIndex);
167        }
168}