1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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 }