View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.demos;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.util.HashMap;
35  import java.util.Map;
36  
37  import org.openimaj.feature.FloatFV;
38  import org.openimaj.io.IOUtils;
39  import org.openimaj.util.iterator.TextLineIterable;
40  
41  import com.jmatio.io.MatFileReader;
42  import com.jmatio.types.MLDouble;
43  import com.jmatio.types.MLSingle;
44  
45  public class FVFWExtractMatlabVectors {
46  
47  	public static void main(String[] args) throws IOException {
48  		final Map<Integer, String> database = readDatabase();
49  
50  		final File outbase = new File("/Users/jon/Data/lfw/matlab-fvs/");
51  
52  		for (int i = 1; i <= 128; i++) {
53  			final File chunk = new File(
54  					"/Users/jon/Downloads/data/lfw_aligned/SIFT_1pix_PCA64_GMM512/features/poolfv/1/", String.format(
55  							"feat_%d-v6.mat", i));
56  
57  			System.out.println(chunk);
58  
59  			final MatFileReader reader = new MatFileReader(chunk);
60  			final MLSingle feats = (MLSingle) reader.getMLArray("chunk");
61  			final MLDouble index = (MLDouble) reader.getMLArray("index");
62  
63  			for (int j = 0; j < index.getN(); j++) {
64  				final int id = (int) (double) index.get(0, j);
65  
66  				final File outfile = new File(outbase, database.get(id).replace(".jpg", ".bin"));
67  				outfile.getParentFile().mkdirs();
68  
69  				final float[] vec = new float[feats.getM()];
70  				for (int k = 0; k < feats.getM(); k++) {
71  					vec[k] = feats.get(k, j);
72  				}
73  
74  				final FloatFV fv = new FloatFV(vec);
75  				IOUtils.writeBinary(outfile, fv);
76  			}
77  		}
78  
79  	}
80  
81  	private static Map<Integer, String> readDatabase() {
82  		final HashMap<Integer, String> map = new HashMap<Integer, String>();
83  		for (final String line : new TextLineIterable(new File("/Users/jon/Downloads/data/shared/info/database.csv"))) {
84  			final String[] parts = line.split(",");
85  
86  			final String value = parts[0].trim();
87  			final int key = Integer.parseInt(parts[1].trim());
88  
89  			map.put(key, value);
90  		}
91  
92  		return map;
93  	}
94  }