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.experiment.gmm.retrieval;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  import java.util.Set;
35  
36  import org.apache.commons.vfs2.FileObject;
37  import org.apache.commons.vfs2.FileSystemException;
38  import org.apache.commons.vfs2.FileSystemManager;
39  import org.apache.commons.vfs2.VFS;
40  import org.openimaj.data.dataset.GroupedDataset;
41  import org.openimaj.data.dataset.ReadableGroupDataset;
42  import org.openimaj.data.identity.Identifiable;
43  import org.openimaj.io.ObjectReader;
44  
45  /**
46   * A {@link GroupedDataset} of {@link UKBenchListDataset}s instances each of an
47   * item in the UKBench experiment.
48   *
49   * UKBench can be provided in any form supported by {@link VFS}
50   *
51   * The UKBench files must be in one flat directory and named "ukbenchXXXXX.jpg"
52   *
53   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
54   *
55   * @param <IMAGE>
56   *            The type of IMAGE in the dataset
57   */
58  public class UKBenchGroupDataset<IMAGE>
59  		extends
60  		ReadableGroupDataset<Integer, UKBenchListDataset<IMAGE>, IMAGE, FileObject>
61  		implements
62  		Identifiable
63  {
64  	private static final int UKBENCH_OBJECTS = 2550;
65  	private Map<Integer, UKBenchListDataset<IMAGE>> ukbenchObjects;
66  	private FileObject base;
67  
68  	/**
69  	 * @param path
70  	 * @param reader
71  	 */
72  	public UKBenchGroupDataset(String path, ObjectReader<IMAGE, FileObject> reader) {
73  		super(reader);
74  		this.ukbenchObjects = new HashMap<Integer, UKBenchListDataset<IMAGE>>();
75  		FileSystemManager manager;
76  		try {
77  			manager = VFS.getManager();
78  			this.base = manager.resolveFile(path);
79  		} catch (final FileSystemException e) {
80  			throw new RuntimeException(e);
81  		}
82  
83  		for (int i = 0; i < UKBENCH_OBJECTS; i++) {
84  			this.ukbenchObjects.put(i, new UKBenchListDataset<IMAGE>(path, reader, i));
85  		}
86  	}
87  
88  	@Override
89  	public String toString() {
90  		return String.format("%s(%d groups with a total of %d instances)", this.getClass().getName(), this.size(),
91  				this.numInstances());
92  	}
93  
94  	@Override
95  	public String getID() {
96  		return base.getName().getBaseName();
97  	}
98  
99  	@Override
100 	public Set<java.util.Map.Entry<Integer, UKBenchListDataset<IMAGE>>> entrySet() {
101 		return ukbenchObjects.entrySet();
102 	}
103 
104 }