def determine_average_frame_brightness(video_file_or_pattern, output_hdf5_file=None): """ iterates a video and determines its intensity, which will be stored in a hdf5 file, if the respective file name is given""" # read video data with closing(load_any_video(video_file_or_pattern)) as video: brightness = np.empty(video.frame_count, np.double) for k, frame in enumerate(display_progress(video)): brightness[k] = frame.mean() # restrict the result to the number of actual frames read if k < video.frame_count: brightness = brightness[:k + 1] # write brightness data if output_hdf5_file: with h5py.File(output_hdf5_file, "w") as fd: fd.create_dataset("brightness", data=brightness) return brightness
def load_video(self, video=None, crop_video=True, cropping_rect=None, frames_skipped_in_this_pass=0, frames=None): """ loads the video and applies a monochrome and cropping filter """ video_parameters = self.data['parameters/video/video_parameters'] # initialize the video if video is None: video_filename_pattern = os.path.join( self.data['parameters/base_folder'], self.data['parameters/video/filename_pattern'] ) self.video = load_any_video(video_filename_pattern, parameters=video_parameters) elif isinstance(video, str): self.video = load_any_video(video, parameters=video_parameters) video_filename_pattern = None else: self.video = video video_filename_pattern = None # save some data about the video video_info = {'frame_count': self.video.frame_count, 'size': '%d x %d' % tuple(self.video.size), 'fps': self.video.fps} try: video_info['filecount'] = self.video.filecount except AttributeError: video_info['filecount'] = 1 self.data.create_child('video', video_info) self.data['video/filename_pattern'] = video_filename_pattern # restrict the analysis to an interval of frames if frames is None: frames = self.data.get('parameters/video/frames', None) # check whether frames are given if frames is None: frames_start, frames_end = None, None else: frames_start, frames_end = frames # supply defaults for missing values if frames_start is None: frames_start = self.data.get('parameters/video/frames_skip', 0) if frames_end is None: frames_end = self.video.frame_count # skip additional frames if requested if frames_skipped_in_this_pass > 0: frames_start += frames_skipped_in_this_pass # count last frame from the end when it is negative if frames_end < 0: frames_end += self.video.frame_count # apply the filter on the video if not(frames_start == 0 and frames_end == self.video.frame_count): self.video = self.video[frames_start:frames_end] video_info['frames'] = (frames_start, frames_end) if cropping_rect is None: cropping_rect = self.data.get('parameters/video/cropping_rect', None) # restrict video to green channel if it is a color video color_channel = 'green' if self.video.is_color else None video_info['color_channel'] = color_channel # rotate the video if requested if self.data['parameters/video/rotation'] != 0: angle = 90 * self.data['parameters/video/rotation'] self.video = FilterRotate(self.video, angle=angle) # crop the video if requested if crop_video and cropping_rect is not None: if isinstance(cropping_rect, str): # crop according to the supplied string self.video = FilterCrop(self.video, region=cropping_rect, color_channel=color_channel) else: # crop to the given rect self.video = FilterCrop(self.video, rect=cropping_rect, color_channel=color_channel) video_info['cropping_rect'] = cropping_rect else: # user_crop is not None => use the full video if color_channel is None: self.video = self.video else: self.video = FilterMonochrome(self.video, color_channel) video_info['cropping_rect'] = None return video_info
def load_video(self, video=None, crop_video=True, cropping_rect=None, frames_skipped_in_this_pass=0): """ loads the video and applies a monochrome and cropping filter """ # initialize the video if video is None: video_filename_pattern = os.path.join(self.data['parameters/base_folder'], self.data['parameters/video/filename_pattern']) self.video = load_any_video(video_filename_pattern) else: self.video = video video_filename_pattern = None # save some data about the video video_info = {'frame_count': self.video.frame_count, 'size': '%d x %d' % tuple(self.video.size), 'fps': self.video.fps} try: video_info['filecount'] = self.video.filecount except AttributeError: video_info['filecount'] = 1 self.data.create_child('video', video_info) self.data['video/filename_pattern'] = video_filename_pattern # restrict the analysis to an interval of frames frames = self.data.get('parameters/video/frames', None) if frames is None: frames_start = self.data.get('parameters/video/frames_skip', 0) frames_end = self.video.frame_count else: frames_start, frames_end = frames if frames_skipped_in_this_pass > 0: frames_start += frames_skipped_in_this_pass if frames_end < 0: frames_end += self.video.frame_count if frames_start != 0 or frames_end != self.video.frame_count: self.video = self.video[frames_start:frames_end] video_info['frames'] = (frames_start, frames_end) if cropping_rect is None: cropping_rect = self.data.get('parameters/video/cropping_rect', None) # restrict video to green channel if it is a color video color_channel = 'green' if self.video.is_color else None video_info['color_channel'] = color_channel if crop_video and cropping_rect is not None: if isinstance(cropping_rect, str): # crop according to the supplied string self.video = FilterCrop(self.video, region=cropping_rect, color_channel=color_channel) else: # crop to the given rect self.video = FilterCrop(self.video, rect=cropping_rect, color_channel=color_channel) video_info['cropping_rect'] = cropping_rect else: # user_crop is not None => use the full video if color_channel is None: self.video = self.video else: self.video = FilterMonochrome(self.video, color_channel) video_info['cropping_rect'] = None return video_info
def load_video(self, video=None, crop_video=True, cropping_rect=None, frames_skipped_in_this_pass=0): """ loads the video and applies a monochrome and cropping filter """ # initialize the video if video is None: video_filename_pattern = os.path.join( self.data['parameters/base_folder'], self.data['parameters/video/filename_pattern']) self.video = load_any_video(video_filename_pattern) else: self.video = video video_filename_pattern = None # save some data about the video video_info = { 'frame_count': self.video.frame_count, 'size': '%d x %d' % tuple(self.video.size), 'fps': self.video.fps } try: video_info['filecount'] = self.video.filecount except AttributeError: video_info['filecount'] = 1 self.data.create_child('video', video_info) self.data['video/filename_pattern'] = video_filename_pattern # restrict the analysis to an interval of frames frames = self.data.get('parameters/video/frames', None) if frames is None: frames_start = self.data.get('parameters/video/frames_skip', 0) frames_end = self.video.frame_count else: frames_start, frames_end = frames if frames_skipped_in_this_pass > 0: frames_start += frames_skipped_in_this_pass if frames_end < 0: frames_end += self.video.frame_count if frames_start != 0 or frames_end != self.video.frame_count: self.video = self.video[frames_start:frames_end] video_info['frames'] = (frames_start, frames_end) if cropping_rect is None: cropping_rect = self.data.get('parameters/video/cropping_rect', None) # restrict video to green channel if it is a color video color_channel = 'green' if self.video.is_color else None video_info['color_channel'] = color_channel if crop_video and cropping_rect is not None: if isinstance(cropping_rect, str): # crop according to the supplied string self.video = FilterCrop(self.video, region=cropping_rect, color_channel=color_channel) else: # crop to the given rect self.video = FilterCrop(self.video, rect=cropping_rect, color_channel=color_channel) video_info['cropping_rect'] = cropping_rect else: # user_crop is not None => use the full video if color_channel is None: self.video = self.video else: self.video = FilterMonochrome(self.video, color_channel) video_info['cropping_rect'] = None return video_info