| 35 | |
| 36 | |
| 37 | def extract_clips(video, target_frames, video_clip_mode): |
| 38 | # video is (channels, num_frames, height, width) |
| 39 | frames = video.shape[1] |
| 40 | if frames < target_frames: |
| 41 | # TODO: think about how to handle this case. Maybe the video should have already been thrown out? |
| 42 | print(f'video with shape {video.shape} is being skipped because it has less ({frames}) than the target_frames {target_frames}') |
| 43 | return [] |
| 44 | |
| 45 | if video_clip_mode == 'single_beginning': |
| 46 | return [video[:, :target_frames, ...]] |
| 47 | elif video_clip_mode == 'single_middle': |
| 48 | start = int((frames - target_frames) / 2) |
| 49 | assert frames-start >= target_frames |
| 50 | return [video[:, start:start+target_frames, ...]] |
| 51 | # elif video_clip_mode == 'multiple_overlapping': |
| 52 | # # Extract multiple clips so we use the whole video for training. |
| 53 | # # The clips might overlap a little bit. We never cut anything off the end of the video. |
| 54 | # num_clips = ((frames - 1) // target_frames) + 1 |
| 55 | # start_indices = torch.linspace(0, frames-target_frames, num_clips).int() |
| 56 | # return [video[:, i:i+target_frames, ...] for i in start_indices] |
| 57 | else: |
| 58 | raise NotImplementedError(f'video_clip_mode={video_clip_mode} is not recognized') |
| 59 | |
| 60 | |
| 61 | def convert_crop_and_resize(pil_img, width_and_height): |