File size: 1,665 Bytes
8eeedfe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import os
from os import path
import glob
import numpy as np
from PIL import Image
from diffusers.utils import export_to_gif
import torch
ROOT = '/projects/katefgroup/datasets/gs_kubric/InpaintingFormat_Set50'
OUT_ROOT = os.path.join(ROOT, 'Consistent4D')
RESOLUTION = 512
imset = '2017/consistent4d.txt'
os.makedirs(OUT_ROOT, exist_ok=True)
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
mask_dir = path.join(ROOT, 'MergedAnnotations')
image_dir = path.join(ROOT, 'JPEGImages')
videos = []
num_frames = {}
vid_names = os.listdir(image_dir)
for _video in vid_names:
videos.append(_video)
num_frames[_video] = len(os.listdir(path.join(image_dir, _video)))
for index in range(len(videos)):
video = videos[index]
if video == 'swing-human-only':
continue
all_frames = []
img_paths = glob.glob(path.join(image_dir, video + '/*'))
img_paths.sort()
mask_paths = glob.glob(path.join(mask_dir, video, '001' + '/*'))
mask_paths.sort()
os.makedirs(os.path.join(OUT_ROOT, video), exist_ok=True)
for f in range(10):
# Load RGBs
img = Image.open(img_paths[f]).convert('RGBA')
img = img.resize((RESOLUTION, RESOLUTION))
mask = Image.open(mask_paths[f])
mask = mask.resize((RESOLUTION, RESOLUTION))
binary_mask = np.array(mask) > 128
binary_mask = np.tile(binary_mask[:, :, np.newaxis], (1,1,4))
masked_img = Image.fromarray(np.array(img) * binary_mask).convert('RGBA')
masked_img.save(os.path.join(OUT_ROOT, video, f'{f}.png')) |