class Blurring(): """docstring for Blurring.""" def __init__(self, **kwargs): self.logger = getLogger(self.__class__.__name__) self.blur = BlurImage(**kwargs) self.work = WorkFolder(**kwargs) self.debugdirs = [] if kwargs.get('setup', True): self.work.setup() def add_template(self, file=None, folder=None, data=None): """ Add a templat to the work folder. file: Add a image file. folder: All images inside the folder. data: A file with data to create different templates. """ if file: self.logger.debug('Add template file "%s"', file) copyfile( file, os.path.join(self.work['templates'], os.path.basename(file))) elif folder: self.logger.debug('Add template folder "%s"', folder) for name in os.listdir(folder): filename = os.path.join(folder, name) if os.path.isfile(filename): copy(filename, os.path.join(self.work['templates'], name)) elif data: self.logger.debug('Add template data file "%s"', data) TempGen(folder=self.work['templates'], data=data).run() def add_debug(self, name): """Add a debug folder. Only for debugging""" foldername = os.path.abspath(name) self.logger.debug('Add debug folder "%s"', foldername) self.debugdirs.append(foldername) if not os.path.exists(foldername): os.makedirs(foldername) self.logger.debug('Create debug folder "%s"', foldername) def run(self, src, dest): """ Start the blurring process. src: The input video file. dist: The blurred output video file. """ self.logger.debug('Start the blurring. src="%s", dest="%s"', src, dest) create_frames(src, self.work['frames']) for frame in self.work.files('frames'): basename = os.path.basename(frame) areas = self.blur.check_image(frame, self.work.files('templates')) if areas: self.blur.blur_image( frame, areas, os.path.join(self.work['cleaned'], basename)) else: copyfile(frame, os.path.join(self.work['cleaned'], basename)) save_frames(self.work['cleaned'], dest)
class Blurring(): """docstring for Blurring.""" def __init__(self, **kwargs): self.logger = getLogger(self.__class__.__name__) self.offset = kwargs.get('offset', 30) self.blur = BlurImage(**kwargs) self.work = WorkFolder(**kwargs) self.debugdirs = [] if kwargs.get('setup', True): self.work.setup() def add_template(self, file=None, folder=None, data=None): """ Add a templat to the work folder. file: Add a image file. folder: All images inside the folder. data: A file with data to create different templates. """ if file: self.logger.debug('Add template file "%s"', file) copyfile(file, os.path.join(self.work['templates'], os.path.basename(file))) elif folder: self.logger.debug('Add template folder "%s"', folder) for name in os.listdir(folder): filename = os.path.join(folder, name) if os.path.isfile(filename): copy(filename, os.path.join(self.work['templates'], name)) elif data: self.logger.debug('Add template data file "%s"', data) TempGen(folder=self.work['templates'], data=data).run() def add_debug(self, name): """Add a debug folder. Only for debugging""" foldername = os.path.abspath(name) self.logger.debug('Add debug folder "%s"', foldername) self.debugdirs.append(foldername) if not os.path.exists(foldername): os.makedirs(foldername) self.logger.debug('Create debug folder "%s"', foldername) def analyze(self): """Analyze the frames and return a list with data""" result = [] for frame_no, frame in enumerate(self.work.files('frames')): areas = self.blur.check_image(frame, self.work.files('templates')) for area in areas: index = find_area(result, area) if index == -1: result.append({'area': area, 'frames': [frame_no]}) else: result[index]['frames'].append(frame_no) for values in result: sectors = [[values['frames'][0], values['frames'][0]]] for index in range(1, len(values['frames'])): if (values['frames'][index] - sectors[-1][1]) == 1: sectors[-1][1] = values['frames'][index] else: sectors.append([values['frames'][index], values['frames'][index]]) values['sectors'] = sectors return result def run(self, src, dest): """ Start the blurring process. src: The input video file. dist: The blurred output video file. """ self.logger.debug('Start the blurring. src="%s", dest="%s"', src, dest) create_frames(src, self.work['frames']) data = self.analyze() for frame_no, frame in enumerate(self.work.files('frames')): basename = os.path.basename(frame) areas = [] for values in data: for sector in values['sectors']: if frame_no in range(sector[0]-self.offset, sector[1]+self.offset): areas.append(values['area']) break if areas: self.blur.blur_image(frame, areas, os.path.join(self.work['cleaned'], basename)) else: copyfile(frame, os.path.join(self.work['cleaned'], basename)) save_frames(self.work['cleaned'], dest)
def test_files(self): folder = WorkFolder() folder.setup() self.assertIsInstance(folder.files('frames'), list)