示例#1
0
 def create_folder(self, **kwargs):
     tmpdir = self.mkdtemp()
     root = os.path.join(tmpdir, 'test')
     kwargs['root'] = root
     folder = WorkFolder(**kwargs)
     folder.setup()
     return folder
示例#2
0
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)
示例#3
0
    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()
示例#4
0
    def test_full_setup(self):
        folder = WorkFolder()
        self.assertTrue(os.path.exists(folder['root']))
        for dir in folder.FOLDERS:
            self.assertFalse(os.path.exists(folder[dir]))

        folder.setup()
        for dir in folder.FOLDERS:
            self.assertTrue(os.path.exists(folder[dir]))

        self.assertFalse(folder['test'])
示例#5
0
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)
示例#6
0
 def test_exception(self):
     folder = WorkFolder()
     self.assertRaises(TypeError, folder.setup, {'test': False})
     self.assertRaises(TypeError, folder.setup, object())
     self.assertRaises(TypeError, folder.setup, type(2+3j))
示例#7
0
    def test_small_setup(self):
        tmpdir = self.mkdtemp()
        root = os.path.join(tmpdir, 'test')
        folder = WorkFolder(root=root)
        self.assertFalse(folder.check(False))

        folder.setup(False)
        self.assertTrue(folder.check(False))
        for dir in folder.FOLDERS:
            self.assertFalse(folder.check(dir))

        folder.setup(['templates', 'cleaned'])
        self.assertFalse(os.path.exists(folder['frames']))
        self.assertTrue(os.path.exists(folder['templates']))
        self.assertTrue(os.path.exists(folder['cleaned']))

        folder.setup('frames')
        self.assertTrue(os.path.exists(folder['frames']))
        self.assertTrue(os.path.exists(folder['templates']))
        self.assertTrue(os.path.exists(folder['cleaned']))
示例#8
0
 def test_files(self):
     folder = WorkFolder()
     folder.setup()
     self.assertIsInstance(folder.files('frames'), list)