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_usage(self): with deepstar_path(): route_handler = VideoCommandLineRouteHandler() args = ['main.py', 'usage', 'videos'] opts = {} try: sys.stdout = StringIO() route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ self.assertTrue('Usage - Videos' in actual)
def test_insert_file_fails_to_open_a_video_file(self): with deepstar_path(): args = ['main.py', 'insert', 'videos', 'file', 'test'] opts = {} route_handler = VideoCommandLineRouteHandler() with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}): with self.assertRaises(CommandLineRouteHandlerError): try: route_handler.handle(args, opts) except CommandLineRouteHandlerError as e: self.assertEqual(e.message, 'File test not found') raise e
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 test_delete_one(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]) # files self.assertTrue(os.path.exists(FrameSetSubDir.path(1))) # db frame_set_model = FrameSetModel() self.assertIsNotNone(frame_set_model.select(1)) frame_model = FrameModel() self.assertIsNotNone(frame_model.select(1)) self.assertIsNotNone(frame_model.select(2)) self.assertIsNotNone(frame_model.select(3)) self.assertIsNotNone(frame_model.select(4)) self.assertIsNotNone(frame_model.select(5)) args = ['main.py', 'delete', 'frame_sets', '1'] 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, 'Frame set 1 was successfully deleted') # db self.assertIsNone(frame_set_model.select(1)) self.assertIsNone(frame_model.select(1)) self.assertIsNone(frame_model.select(2)) self.assertIsNone(frame_model.select(3)) self.assertIsNone(frame_model.select(4)) self.assertIsNone(frame_model.select(5)) # files self.assertFalse(os.path.exists(FrameSetSubDir.path(1)))
def test_select_extract_fails_to_open_a_video_file(self): with deepstar_path(): VideoModel().insert('test', 'test') args = ['main.py', 'select', 'videos', '1', 'extract'] opts = {} route_handler = VideoCommandLineRouteHandler() with self.assertRaises(CommandLineRouteHandlerError): try: route_handler.handle(args, opts) except CommandLineRouteHandlerError as e: self.assertEqual(e.message, f'OpenCV VideoCapture isOpened returned false for {VideoFile.path("test")}') # noqa raise e
def test_select_deploy_fails_to_get_a_plugin(self): with deepstar_path(): VideoModel().insert('test', 'test') args = ['main.py', 'select', 'videos', '1', 'deploy', 'test'] opts = {} route_handler = VideoCommandLineRouteHandler() with self.assertRaises(CommandLineRouteHandlerError): try: route_handler.handle(args, opts) except CommandLineRouteHandlerError as e: self.assertEqual(e.message, "'test' is not a valid frame set extraction plugin name") # noqa raise e
def test_select_extract_sub_sample(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') args = ['main.py', 'select', 'videos', '1', 'extract'] opts = {'sub-sample': '2'} route_handler = VideoCommandLineRouteHandler() try: sys.stdout = StringIO() route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout self.assertEqual(actual, 'frame_set_id=1, video_id=1') # db result = FrameModel().list(1) self.assertEqual(len(result), 3) self.assertEqual(result[0], (1, 1, 0)) self.assertEqual(result[1], (2, 1, 0)) self.assertEqual(result[2], (3, 1, 0)) # files p1 = FrameSetSubDir.path(1) # frames self.assertTrue(os.path.isfile(FrameFile.path(p1, 1, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 2, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 3, 'jpg'))) # thumbnails self.assertTrue(os.path.isfile(FrameFile.path(p1, 1, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 2, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 3, 'jpg', '192x192'))) # noqa
def test_select_extract_transform_set(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', 'transform_set'] # 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=transform_set, fk_frame_sets=1, fk_prev_transform_sets=None') # noqa # db result = TransformSetModel().select(1) self.assertEqual(result, (1, 'transform_set', 1, None)) result = TransformModel().list(1) self.assertEqual(len(result), 5) self.assertEqual(result[0], (1, 1, 1, None, 0)) self.assertEqual(result[1], (2, 1, 2, None, 0)) self.assertEqual(result[2], (3, 1, 3, None, 0)) self.assertEqual(result[3], (4, 1, 4, None, 0)) self.assertEqual(result[4], (5, 1, 5, None, 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_select_extract_fails_to_select_a_video(self): with deepstar_path(): args = ['main.py', 'select', 'videos', '1', 'extract'] opts = {} with self.assertRaises(CommandLineRouteHandlerError): try: VideoCommandLineRouteHandler().handle(args, opts) except CommandLineRouteHandlerError as e: self.assertEqual(e.message, 'Video with ID 00000001 not found') # noqa raise e
def test_insert_image_fails_to_open_image(self): with deepstar_path(): args = ['main.py', 'insert', 'videos', 'image', 'test'] opts = {} with self.assertRaises(CommandLineRouteHandlerError): try: VideoCommandLineRouteHandler().handle(args, opts) except CommandLineRouteHandlerError as e: self.assertEqual(e.message, 'An error occured while attempting to create one video file from one image file') # noqa raise e
def test_insert_image_description(self): with deepstar_path(): image_0001 = os.path.dirname(os.path.realpath(__file__)) + '/../../support/image_0001.jpg' # noqa args = ['main.py', 'insert', 'videos', 'image', image_0001] opts = {'description': 'test'} with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}): VideoCommandLineRouteHandler().handle(args, opts) # db self.assertEqual(VideoModel().select(1)[3], 'test')
def test_transform_set_select_extract_adjust_color_fails_due_to_option_value_format( self): # noqa 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 with self.assertRaises(ValueError): try: AdjustColorTransformSetSelectExtractPlugin( ).transform_set_select_extract(1, {'r': 'test'}) # noqa except ValueError as e: self.assertEqual(str( e ), 'A color adjustment option value must be formatted as +/- followed by a number (e.g. --r=+10)' ) # noqa raise e
def test_transform_set_select_extract_resize_fails_due_to_missing_required_option( self): # noqa 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 with self.assertRaises(ValueError): try: ResizeTransformSetSelectExtractPlugin( ).transform_set_select_extract(1, {}) # noqa except ValueError as e: self.assertEqual(str( e ), 'The height or width options are required but were not supplied' ) # noqa raise e
def test_list(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]) self.mock_transform_set() args = ['main.py', 'list', 'transform_sets', '1', 'transforms'] opts = {} route_handler = TransformCommandLineRouteHandler() try: sys.stdout = StringIO() with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}): route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout expected = \ '5 results\n' \ 'id | fk_transform_sets | fk_frames | metadata | rejected | ' \ '(width | height)\n' \ '-----------------------------------------------------------' \ '----------------\n' \ '1 | 1 | 1 | {} | 0 | (309 | 404)\n' \ '2 | 1 | 2 | {} | 0 | (309 | 404)\n' \ '3 | 1 | 3 | {} | 0 | (309 | 404)\n' \ '4 | 1 | 4 | {} | 0 | (309 | 404)\n' \ '5 | 1 | 5 | {} | 0 | (309 | 404)' self.assertEqual(actual, expected)
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 setUp_(self): 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]) self.mock_transform_set()
def test_select_export_dir_one_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]) FrameModel().update(5, rejected=1) tmpdir = os.environ['DEEPSTAR_PATH'] + '/test' os.mkdir(tmpdir) args = ['main.py', 'select', 'frame_sets', '1', 'export', 'dir', tmpdir] # noqa opts = {} route_handler = FrameSetCommandLineRouteHandler() try: sys.stdout = StringIO() with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}): route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout self.assertEqual(actual, f'4 frames were successfully exported to {tmpdir}') # noqa # frames self.assertTrue(os.path.isfile(FrameFile.path(tmpdir, 1, 'jpg'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(tmpdir, 2, 'jpg'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(tmpdir, 3, 'jpg'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(tmpdir, 4, 'jpg'))) # noqa
def test_transform_set_select_merge_fade_fails_due_to_transform_set_count_less_than_frame_count(self): # noqa 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', {}) with self.assertRaises(ValueError): try: FadeTransformSetSelectMergePlugin().transform_set_select_merge([1, 2], {'frame-count': '6'}) # noqa except ValueError as e: self.assertEqual(str(e), 'Both transform sets must be greater than frame count') # noqa raise e
def test_transform_set_select_merge_fade_fails_due_to_missing_required_option(self): # noqa 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', {}) with self.assertRaises(ValueError): try: FadeTransformSetSelectMergePlugin().transform_set_select_merge([1, 2], {}) # noqa except ValueError as e: self.assertEqual(str(e), 'The frame-count option is required but was not supplied') # noqa raise e
def test_transform_set_select_merge_fade_fails_due_to_frame_counts_not_equal( self): # noqa 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 video_0003 = os.path.dirname(os.path.realpath( __file__)) + '/../../support/video_0003.mp4' # noqa route_handler.insert_file(video_0001) route_handler.insert_file(video_0003) route_handler.select_extract([1]) route_handler.select_extract([2]) route_handler = FrameSetCommandLineRouteHandler() route_handler.select_extract([1], 'transform_set', {}) route_handler.select_extract([2], 'transform_set', {}) with self.assertRaises(ValueError): try: OverlayTransformSetSelectMergePlugin( ).transform_set_select_merge([1, 2], { 'x1': '0', 'y1': '0' }) # noqa except ValueError as e: self.assertEqual(str( e ), 'Both transform sets must have the same number of non-rejected transforms (be the same length)' ) # noqa raise e
def test_list(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', 'list', 'frame_sets', '1', 'frames'] opts = {} route_handler = FrameCommandLineRouteHandler() try: sys.stdout = StringIO() with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}): route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout expected = textwrap.dedent(''' 5 results id | fk_frame_sets | rejected | (width | height) ------------------------------------------------ 1 | 1 | 0 | (1280 | 720) 2 | 1 | 0 | (1280 | 720) 3 | 1 | 0 | (1280 | 720) 4 | 1 | 0 | (1280 | 720) 5 | 1 | 0 | (1280 | 720)''').strip() self.assertEqual(actual, expected)
def test_insert_image_frame_count(self): with deepstar_path(): image_0001 = os.path.dirname(os.path.realpath(__file__)) + '/../../support/image_0001.jpg' # noqa args = ['main.py', 'insert', 'videos', 'image', image_0001] opts = {'frame-count': '5'} with mock.patch.dict(os.environ, {'DEBUG_LEVEL': '0'}): VideoCommandLineRouteHandler().handle(args, opts) # files video_path = VideoFile.path(VideoModel().select(1)[2]) vc = cv2.VideoCapture(video_path) try: self.assertTrue(vc.isOpened()) self.assertEqual(vc.get(cv2.CAP_PROP_FRAME_COUNT), 5) finally: vc.release()
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 test_select_extract_fails_to_get_a_plugin(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', 'test'] opts = {} with self.assertRaises(CommandLineRouteHandlerError): try: FrameSetCommandLineRouteHandler().handle(args, opts) except CommandLineRouteHandlerError as e: self.assertEqual(e.message, "'test' is not a valid frame set extraction plugin name") # noqa raise e
def test_transform_set_select_extract_slice(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 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), 3) self.assertEqual(result[0], (6, 2, 2, None, 0)) self.assertEqual(result[1], (7, 2, 3, None, 0)) self.assertEqual(result[2], (8, 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')))
def test_select_merge_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.insert_file(video_0001) route_handler.insert_file(video_0001) route_handler.select_extract([1, 2, 3]) FrameModel().update(15, rejected=1) args = ['main.py', 'select', 'frame_sets', '1-2,3', 'merge'] opts = {} route_handler = FrameSetCommandLineRouteHandler() try: sys.stdout = StringIO() with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '4'}): route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout self.assertEqual(actual, 'frame_set_id=4, fk_videos=None') # db result = FrameSetModel().select(4) self.assertEqual(result, (4, None)) result = FrameModel().list(4) self.assertEqual(len(result), 14) self.assertEqual(result[0], (16, 4, 0)) self.assertEqual(result[1], (17, 4, 0)) self.assertEqual(result[2], (18, 4, 0)) self.assertEqual(result[3], (19, 4, 0)) self.assertEqual(result[4], (20, 4, 0)) self.assertEqual(result[5], (21, 4, 0)) self.assertEqual(result[6], (22, 4, 0)) self.assertEqual(result[7], (23, 4, 0)) self.assertEqual(result[8], (24, 4, 0)) self.assertEqual(result[9], (25, 4, 0)) self.assertEqual(result[10], (26, 4, 0)) self.assertEqual(result[11], (27, 4, 0)) self.assertEqual(result[12], (28, 4, 0)) self.assertEqual(result[13], (29, 4, 0)) # files p1 = FrameSetSubDir.path(4) # frames self.assertTrue(os.path.isfile(FrameFile.path(p1, 16, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 17, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 18, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 19, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 20, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 21, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 22, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 23, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 24, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 25, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 26, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 27, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 28, 'jpg'))) self.assertTrue(os.path.isfile(FrameFile.path(p1, 29, 'jpg'))) # thumbnails self.assertTrue(os.path.isfile(FrameFile.path(p1, 16, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 17, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 18, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 19, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 20, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 21, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 22, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 23, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 24, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 25, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 26, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 27, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 28, 'jpg', '192x192'))) # noqa self.assertTrue(os.path.isfile(FrameFile.path(p1, 29, 'jpg', '192x192'))) # noqa
def test_select_clone_many(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.insert_file(video_0001) route_handler.insert_file(video_0001) route_handler.select_extract([1, 2, 3]) args = ['main.py', 'select', 'frame_sets', '1-2,3', 'clone'] opts = {} route_handler = FrameSetCommandLineRouteHandler() try: sys.stdout = StringIO() with mock.patch.dict(os.environ, {'MODEL_LIST_LENGTH': '2'}): route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout expected = textwrap.dedent(''' frame_set_id=4, fk_videos=1 frame_set_id=5, fk_videos=2 frame_set_id=6, fk_videos=3''').strip() self.assertEqual(actual, expected)
def test_delete_many(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.insert_file(video_0001) route_handler.insert_file(video_0001) route_handler.select_extract([1, 2, 3]) args = ['main.py', 'delete', 'frame_sets', '1-2,3'] opts = {} route_handler = FrameSetCommandLineRouteHandler() try: sys.stdout = StringIO() route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout expected = textwrap.dedent(''' Frame set 1 was successfully deleted Frame set 2 was successfully deleted Frame set 3 was successfully deleted''').strip() self.assertEqual(actual, expected)
def test_select_extract_many(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.insert_file(video_0001) route_handler.insert_file(video_0001) route_handler.select_extract([1, 2, 3]) class TestPlugin: def frame_set_select_extract(self, frame_set_id, opts): return TransformSetModel().insert('test', frame_set_id, None) # noqa args = ['main.py', 'select', 'frame_sets', '1-2,3', 'extract', 'test'] # noqa opts = {} route_handler = FrameSetCommandLineRouteHandler() try: sys.stdout = StringIO() with mock.patch.dict(Plugin._map, {'frame_set_select_extract': {'test': TestPlugin}}): # noqa route_handler.handle(args, opts) actual = sys.stdout.getvalue().strip() finally: sys.stdout = sys.__stdout__ # stdout expected = 'transform_set_id=1, name=test, fk_frame_sets=1, ' \ 'fk_prev_transform_sets=None\n' \ 'transform_set_id=2, name=test, fk_frame_sets=2, ' \ 'fk_prev_transform_sets=None\n' \ 'transform_set_id=3, name=test, fk_frame_sets=3, ' \ 'fk_prev_transform_sets=None' self.assertEqual(actual, expected)