View Javadoc

1   /**
2    * FaceTracker Licence
3    * -------------------
4    * (Academic, non-commercial, not-for-profit licence)
5    *
6    * Copyright (c) 2010 Jason Mora Saragih
7    * All rights reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions are met:
11   *
12   *     * The software is provided under the terms of this licence stricly for
13   *       academic, non-commercial, not-for-profit purposes.
14   *     * Redistributions of source code must retain the above copyright notice,
15   *       this list of conditions (licence) and the following disclaimer.
16   *     * Redistributions in binary form must reproduce the above copyright
17   *       notice, this list of conditions (licence) and the following disclaimer
18   *       in the documentation and/or other materials provided with the
19   *       distribution.
20   *     * The name of the author may not be used to endorse or promote products
21   *       derived from this software without specific prior written permission.
22   *     * As this software depends on other libraries, the user must adhere to and
23   *       keep in place any licencing terms of those libraries.
24   *     * Any publications arising from the use of this software, including but
25   *       not limited to academic journal and conference publications, technical
26   *       reports and manuals, must cite the following work:
27   *
28   *       J. M. Saragih, S. Lucey, and J. F. Cohn. Face Alignment through Subspace
29   *       Constrained Mean-Shifts. International Journal of Computer Vision
30   *       (ICCV), September, 2009.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
33   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
35   * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
37   * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
39   * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package com.jsaragih;
44  
45  import java.io.BufferedReader;
46  import java.io.BufferedWriter;
47  import java.io.FileNotFoundException;
48  import java.io.FileReader;
49  import java.io.FileWriter;
50  import java.io.IOException;
51  import java.util.Scanner;
52  
53  import org.openimaj.image.FImage;
54  
55  import Jama.Matrix;
56  
57  /**
58   * Multiple face checker
59   * 
60   * @author Jason Mora Saragih
61   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
62   */
63  public class MFCheck {
64  	static { Tracker.init(); }
65  	
66  	/** FCheck for each view */
67  	FCheck[] _fcheck;
68  
69  	static MFCheck load(final String fname) throws FileNotFoundException {
70  		BufferedReader br = null;
71  		try {
72  			br = new BufferedReader(new FileReader(fname));
73  			Scanner sc = new Scanner(br);
74  			return read(sc, true);
75  		} finally {
76  			try {
77  				br.close();
78  			} catch (IOException e) {
79  			}
80  		}
81  	}
82  
83  	void save(final String fname) throws IOException {
84  		BufferedWriter bw = null;
85  		try {
86  			bw = new BufferedWriter(new FileWriter(fname));
87  
88  			write(bw);
89  		} finally {
90  			try {
91  				if (bw != null)
92  					bw.close();
93  			} catch (IOException e) {
94  			}
95  		}
96  	}
97  
98  	void write(BufferedWriter s) throws IOException {
99  		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 }