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