def build(self): import imageio reader = imageio.get_reader(self.filepath, format='gif', mode='I') def imageio_to_menpo(imio_reader, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if pixels.shape[0] == 4: # If normalise is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if self.normalise: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(np.bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if self.normalise: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False) index_callable = partial(imageio_to_menpo, reader) ll = LazyList.init_from_index_callable(index_callable, reader.get_length()) return ll
def imageio_gif_importer(filepath, asset=None, normalize=True, **kwargs): r""" Imports GIF images using freeimagemulti plugin from the imageio library. Returns a :map:`LazyList` that gives lazy access to the GIF on a per-frame basis. Parameters ---------- filepath : `Path` Absolute filepath of the video. asset : `object`, optional An optional asset that may help with loading. This is unused for this implementation. normalize : `bool`, optional If ``True``, normalize between 0.0 and 1.0 and convert to float. If ``False`` just return whatever imageio imports. \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`LazyList` A :map:`LazyList` containing :map:`Image` or subclasses per frame of the GIF. """ import imageio reader = imageio.get_reader(str(filepath), format='gif', mode='I') def imageio_to_menpo(imio_reader, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if pixels.shape[0] == 4: # If normalize is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if normalize: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(np.bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if normalize: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False) index_callable = partial(imageio_to_menpo, reader) ll = LazyList.init_from_index_callable(index_callable, reader.get_length()) return ll
def imageio_gif_importer(filepath, asset=None, normalise=True, **kwargs): r""" Imports GIF images using freeimagemulti plugin from the imageio library. Returns a :map:`LazyList` that gives lazy access to the GIF on a per-frame basis. Parameters ---------- filepath : `Path` Absolute filepath of the video. asset : `object`, optional An optional asset that may help with loading. This is unused for this implementation. normalise : `bool`, optional If ``True``, normalise between 0.0 and 1.0 and convert to float. If ``False`` just return whatever imageio imports. \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`LazyList` A :map:`LazyList` containing :map:`Image` or subclasses per frame of the GIF. """ import imageio reader = imageio.get_reader(str(filepath), format='gif', mode='I') def imageio_to_menpo(imio_reader, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if pixels.shape[0] == 4: # If normalise is False, then we return the alpha as an extra # channel, which can be useful if the alpha channel has semantic # meanings! if normalise: p = normalize_pixels_range(pixels[:3]) return MaskedImage(p, mask=pixels[-1].astype(np.bool), copy=False) else: return Image(pixels, copy=False) # Assumed not to have an Alpha channel if normalise: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False) index_callable = partial(imageio_to_menpo, reader) ll = LazyList.init_from_index_callable(index_callable, reader.get_length()) return ll
def ffmpeg_importer(filepath, normalize=True, exact_frame_count=True, **kwargs): r""" Imports videos by streaming frames from a pipe using FFMPEG. Returns a :map:`LazyList` that gives lazy access to the video on a per-frame basis. There are two important environment variables that can be set to alter the behaviour of this function: ================== ====================================== ENV Variable Definition ================== ====================================== MENPO_FFMPEG_CMD The path to the 'ffmpeg' executable. MENPO_FFPROBE_CMD The path to the 'ffprobe' executable. ================== ====================================== Parameters ---------- filepath : `Path` Absolute filepath of the video. normalize : `bool`, optional If ``True``, normalize between 0.0 and 1.0 and convert to float. If ``False`` just return whatever ffmpeg imports. exact_frame_count: `bool`, optional If ``True``, the import fails if ffprobe is not available (reading from ffmpeg's output returns inexact frame count) \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`LazyList` A :map:`LazyList` containing :map:`Image` or subclasses per frame of the video. """ reader = FFMpegVideoReader(filepath, normalize=normalize, exact_frame_count=exact_frame_count) ll = LazyList.init_from_index_callable( lambda x: Image.init_from_channels_at_back(reader[x]), len(reader)) ll.fps = reader.fps return ll
def build(self): import imageio reader = imageio.get_reader(self.filepath, format='ffmpeg', mode='I') def imageio_to_menpo(imio_reader, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if self.normalise: return Image(normalise_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False) index_callable = partial(imageio_to_menpo, reader) ll = LazyList.init_from_index_callable(index_callable, reader.get_length()) ll.fps = reader.get_meta_data()['fps'] return ll
def ffmpeg_importer(filepath, normalize=True, exact_frame_count=True, **kwargs): r""" Imports videos by streaming frames from a pipe using FFMPEG. Returns a :map:`LazyList` that gives lazy access to the video on a per-frame basis. There are two important environment variables that can be set to alter the behaviour of this function: ================== ====================================== ENV Variable Definition ================== ====================================== MENPO_FFMPEG_CMD The path to the 'ffmpeg' executable. MENPO_FFPROBE_CMD The path to the 'ffprobe' executable. ================== ====================================== Parameters ---------- filepath : `Path` Absolute filepath of the video. normalize : `bool`, optional If ``True``, normalize between 0.0 and 1.0 and convert to float. If ``False`` just return whatever ffmpeg imports. exact_frame_count: `bool`, optional If ``True``, the import fails if ffprobe is not available (reading from ffmpeg's output returns inexact frame count) \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`LazyList` A :map:`LazyList` containing :map:`Image` or subclasses per frame of the video. """ reader = FFMpegVideoReader(filepath, normalize=normalize, exact_frame_count=exact_frame_count) ll = LazyList.init_from_index_callable(lambda x: Image.init_from_channels_at_back(reader[x]), len(reader)) ll.fps = reader.fps return ll
def test_lazylist_init_from_index_callable(): identity_func = lambda x: x ll = LazyList.init_from_index_callable(identity_func, 5) assert ll[0] == 0 assert ll[-1] == 4
def imageio_ffmpeg_importer(filepath, asset=None, normalise=True, **kwargs): r""" Imports videos using the FFMPEG plugin from the imageio library. Returns a :map:`LazyList` that gives lazy access to the video on a per-frame basis. Parameters ---------- filepath : `Path` Absolute filepath of the video. asset : `object`, optional An optional asset that may help with loading. This is unused for this implementation. normalise : `bool`, optional If ``True``, normalise between 0.0 and 1.0 and convert to float. If ``False`` just return whatever imageio imports. \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`LazyList` A :map:`LazyList` containing :map:`Image` or subclasses per frame of the video. """ import imageio reader = imageio.get_reader(str(filepath), format='ffmpeg', mode='I') index_callable = partial(_imageio_to_menpo, reader, normalise) ll = LazyList.init_from_index_callable(index_callable, reader.get_length()) ll.fps = reader.get_meta_data()['fps'] if len(ll) != 0: # TODO: Remove when imageio fixes the ffmpeg importer duration/start # This is a bit grim but the frame->timestamp logic in imageio at # the moment is not very accurate and so we really need to ensure # that the user is returned a list they can actually index into. So # we just remove all the frames that we can't actually index into. # Remove from the front for start in range(len(ll)): if start > 10: # Arbitrary but probably safe warnings.warn('Highly inaccurate frame->timestamp mapping ' 'returned by imageio - many frames are being ' 'dropped and thus importing may be very slow.' ' Please see the documentation.') try: ll[start] break except: pass else: # If we never broke out then ALL frames raised exceptions ll = LazyList([]) # Only take the frames after the initial broken ones ll = ll[start:] if len(ll) != 0: n_frames = len(ll) - 1 for end in range(n_frames, -1, -1): if end < n_frames - 10: # Arbitrary but probably safe warnings.warn('Highly inaccurate frame->timestamp mapping ' 'returned by imageio - many frames are being ' 'dropped and thus importing may be very slow.' ' Please see the documentation.') try: ll[end] break except: pass # Only take the frames before the broken ones ll = ll[:end+1] return ll
def imageio_ffmpeg_importer(filepath, asset=None, normalise=True, **kwargs): r""" Imports videos using the FFMPEG plugin from the imageio library. Returns a :map:`LazyList` that gives lazy access to the video on a per-frame basis. Parameters ---------- filepath : `Path` Absolute filepath of the video. asset : `object`, optional An optional asset that may help with loading. This is unused for this implementation. normalise : `bool`, optional If ``True``, normalise between 0.0 and 1.0 and convert to float. If ``False`` just return whatever imageio imports. \**kwargs : `dict`, optional Any other keyword arguments. Returns ------- image : :map:`LazyList` A :map:`LazyList` containing :map:`Image` or subclasses per frame of the video. """ import imageio reader = imageio.get_reader(str(filepath), format='ffmpeg', mode='I') index_callable = partial(_imageio_to_menpo, reader, normalise) ll = LazyList.init_from_index_callable(index_callable, reader.get_length()) ll.fps = reader.get_meta_data()['fps'] if len(ll) != 0: # TODO: Remove when imageio fixes the ffmpeg importer duration/start # This is a bit grim but the frame->timestamp logic in imageio at # the moment is not very accurate and so we really need to ensure # that the user is returned a list they can actually index into. So # we just remove all the frames that we can't actually index into. # Remove from the front for start in range(len(ll)): if start > 10: # Arbitrary but probably safe warnings.warn('Highly inaccurate frame->timestamp mapping ' 'returned by imageio - many frames are being ' 'dropped and thus importing may be very slow.' ' Please see the documentation.') try: ll[start] break except: pass else: # If we never broke out then ALL frames raised exceptions ll = LazyList([]) # Only take the frames after the initial broken ones ll = ll[start:] if len(ll) != 0: n_frames = len(ll) - 1 for end in range(n_frames, -1, -1): if end < n_frames - 10: # Arbitrary but probably safe warnings.warn('Highly inaccurate frame->timestamp mapping ' 'returned by imageio - many frames are being ' 'dropped and thus importing may be very slow.' ' Please see the documentation.') try: ll[end] break except: pass # Only take the frames before the broken ones ll = ll[:end + 1] return ll
def build(self): import imageio reader = imageio.get_reader(self.filepath, format='ffmpeg', mode='I') def imageio_to_menpo(imio_reader, index): pixels = imio_reader.get_data(index) pixels = channels_to_front(pixels) if self.normalise: return Image(normalize_pixels_range(pixels), copy=False) else: return Image(pixels, copy=False) index_callable = partial(imageio_to_menpo, reader) ll = LazyList.init_from_index_callable(index_callable, reader.get_length()) ll.fps = reader.get_meta_data()['fps'] if len(ll) != 0: # TODO: Remove when imageio fixes the ffmpeg importer duration/start # This is a bit grim but the frame->timestamp logic in imageio at # the moment is not very accurate and so we really need to ensure # that the user is returned a list they can actually index into. So # we just remove all the frames that we can't actually index into. # Remove from the front for start in range(len(ll)): if start > 10: # Arbitrary but probably safe warnings.warn( 'Highly inaccurate frame->timestamp mapping ' 'returned by imageio - many frames are being ' 'dropped and thus importing may be very slow.' ' Please see the documentation.') try: ll[start] break except: pass else: # If we never broke out then ALL frames raised exceptions ll = LazyList([]) # Only take the frames after the initial broken ones ll = ll[start:] if len(ll) != 0: n_frames = len(ll) - 1 for end in range(n_frames, -1, -1): if end < n_frames - 10: # Arbitrary but probably safe warnings.warn( 'Highly inaccurate frame->timestamp mapping ' 'returned by imageio - many frames are being ' 'dropped and thus importing may be very slow.' ' Please see the documentation.') try: ll[end] break except: pass # Only take the frames before the broken ones ll = ll[:end + 1] return ll