Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class VideoStream:
- """
- Interface to provide easy iteration and indexing access to imageio
- video objects:
- self[10] returns the 10^th frame of the video.
- self[10:20] returns frames 10-20 as a list
- """
- def __init__(self, file:str, color:bool=True):
- """
- file : str
- uri of video file
- color : bool
- Pass true for VideoStream to return color images. Pass false
- for VideoStream to return b+w images. Defaults to True.
- """
- self.video = imageio.get_reader(file, 'ffmpeg')
- self.color = color
- self.duration = self.video.get_meta_data()['duration']
- self.fps = self.video.get_meta_data()['fps']
- self.nframes = self.video.get_meta_data()['nframes']
- if self.color:
- self.shape = (*self.video.get_meta_data()['size'][::-1], 3)
- else:
- self.shape = (*self.video.get_meta_data()['size'][::-1], 1)
- self.index = 0
- def __len__(self):
- """
- Returns number of frames in video.
- """
- return self.nframes
- def __getitem__(self, index):
- """
- Returns single frame if index is int or a list of frames if index is a slice.
- """
- if isinstance(index, slice):
- indices = range(*index.indices(len(self)))
- return (self._get_frame(i) for i in indices)
- else:
- return self._get_frame(index)
- def __next__(self):
- if self.index >= len(self):
- raise StopIteration
- else:
- self.index += 1
- return self.__getitem__(self.index-1)
- def __iter__(self):
- return self
- def _get_frame(self, index:int):
- frame = self.video.get_data(index)
- if self.color:
- return frame
- else:
- return color.rgb2gray(frame)
- def set_color(self, color:bool):
- """
- color : bool
- Pass true for VideoStream to return color images. Pass false
- for VideoStream to return b+w images.
- """
- self.color = color
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement