def test_can_read_frames_randomly(self, fxt_sample_video): video = Video(fxt_sample_video) on_exit_do(video.close) for idx in {1, 3, 2, 0, 3}: frame = video[idx] assert frame.index == idx assert np.array_equal(frame.data, np.ones((*video.frame_size, 3)) * idx)
def test_can_skip_from_end(self, fxt_sample_video): video = Video(fxt_sample_video, end_frame=2) on_exit_do(video.close) last_frame = None for last_frame in video: pass assert 2 == video.length assert 1 == last_frame.index
def test_can_init_frame_count_lazily(self, fxt_sample_video): video = Video(fxt_sample_video) on_exit_do(video.close) assert None is video.length for idx, frame in enumerate(video): assert idx == frame.index assert 4 == video.length
def test_can_read_frames_sequentially(self, fxt_sample_video): video = Video(fxt_sample_video) on_exit_do(video.close) for idx, frame in enumerate(video): assert frame.size == video.frame_size assert frame.index == idx assert frame.video is video assert np.array_equal(frame.data, np.ones((*video.frame_size, 3)) * idx)
def test_can_split_video(self): on_exit_do(MediaManager.get_instance().clear) test_dir = scope_add(TestDir()) video_path = osp.join(test_dir, 'video.avi') make_sample_video(video_path, frames=10) output_dir = osp.join(test_dir, 'result') run(TestCase(), 'util', 'split_video', '-i', video_path, '-o', output_dir, '--image-ext', '.jpg', '--start-frame', '2', '--end-frame', '8', '--step', '2') assert set(os.listdir(output_dir)) == {'%06d.jpg' % n for n in range(2, 8, 2)}
def make_sample_video(path, frames=4, frame_size=(10, 20), fps=25.0): """ frame_size is (H, W), only even sides """ writer = cv2.VideoWriter(path, frameSize=tuple(frame_size[::-1]), fps=float(fps), fourcc=cv2.VideoWriter_fourcc(*'MJPG')) on_exit_do(writer.release) for i in range(frames): # Apparently, only uint8 values are supported, but not floats # Colors are compressed, but grayscale colors suffer no loss writer.write(np.ones((*frame_size, 3), dtype=np.uint8) * i)
def test_can_read_frames(self, fxt_sample_video): on_exit_do(MediaManager.get_instance().clear) expected = Dataset.from_iterable([ DatasetItem('frame_%03d' % i, subset='train', image=np.ones((4, 6, 3)) * i) for i in range(4) ]) actual = Dataset.import_from(fxt_sample_video, 'video_frames', subset='train', name_pattern='frame_%03d') compare_datasets(TestCase(), expected, actual)
def test_can_split_and_load(self, fxt_sample_video): test_dir = scope_add(TestDir()) on_exit_do(MediaManager.get_instance().clear) expected = Dataset.from_iterable([ DatasetItem('frame_%06d' % i, image=np.ones((4, 6, 3)) * i) for i in range(4) ]) dataset = Dataset.import_from(fxt_sample_video, 'video_frames', start_frame=0, end_frame=4, name_pattern='frame_%06d') dataset.export(format='image_dir', save_dir=test_dir, image_ext='.jpg') actual = Dataset.import_from(test_dir, 'image_dir') compare_datasets(TestCase(), expected, actual)
def test_can_skip_from_start(self, fxt_sample_video): video = Video(fxt_sample_video, start_frame=1) on_exit_do(video.close) assert 1 == next(iter(video)).index
def test_can_skip_frames_between(self, fxt_sample_video): video = Video(fxt_sample_video, step=2) on_exit_do(video.close) for idx, frame in enumerate(video): assert 2 * idx == frame.index
def test_can_read_video(self, fxt_sample_video): video = Video(fxt_sample_video) on_exit_do(video.close) assert None is video.length assert (4, 6) == video.frame_size
def foo(): on_error_do(error_cb) on_exit_do(exit_cb) raise TestException()