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.collection.video;
31  
32  import java.io.File;
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  import java.text.ParseException;
36  import java.util.ArrayList;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  import org.openimaj.image.MBFImage;
41  import org.openimaj.tools.imagecollection.collection.ImageCollection;
42  import org.openimaj.tools.imagecollection.collection.ImageCollectionEntry;
43  import org.openimaj.tools.imagecollection.collection.ImageCollectionEntrySelection;
44  import org.openimaj.tools.imagecollection.collection.ImageCollectionSetupException;
45  import org.openimaj.tools.imagecollection.collection.config.ImageCollectionConfig;
46  import org.openimaj.tools.imagecollection.collection.video.selection.XuggleVideoFrameSelection;
47  import org.openimaj.video.xuggle.XuggleVideo;
48  
49  public abstract class XuggleVideoImageCollection implements ImageCollection<MBFImage>{
50  	public XuggleVideo video;
51  	private XuggleVideoFrameSelection frameStyle;
52  
53  	@Override
54  	public Iterator<ImageCollectionEntry<MBFImage>> iterator() {
55  		frameStyle.init(video);
56  		return new MetadataVideoIterator<MBFImage>(frameStyle,video);
57  	}
58  	
59  	@Override
60  	public void setEntrySelection(ImageCollectionEntrySelection<MBFImage> selection){
61  		if(selection instanceof XuggleVideoFrameSelection)
62  			frameStyle = (XuggleVideoFrameSelection) selection;
63  		else
64  			frameStyle = new XuggleVideoFrameSelection.Proxy(selection);
65  	}
66  
67  	@Override
68  	public void setup(ImageCollectionConfig config) throws ImageCollectionSetupException {
69  		if(useable(config) < 0) throw new ImageCollectionSetupException("Can't use config as collection");
70  		String videoEntry;
71  		try {
72  			videoEntry = config.read(videoTag());
73  		} catch (ParseException e) {
74  			throw new ImageCollectionSetupException(e);
75  		}
76  		this.video = loadXuggleVideo(videoEntry);
77  		if(this.video == null){
78  			throw new ImageCollectionSetupException("Failed to load youtube video");
79  		}
80  		
81  		if(!config.containsValid("video.framestyle")){
82  			frameStyle = new XuggleVideoFrameSelection.All(config);
83  		}
84  		else{
85  			String name;
86  			try {
87  				name = config.read("video.framestyle");
88  				frameStyle = XuggleVideoFrameSelection.byName(name,config);
89  			} catch (ParseException e) {
90  				throw new ImageCollectionSetupException("Failed to set framestyle");
91  			}
92  		}
93  	}
94  	
95  	@Override
96  	public int countImages(){
97  		return (int) this.video.countFrames();
98  	}
99  
100 	protected abstract XuggleVideo loadXuggleVideo(String object);
101 	protected abstract String videoTag();
102 
103 	@Override 
104 	public int useable(ImageCollectionConfig config){
105 		if(config.containsValid(videoTag()))
106 			return 0;
107 		else return -1;
108 	}
109 
110 
111 	@Override
112 	public List<ImageCollectionEntry<MBFImage>> getAll() {
113 		List<ImageCollectionEntry<MBFImage>> allFrames = new ArrayList<ImageCollectionEntry<MBFImage>>();
114 		for(ImageCollectionEntry<MBFImage> image : this){
115 			allFrames.add(image);
116 		}
117 		return allFrames;
118 	}
119 	
120 	public static class FromFile extends XuggleVideoImageCollection{
121 		@Override
122 		protected XuggleVideo loadXuggleVideo(String videoEntry) {
123 			File videoFile = new File(videoEntry);
124 			return new XuggleVideo(videoFile);
125 		}
126 
127 		@Override
128 		protected String videoTag() {
129 			return "video.file";
130 		}
131 
132 		@Override
133 		public int useable(String rawInput) {
134 			File f = new File(rawInput);
135 			if(f.exists()) return 0;
136 			else return -1;
137 		}
138 
139 		@Override
140 		public ImageCollectionConfig defaultConfig(String rawInput) {
141 			return new ImageCollectionConfig(
142 					String.format("{video: {file: %s}}",rawInput)
143 			);
144 		}
145 		
146 		
147 
148 		
149 	}
150 	
151 	public static class FromURL extends XuggleVideoImageCollection{
152 		@Override
153 		protected XuggleVideo loadXuggleVideo(String videoEntry) {
154 			return new XuggleVideo(videoEntry);
155 		}
156 
157 		@Override
158 		protected String videoTag() {
159 			return "video.url";
160 		}
161 		
162 		@Override
163 		public int useable(String rawInput) {
164 			try {
165 				new URL(rawInput);
166 				return 0;
167 			} catch (MalformedURLException e) {
168 				return -1;
169 			}
170 		}
171 
172 		@Override
173 		public ImageCollectionConfig defaultConfig(String rawInput) {
174 			return new ImageCollectionConfig(
175 				String.format("{video: {url: %s}}",rawInput)
176 			);
177 		}
178 	}
179 }