我的解决方案有点不合常规,因为它是特定于库的。Mydia允许传递帧以提取,而不是强制
Videos
客户直接取样。这使我有机会预先计算要在父进程中采样的帧。通过这样做,我可以通过实例化一个新的
用那些框架。例如:
class MySampler:
def __init__(self, input_directory: Path, total_frames: int, num_frames: int, fps: int):
self.input_directory = Path(input_directory)
self.frames_per_video = [
self.__get_frame_numbers_for_each_video(total_frames, num_frames, fps)
for _ in self.input_directory.glob("*.mp4")
]
@staticmethod
def get_reader(num_frames: int, frames: List[int]):
# ignores the inputs and returns samples the frames that its constructed with
return Videos(target_size=(512, 512), num_frames=num_frames, mode=lambda *_: frames)
def sample_frames(self, number_of_workers: int):
pool = Pool(processes=number_of_workers)
videos = list(self.input_directory.glob("*.mp4"))
pool.starmap_async(self.read_video, zip(self.frames_per_video, videos))
pool.close()
pool.join()
哪里
read_video
是调用
get_reader
做阅读。