Spaces:
Runtime error
Runtime error
File size: 9,920 Bytes
37ee4a4 |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
import numpy as np
import cv2
from basicsr.utils import img2tensor
import torch
import torch.nn.functional as F
def resize_numpy_image(image, max_resolution=768 * 768, resize_short_edge=None):
h, w = image.shape[:2]
w_org = image.shape[1]
if resize_short_edge is not None:
k = resize_short_edge / min(h, w)
else:
k = max_resolution / (h * w)
k = k**0.5
h = int(np.round(h * k / 64)) * 64
w = int(np.round(w * k / 64)) * 64
image = cv2.resize(image, (w, h), interpolation=cv2.INTER_LANCZOS4)
scale = w/w_org
return image, scale
def split_ldm(ldm):
x = []
y = []
for p in ldm:
x.append(p[0])
y.append(p[1])
return x,y
def process_move(path_mask, h, w, dx, dy, scale, input_scale, resize_scale, up_scale, up_ft_index, w_edit, w_content, w_contrast, w_inpaint, precision, path_mask_ref=None):
dx, dy = dx*input_scale, dy*input_scale
if isinstance(path_mask, str):
mask_x0 = cv2.imread(path_mask)
else:
mask_x0 = path_mask
mask_x0 = cv2.resize(mask_x0, (h, w))
if path_mask_ref is not None:
if isinstance(path_mask_ref, str):
mask_x0_ref = cv2.imread(path_mask_ref)
else:
mask_x0_ref = path_mask_ref
mask_x0_ref = cv2.resize(mask_x0_ref, (h, w))
else:
mask_x0_ref=None
mask_x0 = img2tensor(mask_x0)[0]
mask_x0 = (mask_x0>0.5).float().to('cuda', dtype=precision)
if mask_x0_ref is not None:
mask_x0_ref = img2tensor(mask_x0_ref)[0]
mask_x0_ref = (mask_x0_ref>0.5).float().to('cuda', dtype=precision)
mask_org = F.interpolate(mask_x0[None,None], (int(mask_x0.shape[-2]//scale), int(mask_x0.shape[-1]//scale)))>0.5
mask_tar = F.interpolate(mask_x0[None,None], (int(mask_x0.shape[-2]//scale*resize_scale), int(mask_x0.shape[-1]//scale*resize_scale)))>0.5
mask_cur = torch.roll(mask_tar, (int(dy//scale*resize_scale), int(dx//scale*resize_scale)), (-2,-1))
pad_size_x = abs(mask_tar.shape[-1]-mask_org.shape[-1])//2
pad_size_y = abs(mask_tar.shape[-2]-mask_org.shape[-2])//2
if resize_scale>1:
sum_before = torch.sum(mask_cur)
mask_cur = mask_cur[:,:,pad_size_y:pad_size_y+mask_org.shape[-2],pad_size_x:pad_size_x+mask_org.shape[-1]]
sum_after = torch.sum(mask_cur)
if sum_after != sum_before:
raise ValueError('Resize out of bounds, exiting.')
else:
temp = torch.zeros(1,1,mask_org.shape[-2], mask_org.shape[-1]).to(mask_org.device)
temp[:,:,pad_size_y:pad_size_y+mask_cur.shape[-2],pad_size_x:pad_size_x+mask_cur.shape[-1]]=mask_cur
mask_cur =temp>0.5
mask_other = (1-((mask_cur+mask_org)>0.5).float())>0.5
mask_overlap = ((mask_cur.float()+mask_org.float())>1.5).float()
mask_non_overlap = (mask_org.float()-mask_overlap)>0.5
return {
"mask_x0":mask_x0,
"mask_x0_ref":mask_x0_ref,
"mask_tar":mask_tar,
"mask_cur":mask_cur,
"mask_other":mask_other,
"mask_overlap":mask_overlap,
"mask_non_overlap":mask_non_overlap,
"up_scale":up_scale,
"up_ft_index":up_ft_index,
"resize_scale":resize_scale,
"w_edit":w_edit,
"w_content":w_content,
"w_contrast":w_contrast,
"w_inpaint":w_inpaint,
}
def process_drag_face(h, w, x, y, x_cur, y_cur, scale, input_scale, up_scale, up_ft_index, w_edit, w_inpaint, precision):
for i in range(len(x)):
x[i] = int(x[i]*input_scale)
y[i] = int(y[i]*input_scale)
x_cur[i] = int(x_cur[i]*input_scale)
y_cur[i] = int(y_cur[i]*input_scale)
mask_tar = []
for p_idx in range(len(x)):
mask_i = torch.zeros(int(h//scale), int(w//scale)).cuda()
y_clip = int(np.clip(y[p_idx]//scale, 1, mask_i.shape[0]-2))
x_clip = int(np.clip(x[p_idx]//scale, 1, mask_i.shape[1]-2))
mask_i[y_clip-1:y_clip+2,x_clip-1:x_clip+2]=1
mask_i = mask_i>0.5
mask_tar.append(mask_i)
mask_cur = []
for p_idx in range(len(x_cur)):
mask_i = torch.zeros(int(h//scale), int(w//scale)).cuda()
y_clip = int(np.clip(y_cur[p_idx]//scale, 1, mask_i.shape[0]-2))
x_clip = int(np.clip(x_cur[p_idx]//scale, 1, mask_i.shape[1]-2))
mask_i[y_clip-1:y_clip+2,x_clip-1:x_clip+2]=1
mask_i=mask_i>0.5
mask_cur.append(mask_i)
return {
"mask_tar":mask_tar,
"mask_cur":mask_cur,
"up_scale":up_scale,
"up_ft_index":up_ft_index,
"w_edit": w_edit,
"w_inpaint": w_inpaint,
}
def process_drag(path_mask, h, w, x, y, x_cur, y_cur, scale, input_scale, up_scale, up_ft_index, w_edit, w_inpaint, w_content, precision, latent_in):
if isinstance(path_mask, str):
mask_x0 = cv2.imread(path_mask)
else:
mask_x0 = path_mask
mask_x0 = cv2.resize(mask_x0, (h, w))
mask_x0 = img2tensor(mask_x0)[0]
dict_mask = {}
dict_mask['base'] = mask_x0
mask_x0 = (mask_x0>0.5).float().to('cuda', dtype=precision)
mask_other = F.interpolate(mask_x0[None,None], (int(mask_x0.shape[-2]//scale), int(mask_x0.shape[-1]//scale)))<0.5
mask_tar = []
mask_cur = []
for p_idx in range(len(x)):
mask_tar_i = torch.zeros(int(mask_x0.shape[-2]//scale), int(mask_x0.shape[-1]//scale)).to('cuda', dtype=precision)
mask_cur_i = torch.zeros(int(mask_x0.shape[-2]//scale), int(mask_x0.shape[-1]//scale)).to('cuda', dtype=precision)
y_tar_clip = int(np.clip(y[p_idx]//scale, 1, mask_tar_i.shape[0]-2))
x_tar_clip = int(np.clip(x[p_idx]//scale, 1, mask_tar_i.shape[0]-2))
y_cur_clip = int(np.clip(y_cur[p_idx]//scale, 1, mask_cur_i.shape[0]-2))
x_cur_clip = int(np.clip(x_cur[p_idx]//scale, 1, mask_cur_i.shape[0]-2))
mask_tar_i[y_tar_clip-1:y_tar_clip+2,x_tar_clip-1:x_tar_clip+2]=1
mask_cur_i[y_cur_clip-1:y_cur_clip+2,x_cur_clip-1:x_cur_clip+2]=1
mask_tar_i = mask_tar_i>0.5
mask_cur_i=mask_cur_i>0.5
mask_tar.append(mask_tar_i)
mask_cur.append(mask_cur_i)
latent_in[:,:,y_cur_clip//up_scale-1:y_cur_clip//up_scale+2, x_cur_clip//up_scale-1:x_cur_clip//up_scale+2] = latent_in[:,:, y_tar_clip//up_scale-1:y_tar_clip//up_scale+2, x_tar_clip//up_scale-1:x_tar_clip//up_scale+2]
return {
"dict_mask":dict_mask,
"mask_x0":mask_x0,
"mask_tar":mask_tar,
"mask_cur":mask_cur,
"mask_other":mask_other,
"up_scale":up_scale,
"up_ft_index":up_ft_index,
"w_edit": w_edit,
"w_inpaint": w_inpaint,
"w_content": w_content,
"latent_in":latent_in,
}
def process_appearance(path_mask, path_mask_replace, h, w, scale, input_scale, up_scale, up_ft_index, w_edit, w_content, precision):
if isinstance(path_mask, str):
mask_base = cv2.imread(path_mask)
else:
mask_base = path_mask
mask_base = cv2.resize(mask_base, (h, w))
if isinstance(path_mask_replace, str):
mask_replace = cv2.imread(path_mask_replace)
else:
mask_replace = path_mask_replace
mask_replace = cv2.resize(mask_replace, (h, w))
dict_mask = {}
mask_base = img2tensor(mask_base)[0]
dict_mask['base'] = mask_base
mask_base = (mask_base>0.5).to('cuda', dtype=precision)
mask_replace = img2tensor(mask_replace)[0]
dict_mask['replace'] = mask_replace
mask_replace = (mask_replace>0.5).to('cuda', dtype=precision)
mask_base_cur = F.interpolate(mask_base[None,None], (int(mask_base.shape[-2]//scale), int(mask_base.shape[-1]//scale)))>0.5
mask_replace_cur = F.interpolate(mask_replace[None,None], (int(mask_replace.shape[-2]//scale), int(mask_replace.shape[-1]//scale)))>0.5
return {
"dict_mask":dict_mask,
"mask_base_cur":mask_base_cur,
"mask_replace_cur":mask_replace_cur,
"up_scale":up_scale,
"up_ft_index":up_ft_index,
"w_edit":w_edit,
"w_content":w_content,
}
def process_paste(path_mask, h, w, dx, dy, scale, input_scale, up_scale, up_ft_index, w_edit, w_content, precision, resize_scale=None):
dx, dy = dx*input_scale, dy*input_scale
if isinstance(path_mask, str):
mask_base = cv2.imread(path_mask)
else:
mask_base = path_mask
mask_base = cv2.resize(mask_base, (h, w))
dict_mask = {}
mask_base = img2tensor(mask_base)[0][None, None]
mask_base = (mask_base>0.5).to('cuda', dtype=precision)
if resize_scale is not None and resize_scale!=1:
hi, wi = mask_base.shape[-2], mask_base.shape[-1]
mask_base = F.interpolate(mask_base, (int(hi*resize_scale), int(wi*resize_scale)))
pad_size_x = np.abs(mask_base.shape[-1]-wi)//2
pad_size_y = np.abs(mask_base.shape[-2]-hi)//2
if resize_scale>1:
mask_base = mask_base[:,:,pad_size_y:pad_size_y+hi,pad_size_x:pad_size_x+wi]
else:
temp = torch.zeros(1,1,hi, wi).to(mask_base.device)
temp[:,:,pad_size_y:pad_size_y+mask_base.shape[-2],pad_size_x:pad_size_x+mask_base.shape[-1]]=mask_base
mask_base = temp
mask_replace = mask_base.clone()
mask_base = torch.roll(mask_base, (int(dy), int(dx)), (-2,-1))
dict_mask['base'] = mask_base[0,0]
dict_mask['replace'] = mask_replace[0,0]
mask_replace = (mask_replace>0.5).to('cuda', dtype=precision)
mask_base_cur = F.interpolate(mask_base, (int(mask_base.shape[-2]//scale), int(mask_base.shape[-1]//scale)))>0.5
mask_replace_cur = torch.roll(mask_base_cur, (-int(dy/scale), -int(dx/scale)), (-2,-1))
return {
"dict_mask":dict_mask,
"mask_base_cur":mask_base_cur,
"mask_replace_cur":mask_replace_cur,
"up_scale":up_scale,
"up_ft_index":up_ft_index,
"w_edit":w_edit,
"w_content":w_content,
"w_edit":w_edit,
"w_content":w_content,
} |