def update_video_loader(self, file_path=None, device=None, backend_factory=None, backend_opts=None): if backend_factory is not None: self.backend_factory = backend_factory if device is not None: self.device = device if file_path is not None: self.file_path = file_path if backend_opts is not None: self.backend_opts = backend_opts if not self.file_path: return try: if self.backend_factory is not None: tvl.set_backend_factory(self.device.type, self.backend_factory) vl = tvl.VideoLoader(self.file_path, self.device, backend_opts=self.backend_opts) self.video_thread.set_video_loader(vl) self.spn_frame_index.configure(to=vl.n_frames - 1) self.title(self.file_path.name) except Exception: print(f'Failed to load video: {self.file_path:s}') traceback.print_exc()
def read_sequential(video_file, device, **backend_opts): n_frames = 40 n_trials = 1 vl = tvl.VideoLoader(video_file, device, torch.float32, backend_opts) # Read one frame to get any initialisation out of the way. vl.read_frame() t1 = time.time() for _ in range(n_trials): frames = list(vl.read_frames(n_frames)) assert len(frames) == n_frames t2 = time.time() return (n_trials * n_frames) / (t2 - t1)
def read_random(video_file, device, **backend_opts): n_frames = 5 n_trials = 8 vl = tvl.VideoLoader(video_file, device, torch.float32, backend_opts) # Read one frame to get any initialisation out of the way. vl.read_frame() vl.seek(0) t1 = time.time() for _ in range(n_trials): frames = list(vl.select_frames(np.arange(n_frames) * 10)) assert len(frames) == n_frames t2 = time.time() return (n_trials * n_frames) / (t2 - t1)
def test_vl_read_frame(dummy_backend): vl = tvl.VideoLoader('', dummy_backend.device) assert vl.read_frame() == dummy_backend.frames[0]
def test_vl_select_frames_mixed(dummy_backend, mocker): vl = tvl.VideoLoader('', dummy_backend.device) mocked_seek = mocker.patch.object(vl, 'seek_to_frame') list(vl.select_frames([1, 2, 10, 12])) assert mocked_seek.mock_calls == [call(1), call(10)]
def test_vl_select_frames_random(dummy_backend, mocker): vl = tvl.VideoLoader('', dummy_backend.device) mocked_seek = mocker.patch.object(vl, 'seek_to_frame') list(vl.select_frames([5, 45, 25])) # When frame indices are sparse, multiple seeks should occur assert mocked_seek.mock_calls == [call(5), call(25), call(45)]
def test_vl_select_frames_sequental(dummy_backend, mocker): vl = tvl.VideoLoader('', dummy_backend.device) mocked_seek = mocker.patch.object(vl, 'seek_to_frame') list(vl.select_frames([24, 26, 25])) # When frame indices are dense, only one seek should occur mocked_seek.assert_called_once_with(24)
def test_vl_remaining_frames(dummy_backend): vl = tvl.VideoLoader('', dummy_backend.device) vl.read_frame() assert list(vl.remaining_frames()) == dummy_backend.frames[1:]
def test_vl_read_all_frames(dummy_backend): vl = tvl.VideoLoader('', dummy_backend.device) assert list(vl.read_all_frames()) == dummy_backend.frames
def test_vl_read_frame(dummy_backend_factory_cpu): vl = tvl.VideoLoader('', dummy_backend_factory_cpu.device) assert vl.read_frame() == dummy_backend_factory_cpu.frames[0]