Download code/agt/recon/preprocess.py from MRBSTUDIO/AGT: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/MRBSTUDIO/AGT/resolve/main/code/agt/recon/preprocess.py
- Command line
-
hf download hf://MRBSTUDIO/AGT/code/agt/recon/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/MRBSTUDIO/AGT/resolve/main/code/agt/recon/preprocess.py
1.09 kB
| import numpy as np | |
| import torch | |
| class PilResize: | |
| def __init__(self, width: int = 518, multiple: int = 14): | |
| self.width, self.multiple = int(width), int(multiple) | |
| def size(self, w: int, h: int) -> tuple: | |
| return self.width, round(h * (self.width / w) / self.multiple) * self.multiple | |
| def __call__(self, frames_u8): | |
| from PIL import Image | |
| from torchvision import transforms as TF | |
| frames = np.asarray(frames_u8) | |
| if frames.dtype != np.uint8 or frames.ndim != 4 or frames.shape[-1] != 3: | |
| raise ValueError('Expected (N, H, W, 3) uint8 frames') | |
| to_tensor, out = TF.ToTensor(), [] | |
| for frame in frames: | |
| image = Image.fromarray(frame).convert('RGB') | |
| image = image.resize(self.size(*image.size), Image.Resampling.BICUBIC) | |
| tensor = to_tensor(image) | |
| if tensor.shape[1] > self.width: | |
| start = (tensor.shape[1] - self.width) // 2 | |
| tensor = tensor[:, start:start + self.width, :] | |
| out.append(tensor) | |
| return torch.stack(out) | |