예제 #1
0
    def insert_youtube_url(self, url, desc=None):
        """
        This method inserts a YouTube video into the video collection.

        :param str url: The URL of the YouTube video.
        :param str desc: An optional description of the video.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        try:
            context = self.youtube_context(url)
        except pytube.exceptions.VideoUnavailable:
            raise CommandLineRouteHandlerError(
                f'A VideoUnavailable exception was raised and caught for '
                f'{url}')

        streams = self.youtube_streams(context)
        stream = self.youtube_select(streams)

        try:
            path = self.youtube_download(context, stream[0], VideoDir.path(),
                                         self.uuid())
        except urllib.error.HTTPError as e:  # noqa
            raise CommandLineRouteHandlerError(
                f'An HTTPError exception was raised and caught for {url} '
                f'with HTTP status code {e.code}')

        filename = os.path.basename(path)

        video_id = VideoModel().insert(url, filename, desc)

        debug(
            f'video_id={video_id}, uri={url}, filename={filename}, '
            f'description={desc}', 3)
예제 #2
0
    def select_curate_auto(self, transform_set_ids, name, opts):
        """
        This method automatically curates a transform set.

        :param list(int) transform_set_ids: The transform set IDs.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        for transform_set_id in transform_set_ids:
            result = TransformSetModel().select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        plugin = Plugin.get('transform_set_select_curate', name)
        if plugin is None:
            raise CommandLineRouteHandlerError(
                f"'{name}' is not a valid transform set curation "
                f'plugin name')

        for transform_set_id in transform_set_ids:
            plugin = Plugin.get('transform_set_select_curate', name)

            try:
                plugin().transform_set_select_curate(transform_set_id, opts)
            except ValueError as e:
                raise CommandLineRouteHandlerError(str(e))

        debug('success', 3)
예제 #3
0
    def select_deploy(self, video_id, name, opts):
        """
        This method deploys a video via a plugin.

        :param int video_id: The video ID.
        :param str name: The plugin name.
        :param opts dict: The dict of options.
        :rtype: None
        """

        video_model = VideoModel()

        result = video_model.select(video_id)
        if result is None:
            raise CommandLineRouteHandlerError(
                f'Video with ID {video_id:08d} not found')

        plugin = Plugin.get('video_select_deploy', name)
        if plugin is None:
            raise CommandLineRouteHandlerError(
                f"'{name}' is not a valid frame set extraction plugin name")

        plugin().video_select_deploy(video_id, opts)

        debug('success', 3)
예제 #4
0
    def select_extract(self, video_ids, opts={}):
        """
        This method extracts frames and thumbnails from a video to a frame set.

        :param list(int) video_ids: The video IDs.
        :param dict opts: The optional dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        video_model = VideoModel()

        for video_id in video_ids:
            result = video_model.select(video_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Video with ID {video_id:08d} not found')

        plugin = Plugin.get('video_select_extract', 'default')

        for video_id in video_ids:
            frame_set_id = plugin().video_select_extract(
                video_id,
                sub_sample=int(opts.get('sub-sample', 1)),
                max_sample=int(opts.get('max-sample', 0)))

            debug(f'frame_set_id={frame_set_id}, video_id={video_id}', 3)
예제 #5
0
    def delete(self, video_ids):
        """
        This method deletes a video from the video collection.

        :param list(ints) video_ids: The video IDs.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        video_model = VideoModel()

        for video_id in video_ids:
            result = video_model.select(video_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Video with ID {video_id:08d} not found')

        for video_id in video_ids:
            result = video_model.select(video_id)

            path = VideoFile.path(result[2])

            video_model.delete(video_id)

            os.remove(path)

            debug(f'Video {video_id} was successfully deleted', 3)
예제 #6
0
    def select_clone(self, transform_set_ids):
        """
        This method clones transform sets to new transform sets.

        :param list(int) transform_set_ids: The transform set IDs.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        transform_set_model = TransformSetModel()

        for transform_set_id in transform_set_ids:
            result = transform_set_model.select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        for transform_set_id in transform_set_ids:
            result = transform_set_model.select(transform_set_id)

            self.select_merge([transform_set_id],
                              name=result[1],
                              fk_frame_sets=result[2],
                              fk_prev_transform_sets=transform_set_id,
                              rejected=True)
예제 #7
0
    def insert_vimeo_url(self, url, quality='hq', desc=None):
        """
        This method inserts a Vimeo video into the video collection.

        :param str url: The URL of the Vimeo video.
        :param str desc: An optional description of the video.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        try:
            video = self.vimeo_context(url)
        except KeyError:
            raise CommandLineRouteHandlerError(
                f'A KeyError exception was raised and caught for '
                f'{url}')

        if desc is None:
            desc = self.vimeo_title(video)

        stream = self.vimeo_stream(video, quality)

        filename = f'{self.uuid()}.mp4'
        self.vimeo_stream_download(stream, VideoDir.path(), filename)

        video_id = VideoModel().insert(url, filename, desc)

        debug(
            f'video_id={video_id}, uri={url}, filename={filename}, '
            f'description={desc}', 3)
    def test_message(self):
        e = None

        try:
            raise CommandLineRouteHandlerError('test')
        except CommandLineRouteHandlerError as e_:
            e = e_

        self.assertEqual(e.message, 'test')
    def select_export_dir(self, frame_set_ids, target_dir, opts={}):
        """
        This method exports frame sets to a directory.

        :param list frame_set_ids: The list of frame set IDs to export.
        :param str target_dir: The directory to which to export the frame sets.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        frame_set_model = FrameSetModel()

        for frame_set_id in frame_set_ids:
            result = frame_set_model.select(frame_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Frame set with ID {frame_set_id:08d} not found')

        frame_model = FrameModel()
        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))
        count = 0

        for fid in frame_set_ids:
            offset = 0
            frame_set_path = FrameSetSubDir.path(fid)

            while True:
                results = frame_model.list(fid,
                                           length=length,
                                           offset=offset,
                                           rejected=False)

                if not results:
                    break

                for frame_id, _, _ in results:
                    file_path = FrameFile().path(frame_set_path, frame_id,
                                                 'jpg')
                    if 'format' in opts:
                        filename = opts['format'] % frame_id
                    else:
                        filename = os.path.basename(file_path)

                    target_path = os.path.join(target_dir, filename)
                    shutil.copy(file_path, target_path)
                    count += 1

                    debug(
                        f'Frame with ID {frame_id:08d} at {file_path} '
                        f'exported to {target_path}', 4)

                offset += length

        debug(f'{count} frames were successfully exported to {target_dir}', 3)
    def list(self, transform_set_id):
        """
        This method lists transforms in the transform collection for a
        transform set.

        :param int transform_set_id: The transform set ID.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        result = TransformSetModel().select(transform_set_id)
        if result is None:
            raise CommandLineRouteHandlerError(
                f'Transform set with ID {transform_set_id:08d} not found')

        transform_model = TransformModel()

        count = transform_model.count(transform_set_id)

        debug(f'{count} results', 3)
        debug(
            'id | fk_transform_sets | fk_frames | metadata | rejected | '
            '(width | height)', 3)
        debug(
            '-----------------------------------------------------------'
            '----------------', 3)

        if count == 0:
            return

        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))
        offset = 0
        p1 = TransformSetSubDir.path(transform_set_id)

        while True:
            transforms = transform_model.list(transform_set_id,
                                              length=length,
                                              offset=offset)

            if not transforms:
                break

            for transform in transforms:
                p2 = TransformFile.path(p1, transform[0], 'jpg')

                height, width, _ = cv2.imread(p2).shape

                debug(
                    f'{transform[0]} | {transform[1]} | {transform[2]} | '
                    f'{transform[3]} | {transform[4]} | ({width} | '
                    f'{height})', 3)

            offset += length
예제 #11
0
    def select_extract(self, transform_set_ids, name, opts):
        """
        This method runs a transformation plugin over previously created
        transform sets.

        :param list(int) transform_set_id: The transform set IDs.
        :param str name: The name of the transform set extraction plugin to
            use.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        transform_set_model = TransformSetModel()

        for transform_set_id in transform_set_ids:
            result = transform_set_model.select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        plugin = Plugin.get('transform_set_select_extract', name)
        if plugin is None:
            raise CommandLineRouteHandlerError(
                f"'{name}' is not a valid transform set extraction "
                f'plugin name')

        for transform_set_id in transform_set_ids:
            try:
                new_transform_set_id = plugin() \
                    .transform_set_select_extract(transform_set_id, opts)
            except ValueError as e:
                raise CommandLineRouteHandlerError(str(e))

            result = transform_set_model.select(new_transform_set_id)

            debug(
                f'transform_set_id={result[0]}, name={result[1]}, '
                f'fk_frame_sets={result[2]}, '
                f'fk_prev_transform_sets={result[3]}', 3)
    def insert_images(self, images_path, opts):
        """
        This method inserts a frame set from a directory of images.

        :param str images_path: The path to the directory containing the images
            to insert.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        if not os.path.isdir(images_path):
            raise CommandLineRouteHandlerError(
                f'The path at {images_path} is not a directory')

        frame_set_id = FrameSetModel().insert(None)

        p1 = FrameSetSubDir.path(frame_set_id)

        os.makedirs(p1)

        frame_model = FrameModel()

        for image_path in glob.glob(os.path.join(images_path, '*')):
            ext = os.path.splitext(image_path)[1].lower()

            if ext not in ['.jpg', '.png']:
                debug(
                    f"Skipped image at {image_path} because it does not "
                    f"have a '.jpg' or '.png' file extension", 4)
                continue

            image = cv2.imread(image_path)

            frame_id = frame_model.insert(frame_set_id, 0)

            p2 = FrameFile.path(p1, frame_id, 'jpg')

            cv2.imwrite(p2, image, [cv2.IMWRITE_JPEG_QUALITY, 100])

            thumbnail = imutils.resize(image, width=192, height=192)

            p3 = FrameFile.path(p1, frame_id, 'jpg', '192x192')

            cv2.imwrite(p3, thumbnail, [cv2.IMWRITE_JPEG_QUALITY, 100])

            debug(
                f'Image at {image_path} inserted with ID {frame_id:08d} at '
                f'{p2} and {p3}', 4)

        debug(f'frame_set_id={frame_set_id}, fk_videos=None', 3)
예제 #13
0
    def select_detect(self, video_id, model_name, opts):
        """
        This method runs a deepfake detection model over a video.

        :param int video_id: The video ID.
        :param str model_name: The name of the detection model to run.
        :param opts dict: The dict of options.
        """

        video_model = VideoModel()

        result = video_model.select(video_id)
        if result is None:
            raise CommandLineRouteHandlerError(
                f'Video with ID {video_id:08d} not found')

        plugin = Plugin.get('video_select_detect', model_name)
        if plugin is None:
            raise CommandLineRouteHandlerError(
                f"'{model_name}' is not a valid video select detect plugin name")

        video_path = VideoFile.path(result[2])
        plugin().video_select_detect(video_path, **opts)
    def select_extract(self, frame_set_ids, name, opts):
        """
        This method extracts portions of frames to transform sets.

        :param list(int) frame_set_ids: The frame set IDs.
        :param str name: The name of the frame set extraction plugin to use.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        frame_set_model = FrameSetModel()

        for frame_set_id in frame_set_ids:
            result = frame_set_model.select(frame_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Frame set with ID {frame_set_id:08d} not found')

        plugin = Plugin.get('frame_set_select_extract', name)
        if plugin is None:
            raise CommandLineRouteHandlerError(
                f"'{name}' is not a valid frame set extraction plugin name")

        transform_set_model = TransformSetModel()

        for frame_set_id in frame_set_ids:
            transform_set_id = plugin().frame_set_select_extract(
                frame_set_id, opts)

            result = transform_set_model.select(transform_set_id)

            debug(
                f'transform_set_id={result[0]}, name={result[1]}, '
                f'fk_frame_sets={result[2]}, '
                f'fk_prev_transform_sets={result[3]}', 3)
예제 #15
0
    def select_merge_non_default(self, transform_set_ids, name, opts):
        """
        This method merges multiple transform sets into one transform set.

        :param list(int) transform_set_ids: The transform set IDs.
        :param str name: The name of the transform set merge plugin
            to  use.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        transform_set_model = TransformSetModel()

        for transform_set_id in transform_set_ids:
            result = transform_set_model.select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        plugin = Plugin.get('transform_set_select_merge', name)
        if plugin is None:
            raise CommandLineRouteHandlerError(
                f"'{name}' is not a valid transform set merge plugin name")

        try:
            new_transform_set_id = plugin().transform_set_select_merge(
                transform_set_ids, opts)
        except ValueError as e:
            raise CommandLineRouteHandlerError(str(e))

        result = transform_set_model.select(new_transform_set_id)

        debug(
            f'transform_set_id={result[0]}, name={result[1]}, '
            f'fk_frame_sets={result[2]}, '
            f'fk_prev_transform_sets={result[3]}', 3)
    def list(self, frame_set_id):
        """
        This method lists frames in the frame collection for a frame set.

        :param int frame_set_id: The frame set ID.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        result = FrameSetModel().select(frame_set_id)
        if result is None:
            raise CommandLineRouteHandlerError(
                f'Frame set with ID {frame_set_id:08d} not found')

        frame_model = FrameModel()

        count = frame_model.count(frame_set_id)

        debug(f'{count} results', 3)
        debug('id | fk_frame_sets | rejected | (width | height)', 3)
        debug('------------------------------------------------', 3)

        if count == 0:
            return

        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))
        offset = 0
        p1 = FrameSetSubDir.path(frame_set_id)

        while True:
            frames = frame_model.list(frame_set_id,
                                      length=length,
                                      offset=offset)

            if not frames:
                break

            for frame in frames:
                p2 = FrameFile.path(p1, frame[0], 'jpg')

                height, width, _ = cv2.imread(p2).shape

                debug(
                    f'{frame[0]} | {frame[1]} | {frame[2]} | ({width} | '
                    f'{height})', 3)

            offset += length
    def select_curate_manual(self, frame_set_id, opts):
        """
        This method serves a manual frame set curation UI.

        :param int frame_set_id: The frame set ID.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        result = FrameSetModel().select(frame_set_id)
        if result is None:
            raise CommandLineRouteHandlerError(
                f'Frame set with ID {frame_set_id:08d} not found')

        plugin = Plugin.get('frame_set_select_curate', 'manual')

        plugin().frame_set_select_curate(frame_set_id, opts)
예제 #18
0
    def select_curate_manual(self, transform_set_id, opts):
        """
        This method serves a manual transform set curation UI.

        :param int transform_set_id: The transform set ID.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        result = TransformSetModel().select(transform_set_id)
        if result is None:
            raise CommandLineRouteHandlerError(
                f'Transform set with ID {transform_set_id:08d} not found')

        plugin = Plugin.get('transform_set_select_curate', 'manual')

        plugin().transform_set_select_curate(transform_set_id, opts)
    def select_clone(self, frame_set_ids):
        """
        This method clones frame sets to new frame sets.

        :param list(int) frame_set_ids: The frame set IDs.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        frame_set_model = FrameSetModel()

        for frame_set_id in frame_set_ids:
            result = frame_set_model.select(frame_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Frame set with ID {frame_set_id:08d} not found')

        for frame_set_id in frame_set_ids:
            result = frame_set_model.select(frame_set_id)

            self.select_merge([frame_set_id], result[1], rejected=True)
예제 #20
0
    def insert_image(self, image_path, opts):
        """
        This method inserts a video from an image.

        :param str image_path: The path to the image.
        :param dict opts: The dict of options.
        :rtype: None
        """

        with tempdir() as tempdir_:
            video_path = os.path.join(tempdir_, 'video.mp4')

            ret = create_one_video_file_from_one_image_file(
                image_path, video_path,
                frame_count=int(opts.get('frame-count', '1')))
            if ret is False:
                raise CommandLineRouteHandlerError(
                    'An error occured while attempting to create one video '
                    'file from one image file')

            self.insert_file(video_path, opts)
    def delete(self, frame_set_ids):
        """
        This method deletes a frame set from the frame set collection.

        :param list(int) frame_set_ids: The frame set IDs.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        frame_set_model = FrameSetModel()

        for frame_set_id in frame_set_ids:
            result = frame_set_model.select(frame_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Frame set with ID {frame_set_id:08d} not found')

        for frame_set_id in frame_set_ids:
            frame_set_model.delete(frame_set_id)

            shutil.rmtree(FrameSetSubDir.path(frame_set_id))

            debug(f'Frame set {frame_set_id} was successfully deleted', 3)
예제 #22
0
    def vimeo_stream(self, video, quality='hq'):
        """
        This method selects the given video stream that matches the specified
        quality. Quality can be:
          - hq: highest quality
          - lq: lowest quality
          - YYYxZZZ: video with the given dimensions
        If a certain quality video can't be found than a
        CommandLineRouteHandlerError is raised.

        :param vimeo_dl.vimeo_internal.InternVimeo video: The Vimeo video.
        :param str quality: The quality of the video stream to download.
        :rtype: vimeo_dl.vimeo_internal.InternStream
        """

        stream = None
        if quality != 'hq' or quality != 'lq':
            for s in video.streams:
                info = str(s).split('@')[1]
                if info == quality:
                    stream = s
                    break
    
            if stream is None:
                options = ', '.join([str(s).split('@')[1]
                                     for s in video.streams])
                raise CommandLineRouteHandlerError(
                    f'A video with the dimensions "{quality}" could not be '
                    f'found. Available quality options: {options}.'
                )
    
        if quality == 'lq':
            stream = video.streams[-1]
        elif stream is None:
            stream = video.streams[0]
        
        return stream
예제 #23
0
    def delete(self, transform_set_ids):
        """
        This method deletes a transform set from the transform set collection.

        :param list(int) transform_set_ids: The transform set IDs.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        transform_set_model = TransformSetModel()

        for transform_set_id in transform_set_ids:
            result = transform_set_model.select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        for transform_set_id in transform_set_ids:
            transform_set_model.delete(transform_set_id)

            shutil.rmtree(TransformSetSubDir.path(transform_set_id))

            debug(f'Transform set {transform_set_id} was successfully deleted',
                  3)
예제 #24
0
    def insert_file(self, path, opts={}):
        """
        This method inserts a local video into the video colletion.

        :param str path: The path to the video.
        :param dict opts: The optional dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        if not os.path.isfile(path):
            raise CommandLineRouteHandlerError(f'File {path} not found')

        filename = self.uuid() + os.path.splitext(path)[1]

        shutil.copy(path, VideoDir.path() + '/' + filename)

        desc = opts.get('description', None)

        video_id = VideoModel().insert(path, filename, desc)

        debug(
            f'video_id={video_id}, uri={path}, filename={filename}, '
            f'description={desc}', 3)
예제 #25
0
    def select_merge(self,
                     transform_set_ids,
                     name='merge',
                     fk_frame_sets=None,
                     fk_prev_transform_sets=None,
                     rejected=False):
        """
        This method merges multiple transform sets into one transform set.

        :param list(int) transform_set_ids: The transform set IDs.
        :param str name: The merged transform set's name. The default value is
            'merge'.
        :param int fk_frame_sets: The frame set ID to which this transform set
            corresponds (if any). The default value is None.
        :param int fk_prev_transform_sets: The transform set ID the previous
            transform set in this series of transformations. The default value
            is None.
        :param bool rejected: True if should include rejected else False if
            should not. The default value is False.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        transform_set_model = TransformSetModel()

        for transform_set_id in transform_set_ids:
            result = transform_set_model.select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        transform_set_id = transform_set_model.insert(name, fk_frame_sets,
                                                      fk_prev_transform_sets)

        p1 = TransformSetSubDir.path(transform_set_id)

        os.makedirs(p1)

        transform_model = TransformModel()
        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))

        for transform_set_id_ in transform_set_ids:
            offset = 0
            p2 = TransformSetSubDir.path(transform_set_id_)

            while True:
                transforms = transform_model.list(transform_set_id_,
                                                  length=length,
                                                  offset=offset,
                                                  rejected=rejected)

                if not transforms:
                    break

                for transform in transforms:
                    transform_id = transform_model.insert(
                        transform_set_id, transform[2], transform[3],
                        transform[4])

                    p3 = TransformFile.path(p2, transform[0], 'jpg')
                    p4 = TransformFile.path(p1, transform_id, 'jpg')

                    shutil.copy(p3, p4)

                    debug(
                        f'Transform with ID {transform[0]:08d} at {p3} '
                        f'merged as ID {transform_id:08d} at {p4}', 4)

                offset += length

        result = transform_set_model.select(transform_set_id)

        debug(
            f'transform_set_id={result[0]}, name={result[1]}, '
            f'fk_frame_sets={result[2]}, fk_prev_transform_sets={result[3]}',
            3)
예제 #26
0
    def select_export_video(self, transform_set_ids, target_dir):
        """
        This method exports transform sets to videos.

        :param list transform_set_ids: The list of transform set IDs.
        :param str target_dir: The directory to which to export the videos.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        for transform_set_id in transform_set_ids:
            result = TransformSetModel().select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        if not os.path.isdir(target_dir):
            raise CommandLineRouteHandlerError(
                f'{target_dir} is not a directory')

        transform_model = TransformModel()
        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))
        count = 0

        for transform_set_id in transform_set_ids:
            video_path = os.path.join(target_dir,
                                      f'{transform_set_id:08X}.mp4')

            def image_paths():
                offset = 0
                p1 = TransformSetSubDir.path(transform_set_id)

                while True:
                    transforms = transform_model.list(transform_set_id,
                                                      length=length,
                                                      offset=offset,
                                                      rejected=False)

                    if not transforms:
                        break

                    for transform in transforms:
                        image_path = TransformFile.path(
                            p1, transform[0], 'jpg')

                        yield image_path

                        debug(
                            f'Transform with ID {transform[0]:08d} at '
                            f'{image_path} exported to {video_path}', 4)

                    offset += length

            ret = create_one_video_file_from_many_image_files(
                image_paths, video_path)
            if ret is False:
                raise CommandLineRouteHandlerError(
                    'An error occured while attempting to create one '
                    'video file from many image files')

            count += 1

            debug(
                f'Transform set with ID {transform_set_id:08d} exported to '
                f'{video_path}', 4)

        debug(f'{count} videos were successfully exported to {target_dir}', 3)
예제 #27
0
    def video_select_extract(self, video_id, sub_sample=1, max_sample=0):
        """
        This method extracts frames and thumbnails from a video to a frame set.

        :param int video_id: The video ID.
        :param int sub_sample: Sample frames at a rate of 1 sample per
            sub_sample rate. For example, if sub_sample is 30, then sample
            frames at a rate of 1 frame per 30 frames. The default value of 1
            indicates to sample every frame.
        :param int max_sample: Sample up to maximum count of frames. For
            example, sample up to 1000 total frames (and then cease to sample).
            The default value of 0 indicates that there is no maximum count of
            frames.
        :raises: CommandLineRouteHandlerError
        :rtype: int
        """

        result = VideoModel().select(video_id)

        p1 = VideoFile.path(result[2])

        vc = cv2.VideoCapture(p1)

        try:
            if not vc.isOpened():
                raise CommandLineRouteHandlerError(
                    f'OpenCV VideoCapture isOpened returned false for {p1}')

            frame_set_id = FrameSetModel().insert(video_id)

            p2 = FrameSetSubDir.path(frame_set_id)

            os.makedirs(p2)

            frame_model = FrameModel()

            sub_sample_ = sub_sample
            max_sample_ = 0

            while True:
                ret, frame = vc.read()
                if not ret:
                    break

                # sub_sample
                if sub_sample_ != sub_sample:
                    sub_sample_ += 1
                    continue
                else:
                    sub_sample_ = 1

                frame_id = frame_model.insert(frame_set_id, 0)

                p3 = FrameFile.path(p2, frame_id, 'jpg')

                cv2.imwrite(p3, frame, [cv2.IMWRITE_JPEG_QUALITY, 100])

                # imutils.resize preserves aspect ratio.
                thumbnail = imutils.resize(frame, width=192, height=192)

                p4 = FrameFile.path(p2, frame_id, 'jpg', '192x192')

                # can adjust jpeg quality thus impacting file size
                cv2.imwrite(p4, thumbnail, [cv2.IMWRITE_JPEG_QUALITY, 100])

                debug(
                    f'Frame with ID {frame_id:08d} and thumbnail extracted '
                    f'to {p3} and {p4}', 4)

                # max_sample
                max_sample_ += 1

                if max_sample > 0:
                    if max_sample_ == max_sample:
                        break
        finally:
            vc.release()

        return frame_set_id
예제 #28
0
    def select_export_dir(self, transform_set_ids, target_dir, opts={}):
        """
        This method exports transform sets to a directory.

        :param list transform_set_ids: The list of transform set IDs.
        :param str target_dir: The directory to which to export the transform
            sets.
        :param dict opts: The dict of options.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        transform_set_model = TransformSetModel()

        for transform_set_id in transform_set_ids:
            result = transform_set_model.select(transform_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Transform set with ID {transform_set_id:08d} not found')

        if not os.path.isdir(target_dir):
            raise CommandLineRouteHandlerError(
                f'{target_dir} is not a directory')

        transform_model = TransformModel()
        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))
        count = 0

        for tid in transform_set_ids:
            offset = 0
            transform_set_path = TransformSetSubDir.path(tid)

            while True:
                results = transform_model.list(tid,
                                               length=length,
                                               offset=offset,
                                               rejected=False)

                if not results:
                    break

                for transform_id, _, _, _, _ in results:
                    file_path = TransformFile().path(transform_set_path,
                                                     transform_id, 'jpg')
                    if 'format' in opts:
                        filename = opts['format'] % transform_id
                    else:
                        filename = os.path.basename(file_path)

                    target_path = os.path.join(target_dir, filename)
                    shutil.copy(file_path, target_path)
                    count += 1

                    debug(
                        f'Transform with ID {transform_id:08d} at '
                        f'{file_path} exported to {target_path}', 4)

                offset += length

        debug(f'{count} transforms were successfully exported to {target_dir}',
              3)
    def select_merge(self, frame_set_ids, video_id=None, rejected=False):
        """
        This method merges multiple frame sets into one frame set.

        :param list(int) frame_set_ids: The frame set IDs.
        :param int video_id: The video ID to which this frame set corresponds
             (if any). The default value is None.
        :param bool rejected: True if should include rejected frames else False
            if should not. The default value is False.
        :raises: CommandLineRouteHandlerError
        :rtype: None
        """

        frame_set_model = FrameSetModel()

        for frame_set_id in frame_set_ids:
            result = frame_set_model.select(frame_set_id)
            if result is None:
                raise CommandLineRouteHandlerError(
                    f'Frame set with ID {frame_set_id:08d} not found')

        frame_set_id = frame_set_model.insert(video_id)

        p1 = FrameSetSubDir.path(frame_set_id)

        os.makedirs(p1)

        frame_model = FrameModel()
        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))

        for frame_set_id_ in frame_set_ids:
            offset = 0
            p2 = FrameSetSubDir.path(frame_set_id_)

            while True:
                frames = frame_model.list(frame_set_id_,
                                          length=length,
                                          offset=offset,
                                          rejected=rejected)

                if not frames:
                    break

                for frame in frames:
                    frame_id = frame_model.insert(frame_set_id, frame[2])

                    p3 = FrameFile.path(p2, frame[0], 'jpg')
                    p4 = FrameFile.path(p2, frame[0], 'jpg', '192x192')
                    p5 = FrameFile.path(p1, frame_id, 'jpg')
                    p6 = FrameFile.path(p1, frame_id, 'jpg', '192x192')

                    shutil.copy(p3, p5)
                    shutil.copy(p4, p6)

                    debug(
                        f'Frame with ID {frame[0]:08d} and thumbnail at '
                        f'{p3} and {p4} merged as ID {frame_id:08d} at '
                        f'{p5} and {p6}', 4)

                offset += length

        debug(f'frame_set_id={frame_set_id}, fk_videos={video_id}', 3)