|
|
|
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):
|
|
|
|
|
|
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')) |