MCPcopy Create free account
hub / github.com/NVIDIA/DreamDojo / resize_input

Function resize_input

cosmos_predict2/_src/predict2/inference/utils.py:64–86  ·  view source on GitHub ↗

r""" Resizes and crops the input video tensor while preserving aspect ratio. The video is first resized so that the smaller dimension matches the target resolution, preserving the aspect ratio. Then, it's center-cropped to the target resolution. Args: video (torch.Tensor):

(video: torch.Tensor, resolution: tuple[int, int])

Source from the content-addressed store, hash-verified

62
63
64def resize_input(video: torch.Tensor, resolution: tuple[int, int]):
65 r"""
66 Resizes and crops the input video tensor while preserving aspect ratio.
67
68 The video is first resized so that the smaller dimension matches the target resolution,
69 preserving the aspect ratio. Then, it's center-cropped to the target resolution.
70
71 Args:
72 video (torch.Tensor): Input video tensor of shape (T, C, H, W).
73 resolution (list[int]): Target resolution [H, W].
74
75 Returns:
76 torch.Tensor: Resized and cropped video tensor of shape (T, C, target_H, target_W).
77 """
78
79 orig_h, orig_w = video.shape[2], video.shape[3]
80 target_h, target_w = resolution
81
82 scaling_ratio = max((target_w / orig_w), (target_h / orig_h))
83 resizing_shape = (int(math.ceil(scaling_ratio * orig_h)), int(math.ceil(scaling_ratio * orig_w)))
84 video_resized = F.resize(video, list(resizing_shape))
85 video_cropped = F.center_crop(video_resized, list(resolution))
86 return video_cropped
87
88
89def read_and_process_image(img_path: str, resolution: tuple[int, int], num_video_frames: int, resize: bool = True):

Callers 2

read_and_process_imageFunction · 0.70
read_and_process_videoFunction · 0.70

Calls 1

center_cropMethod · 0.80

Tested by

no test coverage detected