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
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)
def test_transform_set_select_extract_max_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() 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), 5) 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, 2, 0]) t = list(result[2]) json.loads(t.pop(3)) self.assertEqual(t, [8, 2, 3, 0]) t = list(result[3]) json.loads(t.pop(3)) self.assertEqual(t, [9, 2, 4, 0]) t = list(result[4]) json.loads(t.pop(3)) self.assertEqual(t, [10, 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 self.assertEqual( cv2.imread(TransformFile.path(p1, 9, 'jpg')).shape[0], 299) # noqa self.assertEqual( cv2.imread(TransformFile.path(p1, 10, 'jpg')).shape[0], 299) # noqa
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)
def mock_transform_set(self): image_0001 = os.path.dirname(os.path.realpath( __file__)) + '/../../support/image_0001.jpg' # noqa image_0003 = os.path.dirname(os.path.realpath( __file__)) + '/../../support/image_0003.jpg' # 200-blurry # noqa image_0004 = os.path.dirname(os.path.realpath( __file__)) + '/../../support/image_0004.jpg' # 200-sharp # noqa image_0005 = os.path.dirname(os.path.realpath( __file__)) + '/../../support/image_0005.jpg' # 650-blurry # noqa image_0006 = os.path.dirname(os.path.realpath( __file__)) + '/../../support/image_0006.jpg' # 650-sharp # noqa TransformSetModel().insert('face', 1, None) p1 = TransformSetSubDir.path(1) os.mkdir(p1) transform_model = TransformModel() transform_model.insert(1, 1, '{}', 0) shutil.copy(image_0001, TransformFile.path(p1, 1, 'jpg')) transform_model.insert(1, 2, '{}', 0) shutil.copy(image_0003, TransformFile.path(p1, 2, 'jpg')) transform_model.insert(1, 3, '{}', 0) shutil.copy(image_0004, TransformFile.path(p1, 3, 'jpg')) transform_model.insert(1, 4, '{}', 0) shutil.copy(image_0005, TransformFile.path(p1, 4, 'jpg')) transform_model.insert(1, 5, '{}', 0) shutil.copy(image_0006, TransformFile.path(p1, 5, 'jpg'))
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 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')))
def test_transform_set_select_merge_overlay_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', {}) route_handler.select_extract([1], 'transform_set', {}) transform_model = TransformModel() transform_model.update(1, rejected=1) transform_model.update(10, rejected=1) 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), 4) 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)) # 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
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 test_transform_set_select_extract_crop(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 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), 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], (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 self.assertEqual( cv2.imread(TransformFile.path(p1, 10, 'jpg')).shape[:2], (50, 50)) # noqa
def test_select(self): with deepstar_path(): VideoModel().insert('test1', 'test2') FrameSetModel().insert(1) transform_set_model = TransformSetModel() transform_set_model.insert('test', 1) result = transform_set_model.select(1) self.assertEqual(result, (1, 'test', 1, None))
def test_transform_set_select_extract_mouth(self): with deepstar_path(): self.mock_transform_set() tid = MouthTransformSetSelectExtractPlugin( ).transform_set_select_extract(1, {}) # noqa self.assertEqual(tid, 2) result = TransformSetModel().select(2) self.assertEqual(result, (2, 'mouth', 1, 1))
def test_transform_set_select_merge_overlay_image(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', {}) 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), 5) 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)) self.assertEqual(result[4], (10, 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 self.assertIsInstance(cv2.imread(TransformFile.path(p1, 10, 'jpg')), np.ndarray) # noqa
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_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 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
def list(self): """ This method lists transform sets in the transform set collection. :rtype: None """ result = TransformSetModel().list() debug(f'{len(result)} results', 3) debug('id | name | fk_frame_sets | fk_prev_transform_sets', 3) debug('--------------------------------------------------', 3) for r in result: debug(f'{r[0]} | {r[1]} | {r[2]} | {r[3]}', 3)
def test_list(self): with deepstar_path(): VideoModel().insert('test1', 'test2') FrameSetModel().insert(1) transform_set_model = TransformSetModel() transform_set_model.insert('test', 1) transform_set_model.insert('test', 1) transform_set_model.insert('test', 1) result = transform_set_model.list() self.assertEqual(len(result), 3) self.assertEqual(result[0], (1, 'test', 1, None)) self.assertEqual(result[1], (2, 'test', 1, None)) self.assertEqual(result[2], (3, 'test', 1, None))
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 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)
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
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)
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
def test_transform_set_select_merge_fade(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', {}) FadeTransformSetSelectMergePlugin().transform_set_select_merge([1, 2], {'frame-count': '2'}) # noqa # db result = TransformSetModel().select(3) self.assertEqual(result, (3, 'fade', None, None)) result = TransformModel().list(3) self.assertEqual(len(result), 8) self.assertEqual(result[0], (11, 3, 1, None, 0)) self.assertEqual(result[1], (12, 3, 2, None, 0)) self.assertEqual(result[2], (13, 3, 3, None, 0)) self.assertEqual(result[3], (14, 3, None, None, 0)) self.assertEqual(result[4], (15, 3, None, None, 0)) self.assertEqual(result[5], (16, 3, 3, None, 0)) self.assertEqual(result[6], (17, 3, 4, None, 0)) self.assertEqual(result[7], (18, 3, 5, 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 self.assertIsInstance(cv2.imread(TransformFile.path(p1, 16, 'jpg')), np.ndarray) # noqa self.assertIsInstance(cv2.imread(TransformFile.path(p1, 17, 'jpg')), np.ndarray) # noqa self.assertIsInstance(cv2.imread(TransformFile.path(p1, 18, 'jpg')), np.ndarray) # noqa
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 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))
def test_transform_set_select_extract_adjust_color_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) 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), 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.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')))
def frame_set_select_extract(self, frame_set_id, opts): """ This method extracts faces from each frame in a frame set. :param int frame_set_id: The frame set ID. :param dict opts: The dict of opts. :rtype: int """ detector = MTCNN() offset_percent = 0.2 min_confidence = 0.9 debug_ = True if 'debug' in opts else False frame_set_path = FrameSetSubDir.path(frame_set_id) transform_set_id = TransformSetModel().insert(self.name, frame_set_id) transform_set_path = TransformSetSubDir.path(transform_set_id) os.makedirs(transform_set_path) length = int(os.environ.get('MODEL_LIST_LENGTH', '100')) offset = 0 while True: result = FrameModel().list(frame_set_id, length=length, offset=offset, rejected=False) if not result: break for frame_id, _, rejected in result: self._extract_faces(frame_set_path, frame_id, transform_set_path, transform_set_id, detector, offset_percent, min_confidence, debug_) offset += length return transform_set_id
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 list(self, transform_set_id, length=-1, offset=None, rejected=True): """ This method performs a list operation. :param int transform_set_id: The transform set ID. :param int length: The optional length. :param int offset: The optional offset. :param bool rejected: True if should include rejected transforms else False if should not. The default value is True. :rtype: tuple """ result = TransformSetModel().select(transform_set_id) if result is None: return None params = (transform_set_id, ) query = """ SELECT id, fk_transform_sets, fk_frames, metadata, rejected FROM transforms WHERE fk_transform_sets = ? """ if rejected is False: query += ' AND rejected = 0' if length is not None: query += ' LIMIT ?' params += (length, ) if offset is not None: query += ' OFFSET ?' params += (offset, ) result = Model.execute(query, params) return result.fetchall()