| 51 | |
| 52 | |
| 53 | def resize_for_rectangle_crop(arr, image_size, reshape_mode="random"): |
| 54 | # arr: T C H W |
| 55 | # image_size: H W |
| 56 | |
| 57 | if arr.shape[3] / arr.shape[2] > image_size[1] / image_size[0]: |
| 58 | arr = resize( |
| 59 | arr, |
| 60 | size=[image_size[0], int(arr.shape[3] * image_size[0] / arr.shape[2])], |
| 61 | interpolation=InterpolationMode.BICUBIC, |
| 62 | ) |
| 63 | else: |
| 64 | arr = resize( |
| 65 | arr, |
| 66 | size=[int(arr.shape[2] * image_size[1] / arr.shape[3]), image_size[1]], |
| 67 | interpolation=InterpolationMode.BICUBIC, |
| 68 | ) |
| 69 | |
| 70 | h, w = arr.shape[2], arr.shape[3] |
| 71 | arr = arr.squeeze(0) |
| 72 | |
| 73 | delta_h = h - image_size[0] |
| 74 | delta_w = w - image_size[1] |
| 75 | |
| 76 | if reshape_mode == "random" or reshape_mode == "none": |
| 77 | top = np.random.randint(0, delta_h + 1) |
| 78 | left = np.random.randint(0, delta_w + 1) |
| 79 | elif reshape_mode == "center": |
| 80 | top, left = delta_h // 2, delta_w // 2 |
| 81 | elif reshape_mode == "lower": |
| 82 | # Crop out the upper part |
| 83 | top, left = delta_h, delta_w // 2 |
| 84 | else: |
| 85 | raise NotImplementedError |
| 86 | arr = TT.functional.crop(arr, top=top, left=left, height=image_size[0], width=image_size[1]) |
| 87 | return arr |
| 88 | |
| 89 | |
| 90 | def pad_last_frame(tensor, num_frames): |