@@ -138,23 +138,24 @@ class ImageCollection(object):
138
138
ImageCollection can be modified to load images from an arbitrary
139
139
source by specifying a combination of `load_pattern` and
140
140
`load_func`. For an ImageCollection ``ic``, ``ic[5]`` uses
141
- ``load_func(file_pattern [5])`` to load the image.
141
+ ``load_func(load_pattern [5])`` to load the image.
142
142
143
- Imagine, for example, an ImageCollection that loads every tenth
143
+ Imagine, for example, an ImageCollection that loads every third
144
144
frame from a video file::
145
145
146
- class AVILoader:
147
- video_file = 'myvideo.avi'
146
+ video_file = 'no_time_for_that_tiny.gif'
148
147
149
- def __call__(self, frame):
150
- return video_read(self.video_file, frame)
148
+ def vidread_step(f, step):
149
+ vid = imageio.get_reader(f)
150
+ seq = [v for v in vid.iter_data()]
151
+ return seq[::step]
151
152
152
- avi_load = AVILoader( )
153
+ ic = ImageCollection(video_file, load_func=vidread_step, step=3 )
153
154
154
- frames = range(0, 1000, 10) # 0, 10, 20, ...
155
- ic = ImageCollection(frames, load_func=avi_load)
155
+ ic # is an ImageCollection object of length 1 because there is 1 file
156
156
157
- x = ic[5] # calls avi_load(frames[5]) or equivalently avi_load(50)
157
+ x = ic[0] # calls vidread_step(video_file, step=3)
158
+ x[5] # is the sixth element of a list of length 8 (24 / 3)
158
159
159
160
Another use of ``load_func`` would be to convert all images to ``uint8``::
160
161
@@ -191,7 +192,15 @@ def __init__(self, load_pattern, conserve_memory=True, load_func=None,
191
192
raise TypeError ('Invalid pattern as input.' )
192
193
193
194
self ._files = sorted (self ._files , key = alphanumeric_key )
194
- self ._numframes = self ._find_images ()
195
+
196
+ if load_func is None :
197
+ from ._io import imread
198
+ self .load_func = imread
199
+ self ._numframes = self ._find_images ()
200
+ else :
201
+ self .load_func = load_func
202
+ self ._numframes = len (self ._files )
203
+ self ._frame_index = None
195
204
196
205
if conserve_memory :
197
206
memory_slots = 1
@@ -201,12 +210,6 @@ def __init__(self, load_pattern, conserve_memory=True, load_func=None,
201
210
self ._conserve_memory = conserve_memory
202
211
self ._cached = None
203
212
204
- if load_func is None :
205
- from ._io import imread
206
- self .load_func = imread
207
- else :
208
- self .load_func = load_func
209
-
210
213
self .load_func_kwargs = load_func_kwargs
211
214
self .data = np .empty (memory_slots , dtype = object )
212
215
0 commit comments