A list of video frames to be load asynchronously without blocking session start.
| 102 | |
| 103 | |
| 104 | class AsyncVideoFrameLoader: |
| 105 | """ |
| 106 | A list of video frames to be load asynchronously without blocking session start. |
| 107 | """ |
| 108 | |
| 109 | def __init__(self, img_paths, image_size, offload_video_to_cpu, img_mean, img_std): |
| 110 | self.img_paths = img_paths |
| 111 | self.image_size = image_size |
| 112 | self.offload_video_to_cpu = offload_video_to_cpu |
| 113 | self.img_mean = img_mean |
| 114 | self.img_std = img_std |
| 115 | # items in `self._images` will be loaded asynchronously |
| 116 | self.images = [None] * len(img_paths) |
| 117 | # catch and raise any exceptions in the async loading thread |
| 118 | self.exception = None |
| 119 | # video_height and video_width be filled when loading the first image |
| 120 | self.video_height = None |
| 121 | self.video_width = None |
| 122 | |
| 123 | # load the first frame to fill video_height and video_width and also |
| 124 | # to cache it (since it's most likely where the user will click) |
| 125 | self.__getitem__(0) |
| 126 | |
| 127 | # load the rest of frames asynchronously without blocking the session start |
| 128 | def _load_frames(): |
| 129 | try: |
| 130 | for n in tqdm(range(len(self.images)), desc="frame loading (JPEG)"): |
| 131 | self.__getitem__(n) |
| 132 | except Exception as e: |
| 133 | self.exception = e |
| 134 | |
| 135 | self.thread = Thread(target=_load_frames, daemon=True) |
| 136 | self.thread.start() |
| 137 | |
| 138 | def __getitem__(self, index): |
| 139 | if self.exception is not None: |
| 140 | raise RuntimeError("Failure in frame loading thread") from self.exception |
| 141 | |
| 142 | img = self.images[index] |
| 143 | if img is not None: |
| 144 | return img |
| 145 | |
| 146 | img, video_height, video_width = _load_img_as_tensor( |
| 147 | self.img_paths[index], self.image_size |
| 148 | ) |
| 149 | self.video_height = video_height |
| 150 | self.video_width = video_width |
| 151 | # normalize by mean and std |
| 152 | img -= self.img_mean |
| 153 | img /= self.img_std |
| 154 | if not self.offload_video_to_cpu: |
| 155 | img = img.cuda(non_blocking=True) |
| 156 | self.images[index] = img |
| 157 | return img |
| 158 | |
| 159 | def __len__(self): |
| 160 | return len(self.images) |
| 161 |