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.BufferedWriter;
46  import java.io.FileInputStream;
47  import java.io.FileNotFoundException;
48  import java.io.IOException;
49  import java.io.InputStream;
50  import java.util.Locale;
51  import java.util.Scanner;
52  
53  import org.openimaj.image.FImage;
54  
55  import Jama.Matrix;
56  
57  /**
58   * IO Utilities
59   * 
60   * @author Jason Mora Saragih
61   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
62   */
63  public class IO {
64  	static {
65  		Tracker.init();
66  	}
67  
68  	/**
69  	 * Types of object
70  	 */
71  	public enum Types {
72  		/** PDM */
73  		PDM,
74  		/** PAW */
75  		PAW,
76  		/** PATCH */
77  		PATCH,
78  		/** MPATCH */
79  		MPATCH,
80  		/** CLM */
81  		CLM,
82  		/** FDET */
83  		FDET,
84  		/** FCHECK */
85  		FCHECK,
86  		/** MFCHECK */
87  		MFCHECK,
88  		/** TRACKER */
89  		TRACKER
90  	};
91  
92  	/**
93  	 * Read a matrix
94  	 * 
95  	 * @param s
96  	 * @return the matrix
97  	 */
98  	public static Matrix readMat(Scanner s) {
99  		s.useLocale(Locale.UK);
100 
101 		final int r = s.nextInt();
102 		final int c = s.nextInt();
103 		s.nextInt(); // types are ignored
104 
105 		final Matrix M = new Matrix(r, c);
106 		final double[][] Mv = M.getArray();
107 
108 		for (int rr = 0; rr < r; rr++)
109 			for (int cc = 0; cc < c; cc++)
110 				Mv[rr][cc] = s.nextDouble();
111 
112 		return M;
113 	}
114 
115 	static void writeMat(BufferedWriter s, Matrix M) throws IOException {
116 		final int r = M.getRowDimension();
117 		final int c = M.getColumnDimension();
118 
119 		s.write(r + " " + c + " 0"); // type always 0 for java version as its
120 										// ignored
121 
122 		final double[][] Mv = M.getArray();
123 		for (int rr = 0; rr < r; rr++)
124 			for (int cc = 0; cc < c; cc++)
125 				s.write(Mv[rr][cc] + " ");
126 	}
127 
128 	static void writeImg(BufferedWriter s, FImage img) throws IOException {
129 		final int r = img.height;
130 		final int c = img.width;
131 
132 		s.write(r + " " + c + " 0"); // type always 0 for java version as its
133 										// ignored
134 
135 		final float[][] Mv = img.pixels;
136 		for (int rr = 0; rr < r; rr++)
137 			for (int cc = 0; cc < c; cc++)
138 				s.write(Mv[rr][cc] + " ");
139 	}
140 
141 	static FImage readImg(Scanner s) {
142 		s.useLocale(Locale.UK);
143 
144 		final int r = s.nextInt();
145 		final int c = s.nextInt();
146 		s.nextInt(); // types are ignored
147 
148 		final FImage M = new FImage(c, r);
149 		final float[][] Mv = M.pixels;
150 
151 		for (int rr = 0; rr < r; rr++)
152 			for (int cc = 0; cc < c; cc++)
153 				Mv[rr][cc] = s.nextFloat();
154 
155 		return M;
156 	}
157 
158 	static void writeIntArray(BufferedWriter s, int[][] arr) throws IOException {
159 		final int r = arr.length;
160 		final int c = arr[0].length;
161 
162 		s.write(r + " " + c + " 0"); // type always 0 for java version as its
163 										// ignored
164 
165 		for (int rr = 0; rr < r; rr++)
166 			for (int cc = 0; cc < c; cc++)
167 				s.write(arr[rr][cc] + " ");
168 	}
169 
170 	static int[][] readIntArray(Scanner s) {
171 		s.useLocale(Locale.UK);
172 
173 		final int r = s.nextInt();
174 		final int c = s.nextInt();
175 		s.nextInt(); // types are ignored
176 
177 		final int[][] M = new int[r][c];
178 		for (int rr = 0; rr < r; rr++)
179 			for (int cc = 0; cc < c; cc++)
180 				M[rr][cc] = s.nextInt();
181 
182 		return M;
183 	}
184 
185 	static int[][] loadCon(final String fname) throws FileNotFoundException {
186 		return loadCon(new FileInputStream(fname));
187 	}
188 
189 	/**
190 	 * Load connections
191 	 * 
192 	 * @param in
193 	 * @return the connections
194 	 */
195 	public static int[][] loadCon(final InputStream in) {
196 		final Scanner s = new Scanner(in);
197 		s.useLocale(Locale.UK);
198 
199 		while (true) {
200 			final String str = s.next();
201 			if ("n_connections:".equals(str))
202 				break;
203 		}
204 
205 		final int n = s.nextInt();
206 		final int[][] con = new int[2][n];
207 
208 		while (true) {
209 			final String c = s.next();
210 			if (c.equals("{"))
211 				break;
212 		}
213 
214 		for (int i = 0; i < n; i++) {
215 			con[0][i] = s.nextInt();
216 			con[1][i] = s.nextInt();
217 		}
218 		s.close();
219 
220 		return con;
221 	}
222 
223 	static int[][] loadTri(final String fname) throws FileNotFoundException {
224 		return loadTri(new FileInputStream(fname));
225 	}
226 
227 	/**
228 	 * Load triangles
229 	 * 
230 	 * @param in
231 	 * @return triangles
232 	 */
233 	public static int[][] loadTri(final InputStream in) {
234 		final Scanner s = new Scanner(in);
235 		s.useLocale(Locale.UK);
236 
237 		while (true) {
238 			final String str = s.next();
239 			if ("n_tri:".equals(str))
240 				break;
241 		}
242 
243 		final int n = s.nextInt();
244 		final int[][] tri = new int[n][3];
245 
246 		while (true) {
247 			final String c = s.next();
248 			if (c.equals("{"))
249 				break;
250 		}
251 
252 		for (int i = 0; i < n; i++) {
253 			tri[i][0] = s.nextInt();
254 			tri[i][1] = s.nextInt();
255 			tri[i][2] = s.nextInt();
256 		}
257 		s.close();
258 
259 		return tri;
260 	}
261 
262 	static FImage readImgByte(Scanner s) {
263 		s.useLocale(Locale.UK);
264 
265 		final int r = s.nextInt();
266 		final int c = s.nextInt();
267 		s.nextInt(); // types are ignored
268 
269 		final FImage M = new FImage(c, r);
270 		final float[][] Mv = M.pixels;
271 
272 		for (int rr = 0; rr < r; rr++)
273 			for (int cc = 0; cc < c; cc++)
274 				Mv[rr][cc] = s.next("[^ ]").codePointAt(0); // Integer.parseInt(s.next());//s.nextByte();
275 
276 		return M;
277 	}
278 }