Exemple #1
0
    def test_transform_set_select_extract_pad_rejected(self):
        with deepstar_path():
            video_0001 = os.path.dirname(os.path.realpath(
                __file__)) + '/../../support/video_0001.mp4'  # noqa

            shutil.copyfile(video_0001, VideoFile.path('video_0001.mp4'))

            VideoModel().insert('test', 'video_0001.mp4')

            DefaultVideoSelectExtractPlugin().video_select_extract(1)  # noqa

            self.mock_transform_set()

            MaxSizeTransformSetSelectExtractPlugin(
            ).transform_set_select_extract(1, {})  # noqa

            transform_model = TransformModel()
            transform_model.update(7, rejected=1)
            transform_model.update(9, rejected=1)

            with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}):
                transform_set_id = PadTransformSetSelectExtractPlugin(
                ).transform_set_select_extract(2, {})  # noqa

            self.assertEqual(transform_set_id, 3)

            # db
            result = TransformSetModel().select(3)
            self.assertEqual(result, (3, 'pad', 1, 2))

            result = TransformModel().list(3)
            self.assertEqual(len(result), 3)
            t = list(result[0])
            json.loads(t.pop(3))
            self.assertEqual(t, [11, 3, 1, 0])
            t = list(result[1])
            json.loads(t.pop(3))
            self.assertEqual(t, [12, 3, 3, 0])
            t = list(result[2])
            json.loads(t.pop(3))
            self.assertEqual(t, [13, 3, 5, 0])

            # files
            p1 = TransformSetSubDir.path(3)

            # transforms
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 11,
                                                                'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 12,
                                                                'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 13,
                                                                'jpg')),
                                  np.ndarray)  # noqa
    def transform_set_select_curate(self, transform_set_id, opts):
        """
        This method automatically curates a transform set and rejects
        transforms that are more blurry than the 'max-blur'.

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

        if 'max-blur' not in opts:
            raise ValueError(
                'The max-blur option is required but was not supplied')

        max_blur = float(opts['max-blur'])

        transform_model = TransformModel()
        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')

                debug(f'Curating transform with ID {transform[0]:08d} at {p2}',
                      4)

                image = cv2.imread(p2)

                image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

                h, w = image.shape[:2]

                # recommendation to scale down image to ~500
                if h > 600 or w > 600:
                    # imutils.resize preserves aspect ratio.
                    image = imutils.resize(image, width=500, height=500)

                score = cv2.Laplacian(image, cv2.CV_64F).var()

                if score < max_blur:
                    transform_model.update(transform[0], rejected=1)

                    debug(f'Transform with ID {transform[0]:08d} rejected', 4)

            offset += length
    def test_transform_set_select_merge_overlay_image_rejected(self):
        with deepstar_path():
            with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}):
                route_handler = VideoCommandLineRouteHandler()

                video_0001 = os.path.dirname(os.path.realpath(
                    __file__)) + '/../../support/video_0001.mp4'  # noqa

                route_handler.insert_file(video_0001)

                route_handler.select_extract([1])

                route_handler = FrameSetCommandLineRouteHandler()

                route_handler.select_extract([1], 'transform_set', {})

                transform_model = TransformModel()

                transform_model.update(1, rejected=1)

            image_0007 = os.path.dirname(os.path.realpath(
                __file__)) + '/../../support/image_0007.png'  # noqa

            OverlayImageTransformSetSelectMergePlugin(
            ).transform_set_select_merge([1], {
                'image-path': image_0007,
                'x1': '0',
                'y1': '0'
            })  # noqa

            # db
            result = TransformSetModel().select(2)
            self.assertEqual(result, (2, 'overlay_image', None, None))

            result = TransformModel().list(2)
            self.assertEqual(len(result), 4)
            self.assertEqual(result[0], (6, 2, None, None, 0))
            self.assertEqual(result[1], (7, 2, None, None, 0))
            self.assertEqual(result[2], (8, 2, None, None, 0))
            self.assertEqual(result[3], (9, 2, None, None, 0))

            # files
            p1 = TransformSetSubDir.path(2)

            # transforms
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 6, 'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 7, 'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 8, 'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 9, 'jpg')),
                                  np.ndarray)  # noqa
    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
Exemple #5
0
    def test_select(self):
        with deepstar_path():
            FrameSetModel().insert(None)
            FrameModel().insert(1, 0)

            TransformSetModel().insert('test', 1)

            transform_model = TransformModel()
            transform_model.insert(1, 1, '{}', 0)

            result = transform_model.select(1)
            self.assertEqual(result, (1, 1, 1, '{}', 0))
    def test_transform_set_select_extract_crop_rejected(self):
        with deepstar_path():
            with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}):
                route_handler = VideoCommandLineRouteHandler()

                video_0001 = os.path.dirname(os.path.realpath(
                    __file__)) + '/../../support/video_0001.mp4'  # noqa

                route_handler.insert_file(video_0001)

                route_handler.select_extract([1])

                FrameSetCommandLineRouteHandler().select_extract(
                    [1], 'transform_set', {})  # noqa

                TransformModel().update(5, rejected=1)

            CropTransformSetSelectExtractPlugin().transform_set_select_extract(
                1, {
                    'x1': '0',
                    'y1': '0',
                    'x2': '50',
                    'y2': '50'
                })  # noqa

            # db
            result = TransformSetModel().select(2)
            self.assertEqual(result, (2, 'crop', 1, 1))

            result = TransformModel().list(2)
            self.assertEqual(len(result), 4)
            self.assertEqual(result[0], (6, 2, 1, None, 0))
            self.assertEqual(result[1], (7, 2, 2, None, 0))
            self.assertEqual(result[2], (8, 2, 3, None, 0))
            self.assertEqual(result[3], (9, 2, 4, None, 0))

            # files
            p1 = TransformSetSubDir.path(2)

            # transforms
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 6, 'jpg')).shape[:2],
                (50, 50))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 7, 'jpg')).shape[:2],
                (50, 50))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 8, 'jpg')).shape[:2],
                (50, 50))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 9, 'jpg')).shape[:2],
                (50, 50))  # noqa
    def test_transform_set_select_extract_max_size_rejected(self):
        with deepstar_path():
            video_0001 = os.path.dirname(os.path.realpath(
                __file__)) + '/../../support/video_0001.mp4'  # noqa

            shutil.copyfile(video_0001, VideoFile.path('video_0001.mp4'))

            VideoModel().insert('test', 'video_0001.mp4')

            DefaultVideoSelectExtractPlugin().video_select_extract(1)  # noqa

            self.mock_transform_set()

            transform_model = TransformModel()
            transform_model.update(2, rejected=1)
            transform_model.update(4, rejected=1)

            with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}):
                transform_set_id = MaxSizeTransformSetSelectExtractPlugin(
                ).transform_set_select_extract(1, {})  # noqa

            self.assertEqual(transform_set_id, 2)

            # db
            result = TransformSetModel().select(2)
            self.assertEqual(result, (2, 'max_size', 1, 1))

            result = TransformModel().list(2)
            self.assertEqual(len(result), 3)
            t = list(result[0])
            json.loads(t.pop(3))
            self.assertEqual(t, [6, 2, 1, 0])
            t = list(result[1])
            json.loads(t.pop(3))
            self.assertEqual(t, [7, 2, 3, 0])
            t = list(result[2])
            json.loads(t.pop(3))
            self.assertEqual(t, [8, 2, 5, 0])

            # files
            p1 = TransformSetSubDir.path(2)

            # transforms
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 6, 'jpg')).shape[0],
                299)  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 7, 'jpg')).shape[0],
                299)  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 8, 'jpg')).shape[0],
                299)  # noqa
            def put(self, transform_set_id, transform_id):
                transform_model = TransformModel()

                result = transform_model.update(
                    transform_id, rejected=request.get_json()['rejected'])
                if result is False:
                    abort(404)

                result = transform_model.select(transform_id)
                if result is None:
                    abort(404)

                return jsonify(result)
Exemple #9
0
    def _pad(self, transform_set_path, transform_id, frame_id, metadata,
             target_set_id, size):
        """
        This method pads a transform.

        :param str transform_set_path: The transform set path.
        :param int transform_id: The transform ID.
        :param int frame_id: The frame ID.
        :param str metadata: Metadata for the transform.
        :param int target_set_id: The new transform set ID.
        :param int size: The size to which to pad.
        :rtype: None
        """

        transform_path = TransformFile().path(transform_set_path, transform_id,
                                              'jpg')
        img = cv2.imread(transform_path)
        img_height, img_width = img.shape[:2]

        img_padded = np.zeros((size, size, 3), dtype=np.uint8)
        img_padded[:img_height, :img_width, :] = img.copy()

        target_id = TransformModel().insert(target_set_id, frame_id,
                                            json.dumps(metadata), 0)
        output_path = TransformFile.path(
            TransformSetSubDir.path(target_set_id), target_id, 'jpg')

        cv2.imwrite(output_path, img_padded, [cv2.IMWRITE_JPEG_QUALITY, 100])

        debug(
            f'Transform with ID {target_id:08d} at {output_path} extracted '
            f'from transform with ID {transform_id:08d} at {transform_path}',
            4)
    def transform_set_select_extract(self, transform_set_id, opts):
        """
        This method extracts just the mouth from an image of a face.

        :param int transform_set_id: The transform set ID.
        :rtype: int
        """

        self._offset_percent = int(opts.get('offset-percent', 20)) / 100

        transform_set_path = TransformSetSubDir.path(transform_set_id)
        _, _, frame_set_id, _ = TransformSetModel().select(transform_set_id)

        target_set_id = TransformSetModel().insert(f'{self.name}',
                                                   frame_set_id,
                                                   transform_set_id)

        target_path = TransformSetSubDir.path(target_set_id)
        os.makedirs(target_path)

        result = TransformModel().list(transform_set_id)

        for transform_id, _, frame_id, metadata, rejected in result:
            if rejected == 1:
                continue

            metadata = json.loads(metadata)
            self._get_mouth(transform_set_path, transform_id, frame_id,
                            metadata, target_set_id)

        return target_set_id
Exemple #11
0
    def mock_transform_set(self):
        image_0001 = os.path.dirname(os.path.realpath(
            __file__)) + '/../../support/image_0001.jpg'  # noqa

        TransformSetModel().insert('face', 1, None)

        p1 = TransformSetSubDir.path(1)

        os.mkdir(p1)

        transform_model = TransformModel()

        for i in range(0, 5):
            transform_model.insert(1, i + 1, '{}', 0)

            shutil.copy(image_0001, TransformFile.path(p1, i + 1, 'jpg'))
    def _resize(self, transform_set_path, transform_id, frame_id, metadata,
                target_set_id, max_size):
        """
        This method resizes a transform.

        :param str transform_set_path: The transform set path.
        :param int transform_id:  The transform ID.
        :param int frame_id: The frame ID.
        :param str metadata: Metadata for the transform.
        :param int target_set_id: The new transform set ID.
        :param int max_size: The max size.
        :rtype: None
        """

        transform_path = TransformFile().path(transform_set_path, transform_id,
                                              'jpg')
        img = cv2.imread(transform_path)
        img_height, img_width = img.shape[:2]

        if img_height > max_size or img_width > max_size:
            if img_height > img_width:
                img = imutils.resize(img, height=max_size)
            else:
                img = imutils.resize(img, width=max_size)

        target_id = TransformModel().insert(target_set_id, frame_id,
                                            json.dumps(metadata), 0)
        output_path = TransformFile.path(
            TransformSetSubDir.path(target_set_id), target_id, 'jpg')

        cv2.imwrite(output_path, img, [cv2.IMWRITE_JPEG_QUALITY, 100])

        debug(f'Transform with ID {target_id:08d} at {output_path} extracted '
              f'from transform with ID {transform_id:08d} at {transform_path}',
              4)
Exemple #13
0
    def test_fk_transform_sets_fk_frames_constraint(self):
        with deepstar_path():
            FrameSetModel().insert(None)

            TransformSetModel().insert('test', 1)

            with self.assertRaises(sqlite3.IntegrityError):
                TransformModel().insert(1, 1, '{}', 0)
    def frame_set_select_extract(self, frame_set_id, opts):
        """
        This method extracts a frame set to a transform set.

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

        transform_set_id = TransformSetModel().insert('transform_set',
                                                      frame_set_id)

        p1 = TransformSetSubDir.path(transform_set_id)

        os.mkdir(p1)

        frame_model = FrameModel()
        transform_model = TransformModel()
        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))
        offset = 0
        p2 = FrameSetSubDir.path(frame_set_id)

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

            if not frames:
                break

            for frame in frames:
                transform_id = transform_model.insert(transform_set_id,
                                                      frame[0], None, 0)

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

                shutil.copy(p3, p4)

                debug(f'Transform with ID {transform_id:08d} at {p4} '
                      f'extracted from frame with ID {frame[0]:08d} at '
                      f'{p3}', 4)

            offset += length

        return transform_set_id
Exemple #15
0
    def test_select_extract_face(self):
        with deepstar_path():
            with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}):
                route_handler = VideoCommandLineRouteHandler()

                video_0001 = os.path.dirname(os.path.realpath(__file__)) + '/../../support/video_0001.mp4'  # noqa

                route_handler.insert_file(video_0001)

                route_handler.select_extract([1])

            args = ['main.py', 'select', 'frame_sets', '1', 'extract', 'face']  # noqa
            opts = {}

            route_handler = FrameSetCommandLineRouteHandler()

            try:
                sys.stdout = StringIO()
                route_handler.handle(args, opts)
                actual = sys.stdout.getvalue().strip()
            finally:
                sys.stdout = sys.__stdout__

            # stdout
            self.assertEqual(actual, 'transform_set_id=1, name=face, fk_frame_sets=1, fk_prev_transform_sets=None')  # noqa

            # db
            result = TransformSetModel().select(1)
            self.assertEqual(result, (1, 'face', 1, None))

            result = TransformModel().list(1)
            self.assertEqual(len(result), 5)
            t = list(result[0])
            json.loads(t.pop(3))
            self.assertEqual(t, [1, 1, 1, 0])
            t = list(result[1])
            json.loads(t.pop(3))
            self.assertEqual(t, [2, 1, 2, 0])
            t = list(result[2])
            json.loads(t.pop(3))
            self.assertEqual(t, [3, 1, 3, 0])
            t = list(result[3])
            json.loads(t.pop(3))
            self.assertEqual(t, [4, 1, 4, 0])
            t = list(result[4])
            json.loads(t.pop(3))
            self.assertEqual(t, [5, 1, 5, 0])

            # files
            p1 = TransformSetSubDir.path(1)

            # transforms
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 1, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 2, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 3, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 4, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 5, 'jpg')))
Exemple #16
0
    def transform_set_select_curate(self, transform_set_id, opts):
        """
        This method automatically curates a transform set and rejects
        transforms with width or height less than 'min-size'.

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

        if 'min-size' not in opts:
            raise ValueError(
                'The min-size option is required but was not supplied')

        min_length = int(opts['min-size'])

        transform_model = TransformModel()
        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')

                debug(f'Curating transform with ID {transform[0]:08d} at {p2}',
                      4)

                h, w = cv2.imread(p2).shape[:2]

                if h < min_length or w < min_length:
                    transform_model.update(transform[0], rejected=1)

                    debug(f'Transform with ID {transform[0]:08d} rejected', 4)

            offset += length
Exemple #17
0
    def test_transform_set_select_merge_overlay(self):
        with deepstar_path():
            with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}):
                route_handler = VideoCommandLineRouteHandler()

                video_0001 = os.path.dirname(os.path.realpath(
                    __file__)) + '/../../support/video_0001.mp4'  # noqa

                route_handler.insert_file(video_0001)

                route_handler.select_extract([1])

                route_handler = FrameSetCommandLineRouteHandler()

                route_handler.select_extract([1], 'transform_set', {})
                route_handler.select_extract([1], 'transform_set', {})

            OverlayTransformSetSelectMergePlugin().transform_set_select_merge(
                [1, 2], {
                    'x1': '0',
                    'y1': '0'
                })  # noqa

            # db
            result = TransformSetModel().select(3)
            self.assertEqual(result, (3, 'overlay', None, None))

            result = TransformModel().list(3)
            self.assertEqual(len(result), 5)
            self.assertEqual(result[0], (11, 3, None, None, 0))
            self.assertEqual(result[1], (12, 3, None, None, 0))
            self.assertEqual(result[2], (13, 3, None, None, 0))
            self.assertEqual(result[3], (14, 3, None, None, 0))
            self.assertEqual(result[4], (15, 3, None, None, 0))

            # files
            p1 = TransformSetSubDir.path(3)

            # transforms
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 11,
                                                                'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 12,
                                                                'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 13,
                                                                'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 14,
                                                                'jpg')),
                                  np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 15,
                                                                'jpg')),
                                  np.ndarray)  # noqa
    def test_transform_set_select_extract_slice_rejected(self):
        with deepstar_path():
            with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}):
                route_handler = VideoCommandLineRouteHandler()

                video_0001 = os.path.dirname(os.path.realpath(
                    __file__)) + '/../../support/video_0001.mp4'  # noqa

                route_handler.insert_file(video_0001)

                route_handler.select_extract([1])

                FrameSetCommandLineRouteHandler().select_extract(
                    [1], 'transform_set', {})  # noqa

                TransformModel().update(4, rejected=1)
                TransformModel().update(5, rejected=1)

            SliceTransformSetSelectExtractPlugin(
            ).transform_set_select_extract(1, {
                'start': '2',
                'end': '4'
            })  # noqa

            # db
            result = TransformSetModel().select(2)
            self.assertEqual(result, (2, 'slice', 1, 1))

            result = TransformModel().list(2)
            self.assertEqual(len(result), 2)
            self.assertEqual(result[0], (6, 2, 2, None, 0))
            self.assertEqual(result[1], (7, 2, 3, None, 0))

            # files
            p1 = TransformSetSubDir.path(2)

            # transforms
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 6, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 7, 'jpg')))
    def test_transform_set_select_extract_resize_height(self):
        with deepstar_path():
            with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}):
                route_handler = VideoCommandLineRouteHandler()

                video_0001 = os.path.dirname(os.path.realpath(
                    __file__)) + '/../../support/video_0001.mp4'  # noqa

                route_handler.insert_file(video_0001)

                route_handler.select_extract([1])

                FrameSetCommandLineRouteHandler().select_extract(
                    [1], 'transform_set', {})  # noqa

            ResizeTransformSetSelectExtractPlugin(
            ).transform_set_select_extract(1, {'height': '100'})  # noqa

            # db
            result = TransformSetModel().select(2)
            self.assertEqual(result, (2, 'resize', 1, 1))

            result = TransformModel().list(2)
            self.assertEqual(len(result), 5)
            self.assertEqual(result[0], (6, 2, 1, None, 0))
            self.assertEqual(result[1], (7, 2, 2, None, 0))
            self.assertEqual(result[2], (8, 2, 3, None, 0))
            self.assertEqual(result[3], (9, 2, 4, None, 0))
            self.assertEqual(result[4], (10, 2, 5, None, 0))

            # files
            p1 = TransformSetSubDir.path(2)

            # transforms
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 6, 'jpg')).shape[:2],
                (100, 177))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 7, 'jpg')).shape[:2],
                (100, 177))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 8, 'jpg')).shape[:2],
                (100, 177))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 9, 'jpg')).shape[:2],
                (100, 177))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 10, 'jpg')).shape[:2],
                (100, 177))  # noqa
    def test_frame_set_select_extract_face(self):
        with deepstar_path():
            video_0001 = os.path.dirname(os.path.realpath(__file__)) + '/../../support/video_0001.mp4'  # noqa

            shutil.copyfile(video_0001, VideoFile.path('video_0001.mp4'))

            VideoModel().insert('test', 'video_0001.mp4')

            DefaultVideoSelectExtractPlugin().video_select_extract(1)  # noqa

            with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}):
                transform_set_id = MTCNNFrameSetSelectExtractPlugin().frame_set_select_extract(1, {})  # noqa

            self.assertEqual(transform_set_id, 1)

            # db
            result = TransformSetModel().select(1)
            self.assertEqual(result, (1, 'face', 1, None))

            result = TransformModel().list(1)
            self.assertEqual(len(result), 5)
            t = list(result[0])
            json.loads(t.pop(3))
            self.assertEqual(t, [1, 1, 1, 0])
            t = list(result[1])
            json.loads(t.pop(3))
            self.assertEqual(t, [2, 1, 2, 0])
            t = list(result[2])
            json.loads(t.pop(3))
            self.assertEqual(t, [3, 1, 3, 0])
            t = list(result[3])
            json.loads(t.pop(3))
            self.assertEqual(t, [4, 1, 4, 0])
            t = list(result[4])
            json.loads(t.pop(3))
            self.assertEqual(t, [5, 1, 5, 0])

            # files
            p1 = TransformSetSubDir.path(1)

            # transforms
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 1, 'jpg')), np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 2, 'jpg')), np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 3, 'jpg')), np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 4, 'jpg')), np.ndarray)  # noqa
            self.assertIsInstance(cv2.imread(TransformFile.path(p1, 5, 'jpg')), np.ndarray)  # noqa
Exemple #21
0
    def test_fk_transform_sets_on_delete_cascade(self):
        with deepstar_path():
            FrameSetModel().insert(None)
            FrameModel().insert(1, 0)

            transform_set_model = TransformSetModel()
            transform_set_model.insert('test', 1)
            transform_model = TransformModel()
            transform_model.insert(1, 1, '{}', 0)
            self.assertEqual(transform_model.list(1), [(1, 1, 1, '{}', 0)])
            transform_set_model.delete(1)
            self.assertIsNone(transform_model.list(1))
            def get(self, transform_set_id):
                if 'offset' in request.args:
                    offset = int(request.args['offset'])
                else:
                    offset = 0

                if 'length' in request.args:
                    length = int(request.args['length'])
                else:
                    length = 100

                result = TransformModel().list(transform_set_id,
                                               length=length,
                                               offset=offset)
                if result is None:
                    abort(404)

                return jsonify(result)
Exemple #23
0
    def test_count_rejected_false(self):
        with deepstar_path():
            FrameSetModel().insert(None)

            frame_model = FrameModel()
            frame_model.insert(1, 0)
            frame_model.insert(1, 0)
            frame_model.insert(1, 0)

            TransformSetModel().insert('test', 1)

            transform_model = TransformModel()
            transform_model.insert(1, 1, '{}', 0)
            transform_model.insert(1, 2, '{}', 1)
            transform_model.insert(1, 3, '{}', 0)

            self.assertEqual(transform_model.count(1, rejected=False), 2)
Exemple #24
0
    def test_transform_set_select_extract_adjust_color(self):
        with deepstar_path():
            with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}):
                route_handler = VideoCommandLineRouteHandler()

                video_0001 = os.path.dirname(os.path.realpath(
                    __file__)) + '/../../support/video_0001.mp4'  # noqa

                route_handler.insert_file(video_0001)

                route_handler.select_extract([1])

                FrameSetCommandLineRouteHandler().select_extract(
                    [1], 'transform_set', {})  # noqa

            AdjustColorTransformSetSelectExtractPlugin(
            ).transform_set_select_extract(1, {
                'r': '+10',
                'g': '-10',
                'b': '+10'
            })  # noqa

            # db
            result = TransformSetModel().select(2)
            self.assertEqual(result, (2, 'adjust_color', 1, 1))

            result = TransformModel().list(2)
            self.assertEqual(len(result), 5)
            self.assertEqual(result[0], (6, 2, 1, None, 0))
            self.assertEqual(result[1], (7, 2, 2, None, 0))
            self.assertEqual(result[2], (8, 2, 3, None, 0))
            self.assertEqual(result[3], (9, 2, 4, None, 0))
            self.assertEqual(result[4], (10, 2, 5, None, 0))

            # files
            p1 = TransformSetSubDir.path(2)

            # transforms
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 6, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 7, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 8, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 9, 'jpg')))
            self.assertTrue(os.path.isfile(TransformFile.path(p1, 10, 'jpg')))
Exemple #25
0
    def test_update(self):
        with deepstar_path():
            FrameSetModel().insert(None)
            FrameModel().insert(1, 0)

            TransformSetModel().insert('test', 1)

            transform_model = TransformModel()
            transform_model.insert(1, 1, 'test1', 0)

            result = transform_model.select(1)
            self.assertEqual(result, (1, 1, 1, 'test1', 0))

            result = transform_model.update(1, 'test2', 1)
            self.assertTrue(result)

            result = transform_model.select(1)
            self.assertEqual(result, (1, 1, 1, 'test2', 1))
Exemple #26
0
    def transform_set_select_extract(self, transform_set_id, opts):
        """
        This method pads each transform in a transform set.

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

        size = int(opts.get('size', 299))

        transform_set_path = TransformSetSubDir.path(transform_set_id)
        _, _, frame_set_id, _ = TransformSetModel().select(transform_set_id)

        target_set_id = TransformSetModel().insert(f'{self.name}',
                                                   frame_set_id,
                                                   transform_set_id)

        target_path = TransformSetSubDir.path(target_set_id)
        os.makedirs(target_path)

        length = int(os.environ.get('MODEL_LIST_LENGTH', '100'))
        offset = 0

        while True:
            result = TransformModel().list(transform_set_id,
                                           length=length,
                                           offset=offset,
                                           rejected=False)

            if not result:
                break

            for transform_id, _, frame_id, metadata, rejected in result:
                self._pad(transform_set_path, transform_id, frame_id, metadata,
                          target_set_id, size)

            offset += length

        return target_set_id
Exemple #27
0
    def test_count(self):
        with deepstar_path():
            FrameSetModel().insert(None)

            frame_model = FrameModel()
            frame_model.insert(1, 0)
            frame_model.insert(1, 0)
            frame_model.insert(1, 0)

            TransformSetModel().insert('test', 1)

            transform_model = TransformModel()
            transform_model.insert(1, 1, '{}', 0)
            transform_model.insert(1, 2, '{}', 0)
            transform_model.insert(1, 3, '{}', 0)

            self.assertEqual(frame_model.count(1), 3)
Exemple #28
0
    def test_transform_set_select_curate_min_size(self):
        with deepstar_path():
            video_0001 = os.path.dirname(os.path.realpath(
                __file__)) + '/../../support/video_0001.mp4'  # noqa

            shutil.copyfile(video_0001, VideoFile.path('video_0001.mp4'))

            VideoModel().insert('test', 'video_0001.mp4')

            DefaultVideoSelectExtractPlugin().video_select_extract(1)  # noqa

            self.mock_transform_set()

            MinSizeTransformSetSelectCuratePlugin(
            ).transform_set_select_curate(1, {'min-size': '300'})  # noqa

            # db
            result = TransformSetModel().select(1)
            self.assertEqual(result, (1, 'face', 1, None))

            result = TransformModel().list(1)
            self.assertEqual(len(result), 5)
            t = list(result[0])
            json.loads(t.pop(3))
            self.assertEqual(t, [1, 1, 1, 0])
            t = list(result[1])
            json.loads(t.pop(3))
            self.assertEqual(t, [2, 1, 2, 1])
            t = list(result[2])
            json.loads(t.pop(3))
            self.assertEqual(t, [3, 1, 3, 0])
            t = list(result[3])
            json.loads(t.pop(3))
            self.assertEqual(t, [4, 1, 4, 1])
            t = list(result[4])
            json.loads(t.pop(3))
            self.assertEqual(t, [5, 1, 5, 0])
Exemple #29
0
    def test_transform_set_select_extract_pad_size(self):
        with deepstar_path():
            video_0001 = os.path.dirname(os.path.realpath(
                __file__)) + '/../../support/video_0001.mp4'  # noqa

            shutil.copyfile(video_0001, VideoFile.path('video_0001.mp4'))

            VideoModel().insert('test', 'video_0001.mp4')

            DefaultVideoSelectExtractPlugin().video_select_extract(1)  # noqa

            self.mock_transform_set()

            MaxSizeTransformSetSelectExtractPlugin(
            ).transform_set_select_extract(1, {'max-size': '200'})  # noqa

            with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}):
                PadTransformSetSelectExtractPlugin(
                ).transform_set_select_extract(2, {'size': '200'})  # noqa

            # db
            result = TransformSetModel().select(3)
            self.assertEqual(result, (3, 'pad', 1, 2))

            result = TransformModel().list(3)
            self.assertEqual(len(result), 5)
            t = list(result[0])
            json.loads(t.pop(3))
            self.assertEqual(t, [11, 3, 1, 0])
            t = list(result[1])
            json.loads(t.pop(3))
            self.assertEqual(t, [12, 3, 2, 0])
            t = list(result[2])
            json.loads(t.pop(3))
            self.assertEqual(t, [13, 3, 3, 0])
            t = list(result[3])
            json.loads(t.pop(3))
            self.assertEqual(t, [14, 3, 4, 0])
            t = list(result[4])
            json.loads(t.pop(3))
            self.assertEqual(t, [15, 3, 5, 0])

            # files
            p1 = TransformSetSubDir.path(3)

            # transforms
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 11, 'jpg')).shape[:2],
                (200, 200))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 12, 'jpg')).shape[:2],
                (200, 200))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 13, 'jpg')).shape[:2],
                (200, 200))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 14, 'jpg')).shape[:2],
                (200, 200))  # noqa
            self.assertEqual(
                cv2.imread(TransformFile.path(p1, 15, 'jpg')).shape[:2],
                (200, 200))  # noqa
Exemple #30
0
    def test_list(self):
        with deepstar_path():
            FrameSetModel().insert(None)
            FrameModel().insert(1, 0)

            TransformSetModel().insert('test', 1)

            transform_model = TransformModel()
            transform_model.insert(1, 1, '{}', 0)
            transform_model.insert(1, 1, '{}', 1)
            transform_model.insert(1, 1, '{}', 0)

            result = transform_model.list(1)
            self.assertEqual(len(result), 3)
            self.assertEqual(result[0], (1, 1, 1, '{}', 0))
            self.assertEqual(result[1], (2, 1, 1, '{}', 1))
            self.assertEqual(result[2], (3, 1, 1, '{}', 0))

            result = transform_model.list(1, length=2)
            self.assertEqual(len(result), 2)
            self.assertEqual(result[0], (1, 1, 1, '{}', 0))
            self.assertEqual(result[1], (2, 1, 1, '{}', 1))

            result = transform_model.list(1, offset=1)
            self.assertEqual(len(result), 2)
            self.assertEqual(result[0], (2, 1, 1, '{}', 1))
            self.assertEqual(result[1], (3, 1, 1, '{}', 0))

            result = transform_model.list(1, length=1, offset=1)
            self.assertEqual(len(result), 1)
            self.assertEqual(result[0], (2, 1, 1, '{}', 1))

            result = transform_model.list(1, rejected=False)
            self.assertEqual(len(result), 2)
            self.assertEqual(result[0], (1, 1, 1, '{}', 0))
            self.assertEqual(result[1], (3, 1, 1, '{}', 0))