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.util.ArrayList;
33  import java.util.List;
34  
35  import org.openimaj.image.Image;
36  import org.openimaj.tools.imagecollection.collection.ImageCollection;
37  import org.openimaj.tools.imagecollection.collection.ImageCollectionEntry;
38  import org.openimaj.tools.imagecollection.metamapper.MetaMapper;
39  import org.openimaj.tools.imagecollection.processor.ImageCollectionProcessor;
40  
41  public class ImageCollectionProcessorJob <T extends Image<?,T>> implements Runnable{
42  	
43  	public static class ProcessorJobEvent{
44  		public int imagesDone;
45  		public int imagesTotal;
46  		public boolean validTotal;
47  	}
48  	
49  	public static interface ProcessorJobListener{
50  		public void progressUpdate(ProcessorJobEvent event);
51  	}
52  	
53  	private ImageCollection<T> collection;
54  	private ImageCollectionProcessor<T> processor;
55  	private List<ProcessorJobListener> listeners;
56  	private MetaMapper mapper;
57  
58  	public ImageCollectionProcessorJob(
59  			ImageCollection<T> collection,
60  			ImageCollectionProcessor<T> sink,
61  			MetaMapper mapper
62  	){
63  		this.collection = collection;
64  		this.processor = sink;
65  		this.listeners = new ArrayList<ProcessorJobListener>();
66  		this.mapper = mapper;
67  	}
68  	
69  	public void addListener(ProcessorJobListener listener){
70  		this.listeners.add(listener);
71  	}
72  
73  	@Override
74  	public void run() {
75  		int done = 0;
76  		try {
77  			this.processor.start();
78  			this.mapper.start();
79  		} catch (Exception e) {
80  			return;
81  		}
82  		for(ImageCollectionEntry<T> entry: collection){
83  			try {
84  				if(entry.accepted)
85  				{
86  					String sinkOutput = this.processor.process(entry);
87  					mapper.mapItem(sinkOutput, entry);
88  				}
89  				ProcessorJobEvent event = new ProcessorJobEvent();
90  				event.imagesDone = ++done;
91  				event.imagesTotal = collection.countImages();
92  				if(event.imagesTotal < 0) event.validTotal = false;
93  				else event.validTotal = true;
94  				fireProgressUpdate(event);
95  			} catch (Exception e) {
96  			}
97  		}
98  		try {
99  			this.mapper.end();
100 			this.processor.end();
101 		} catch (Exception e) {
102 		}
103 	}
104 	
105 	
106 	
107 	private void fireProgressUpdate(ProcessorJobEvent event) {
108 		for (ProcessorJobListener listener : this.listeners) {
109 			listener.progressUpdate(event);
110 		}
111 	}
112 	
113 	
114 }