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.io.IOException;
33  import java.util.ArrayList;
34  import java.util.List;
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.ReadableListDataset;
41  import org.openimaj.data.identity.Identifiable;
42  import org.openimaj.io.ObjectReader;
43  
44  /**
45   *
46   * @param <IMAGE>
47   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
48   */
49  public class UKBenchListDataset<IMAGE> extends ReadableListDataset<IMAGE, FileObject> implements Identifiable{
50  	private int object;
51  	private List<String> ids;
52  	private FileObject base;
53  	
54  	/**
55  	 * @param path
56  	 * @param reader
57  	 * @param object
58  	 */
59  	public UKBenchListDataset(String path, ObjectReader<IMAGE, FileObject> reader, int object) {
60  		super(reader);
61  		this.object = object;
62  		this.ids = heldIDs();
63  		FileSystemManager fsManager;
64  		try {
65  			fsManager = VFS.getManager();
66  			this.base = fsManager.resolveFile(path);
67  		} catch (FileSystemException e) {
68  			throw new RuntimeException(e);
69  		}
70  	}
71  	@Override
72  	public IMAGE getInstance(int index) {
73  		FileObject fo = null; 
74  		try {
75  			fo = createFileObject(this.ids.get(index));
76  			return this.reader.read(fo);
77  		} catch (IOException e) {
78  			throw new RuntimeException(e);
79  		} finally {
80  			if(fo!=null){
81  				try {
82  					fo.close();
83  				} catch (FileSystemException e) {
84  					throw new RuntimeException(e);
85  				}
86  			}
87  		}
88  	}
89  
90  	private FileObject createFileObject(String string) {
91  		try {
92  			FileObject child = base.getChild(string);
93  			return child;
94  		} catch (FileSystemException e) {
95  			throw new RuntimeException(e);
96  		}
97  	}
98  	@Override
99  	public int numInstances() {
100 		return  4;
101 	}
102 
103 	@Override
104 	public String getID() {
105 		List<String> ids = heldIDs();
106 		return String.format("UKBench{%s}",ids.toString());
107 	}
108 	private List<String> heldIDs() {
109 		List<String> ids = new ArrayList<String>();
110 		ids.add(String.format("ukbench%05d.jpg",object * 4 + 0));
111 		ids.add(String.format("ukbench%05d.jpg",object * 4 + 1));
112 		ids.add(String.format("ukbench%05d.jpg",object * 4 + 2));
113 		ids.add(String.format("ukbench%05d.jpg",object * 4 + 3));
114 		return ids;
115 	}
116 	/**
117 	 * @return the objects this group represents
118 	 */
119 	public int getObject() {
120 		return object;
121 	}
122 	
123 	
124 
125 }