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.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 }