001/**
002 * FaceTracker Licence
003 * -------------------
004 * (Academic, non-commercial, not-for-profit licence)
005 *
006 * Copyright (c) 2010 Jason Mora Saragih
007 * All rights reserved.
008 *
009 * Redistribution and use in source and binary forms, with or without
010 * modification, are permitted provided that the following conditions are met:
011 *
012 *     * The software is provided under the terms of this licence stricly for
013 *       academic, non-commercial, not-for-profit purposes.
014 *     * Redistributions of source code must retain the above copyright notice,
015 *       this list of conditions (licence) and the following disclaimer.
016 *     * Redistributions in binary form must reproduce the above copyright
017 *       notice, this list of conditions (licence) and the following disclaimer
018 *       in the documentation and/or other materials provided with the
019 *       distribution.
020 *     * The name of the author may not be used to endorse or promote products
021 *       derived from this software without specific prior written permission.
022 *     * As this software depends on other libraries, the user must adhere to and
023 *       keep in place any licencing terms of those libraries.
024 *     * Any publications arising from the use of this software, including but
025 *       not limited to academic journal and conference publications, technical
026 *       reports and manuals, must cite the following work:
027 *
028 *       J. M. Saragih, S. Lucey, and J. F. Cohn. Face Alignment through Subspace
029 *       Constrained Mean-Shifts. International Journal of Computer Vision
030 *       (ICCV), September, 2009.
031 *
032 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
033 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
034 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
035 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
036 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
037 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
038 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
039 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
040 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
041 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042 */
043package com.jsaragih;
044
045import java.io.BufferedReader;
046import java.io.BufferedWriter;
047import java.io.FileNotFoundException;
048import java.io.FileReader;
049import java.io.FileWriter;
050import java.io.IOException;
051import java.util.Scanner;
052
053import org.openimaj.image.FImage;
054
055import Jama.Matrix;
056
057/**
058 * Multiple face checker
059 * 
060 * @author Jason Mora Saragih
061 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
062 */
063public class MFCheck {
064        static { Tracker.init(); }
065        
066        /** FCheck for each view */
067        FCheck[] _fcheck;
068
069        static MFCheck load(final String fname) throws FileNotFoundException {
070                BufferedReader br = null;
071                try {
072                        br = new BufferedReader(new FileReader(fname));
073                        Scanner sc = new Scanner(br);
074                        return read(sc, true);
075                } finally {
076                        try {
077                                br.close();
078                        } catch (IOException e) {
079                        }
080                }
081        }
082
083        void save(final String fname) throws IOException {
084                BufferedWriter bw = null;
085                try {
086                        bw = new BufferedWriter(new FileWriter(fname));
087
088                        write(bw);
089                } finally {
090                        try {
091                                if (bw != null)
092                                        bw.close();
093                        } catch (IOException e) {
094                        }
095                }
096        }
097
098        void write(BufferedWriter s) throws IOException {
099                s.write(IO.Types.MFCHECK.ordinal() + " " + _fcheck.length + " ");
100
101                for (int i = 0; i < _fcheck.length; i++)
102                        _fcheck[i].write(s);
103        }
104
105        /**
106         * Read the a {@link MFCheck}
107         * 
108         * @param s
109         * @param readType
110         * @return the {@link MFCheck} 
111         */
112        public static MFCheck read(Scanner s, boolean readType) {
113                if (readType) {
114                        int type = s.nextInt();
115                        assert (type == IO.Types.MFCHECK.ordinal());
116                }
117
118                MFCheck mfcheck = new MFCheck();
119
120                int n = s.nextInt();
121                mfcheck._fcheck = new FCheck[n];
122
123                for (int i = 0; i < n; i++)
124                        mfcheck._fcheck[i] = FCheck.read(s, true);
125
126                return mfcheck;
127        }
128
129        /**
130         * Check the whether its actually a face
131         * @param idx
132         * @param im
133         * @param s
134         * @return true if face; false otherwise
135         */
136        public boolean check(int idx, FImage im, Matrix s) {
137                assert ((idx >= 0) && (idx < _fcheck.length));
138
139                return _fcheck[idx].check(im, s);
140        }
141}