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.tools.imagecollection.tool;
31  
32  import java.io.File;
33  import java.io.IOException;
34  
35  import org.kohsuke.args4j.CmdLineException;
36  import org.kohsuke.args4j.CmdLineParser;
37  import org.kohsuke.args4j.Option;
38  import org.kohsuke.args4j.ProxyOptionHandler;
39  import org.openimaj.image.Image;
40  import org.openimaj.image.MBFImage;
41  import org.openimaj.io.IOUtils;
42  import org.openimaj.tools.imagecollection.collection.ImageCollection;
43  import org.openimaj.tools.imagecollection.collection.ImageCollectionSetupException;
44  import org.openimaj.tools.imagecollection.collection.config.ImageCollectionConfig;
45  import org.openimaj.tools.imagecollection.collection.config.ImageCollectionMode;
46  import org.openimaj.tools.imagecollection.collection.config.ImageCollectionProcessorMode;
47  import org.openimaj.tools.imagecollection.collection.config.MetaMapperMode;
48  import org.openimaj.tools.imagecollection.metamapper.MetaMapper;
49  import org.openimaj.tools.imagecollection.processor.ImageCollectionProcessor;
50  import org.openimaj.tools.imagecollection.tool.ImageCollectionProcessorJob.ProcessorJobEvent;
51  import org.openimaj.tools.imagecollection.tool.ImageCollectionProcessorJob.ProcessorJobListener;
52  
53  public class ImageCollectionTool<T extends Image<?,T>> implements ProcessorJobListener {
54  	@Option(name="--input", aliases="-i", required=false, usage="Input Config File (json)", metaVar="STRING")
55  	private String input = null;
56  	
57  	@Option(name="--input-string", aliases="-is", required=false, usage="Input Config String (json).", metaVar="STRING")
58  	private String inputString = null;
59  	
60  	@Option(name="--output-mode", aliases="-om", required=false, usage="Image Collection output mode", handler=ProxyOptionHandler.class)
61  	private ImageCollectionProcessorMode processorMode = ImageCollectionProcessorMode.DIR;
62  	private ImageCollectionProcessorMode.ModeOp processorModeOp;
63  	private ImageCollectionProcessor<MBFImage> processor = null;
64  	
65  	@Option(name="--collection-mode", aliases="-cm", required=false, usage="Image Collection to pass json to")
66  	private ImageCollectionMode collectionMode = null;
67  	private ImageCollection<MBFImage> collection = null;
68  	
69  	@Option(name="--mapper-mode", aliases="-mm", required=false, usage="Imge Collection entry metadata mapper", handler=ProxyOptionHandler.class)
70  	private MetaMapperMode mapperMode = MetaMapperMode.FILE;
71  	private MetaMapper metaMapper;
72  	
73  	public void setup() throws IOException, ImageCollectionSetupException{
74  		ImageCollectionConfig config;
75  		if(input!= null){
76  			config = IOUtils.read(new File(input),ImageCollectionConfig.class);
77  		}
78  		else if(inputString!= null){
79  			config = new ImageCollectionConfig(inputString);
80  		}
81  		else{
82  			try{
83  				config = IOUtils.read(System.in,ImageCollectionConfig.class);
84  			}
85  			catch(ClassCastException t){
86  				throw new IOException("Not parseable!");
87  			}
88  		}
89  		
90  		if(collectionMode==null){
91  			collection = ImageCollectionMode.guessType(config);
92  		}
93  		else{
94  			collection = collectionMode.initCollection(config);
95  		}
96  		
97  		if(collection == null){
98  			throw new IOException("Could not read collection");
99  		}
100 		
101 		this.processor = processorModeOp.processor();
102 		this.metaMapper = mapperMode.mapper(this.processor);
103 	}
104 	
105 	private void run() {
106 		ImageCollectionProcessorJob<MBFImage> job = new ImageCollectionProcessorJob<MBFImage>(
107 				this.collection,
108 				this.processor,
109 				this.metaMapper
110 		);
111 		job.addListener(this);
112 		Thread t = new Thread(job);
113 		t.start();
114 	}
115 	
116 	public static void main(String args[]){
117 		ImageCollectionTool<MBFImage> tool = new ImageCollectionTool<MBFImage>();
118 		CmdLineParser parser = new CmdLineParser(tool);
119 		
120 	    try {
121 		    parser.parseArgument(args);
122 		    tool.setup();
123 			tool.run();
124 		} catch(CmdLineException e) {
125 		    parserError(parser,e);
126 		} catch (IOException e) {
127 			parserError(parser,e);
128 		} catch (ImageCollectionSetupException e) {
129 			parserError(parser,e);
130 		}
131 		
132 		
133 	}
134 
135 	private static void parserError(CmdLineParser parser, Exception e) {
136 		System.err.println(e.getMessage());
137 	    System.err.println("Usage: java -jar ImageCollectionTool.jar [arguments]");
138 	    parser.printUsage(System.err);
139 	    return;
140 	}
141 
142 	@Override
143 	public void progressUpdate(ProcessorJobEvent event) {
144 		System.out.print("\r");
145 		StringBuilder progress = new StringBuilder();
146 		int progressLen = 100;
147 		if(event.validTotal){
148 			
149 			double progressProp = (double)event.imagesDone / (double)event.imagesTotal;
150 			progress.append(String.format("%s/%s",event.imagesDone,event.imagesTotal));
151 			progress.append('[');
152 			int currentProgress = (int) (progressLen * progressProp);
153 			for(int i = 0; i < currentProgress; i++){
154 				progress.append('#');
155 			}
156 			for(int i = currentProgress; i < progressLen; i++){
157 				progress.append(' ');
158 			}
159 		}
160 		else{
161 			progress.append(String.format("%s/%s",event.imagesDone,"?"));
162 			progress.append('[');
163 			int hashIndex = event.imagesDone % progressLen;
164 			if((event.imagesDone / progressLen) % 2 == 1){
165 				hashIndex = progressLen - hashIndex;
166 			}
167 			
168 			for(int i = 0; i < progressLen; i++){
169 				if(i == hashIndex)
170 					progress.append('#');
171 				else
172 					progress.append(' ');
173 			}
174 		}
175 		progress.append(']');
176 		System.out.print(progress.toString());
177 	}
178 }