-
Notifications
You must be signed in to change notification settings - Fork 9
/
Crop_and_mask.py
304 lines (275 loc) · 9.73 KB
/
Crop_and_mask.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from src.utils.alignmengt import crop_faces, calc_alignment_coefficients, crop_faces_from_image
from PIL import Image
import argparse, os, sys, glob
import cv2
import torch
import numpy as np
from pretrained.face_parsing.face_parsing_demo import init_faceParsing_pretrained_model, faceParsing_demo, vis_parsing_maps
from tqdm import tqdm
def crop_and_align_face(target_files):
image_size = 1024
scale = 1.0
center_sigma = 0
xy_sigma = 0
use_fa = False
print('Aligning images')
crops, orig_images, quads = crop_faces(image_size, target_files, scale, center_sigma=center_sigma, xy_sigma=xy_sigma, use_fa=use_fa)
inv_transforms = [
calc_alignment_coefficients(quad + 0.5, [[0, 0], [0, image_size], [image_size, image_size], [image_size, 0]])
for quad in quads
]
return crops, orig_images, quads, inv_transforms
def crop_and_align_face_img(frame):
image_size = 1024
scale = 1.0
center_sigma = 0
xy_sigma = 0
use_fa = False
print('Aligning images')
crops, orig_images, quads = crop_faces_from_image(image_size, frame, scale, center_sigma=center_sigma, xy_sigma=xy_sigma, use_fa=use_fa)
inv_transforms = [
calc_alignment_coefficients(quad + 0.5, [[0, 0], [0, image_size], [image_size, image_size], [image_size, 0]])
for quad in quads
]
return crops, orig_images, quads, inv_transforms
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--prompt",
type=str,
nargs="?",
default="a photograph of an astronaut riding a horse",
help="the prompt to render"
)
parser.add_argument(
"--outdir",
type=str,
nargs="?",
help="dir to write results to",
default="results_video/debug"
)
parser.add_argument(
"--skip_grid",
action='store_true',
help="do not save a grid, only individual samples. Helpful when evaluating lots of samples",
)
parser.add_argument(
"--skip_save",
action='store_true',
help="do not save individual samples. For speed measurements.",
)
parser.add_argument(
"--ddim_steps",
type=int,
default=50,
help="number of ddim sampling steps",
)
parser.add_argument(
"--plms",
action='store_true',
help="use plms sampling",
)
parser.add_argument(
"--laion400m",
action='store_true',
help="uses the LAION400M model",
)
parser.add_argument(
"--fixed_code",
action='store_true',
help="if enabled, uses the same starting code across samples ",
default="True"
)
parser.add_argument(
"--Start_from_target",
action='store_true',
help="if enabled, uses the noised target image as the starting ",
)
parser.add_argument(
"--only_target_crop",
action='store_true',
help="if enabled, uses the noised target image as the starting ",
default=True
)
parser.add_argument(
"--target_start_noise_t",
type=int,
default=1000,
help="target_start_noise_t",
)
parser.add_argument(
"--ddim_eta",
type=float,
default=0.0,
help="ddim eta (eta=0.0 corresponds to deterministic sampling",
)
parser.add_argument(
"--n_iter",
type=int,
default=2,
help="sample this often",
)
parser.add_argument(
"--H",
type=int,
default=512,
help="image height, in pixel space",
)
parser.add_argument(
"--W",
type=int,
default=512,
help="image width, in pixel space",
)
parser.add_argument(
"--C",
type=int,
default=4,
help="latent channels",
)
parser.add_argument(
"--f",
type=int,
default=8,
help="downsampling factor",
)
parser.add_argument(
"--n_samples",
type=int,
default=10,
help="how many samples to produce for each given prompt. A.k.a. batch size",
)
parser.add_argument(
"--n_rows",
type=int,
default=0,
help="rows in the grid (default: n_samples)",
)
parser.add_argument(
"--scale",
type=float,
default=5,
help="unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) - eps(x, empty))",
)
parser.add_argument(
"--target_video",
type=str,
help="target_video",
default="examples/faceswap/Andy2.mp4",
)
parser.add_argument(
"--src_image",
type=str,
help="src_image",
default="examples/faceswap/source.jpg"
)
parser.add_argument(
"--src_image_mask",
type=str,
help="src_image_mask",
)
parser.add_argument(
"--from-file",
type=str,
help="if specified, load prompts from this file",
)
parser.add_argument(
"--config",
type=str,
default="configs/debug.yaml",
help="path to config which constructs model",
)
parser.add_argument(
"--ckpt",
type=str,
default="models/REFace/checkpoints/last.ckpt",
help="path to checkpoint of model",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="the seed (for reproducible sampling)",
)
parser.add_argument(
"--rank",
type=int,
default=0,
help="the seed (for reproducible sampling)",
)
parser.add_argument(
"--precision",
type=str,
help="evaluate at this precision",
choices=["full", "autocast"],
default="autocast"
)
parser.add_argument('--faceParser_name', default='default', type=str, help='face parser name, [ default | segnext] is currently supported.')
parser.add_argument('--faceParsing_ckpt', type=str, default="Other_dependencies/face_parsing/79999_iter.pth")
parser.add_argument('--segnext_config', default='', type=str, help='Path to pre-trained SegNeXt faceParser configuration file, '
'this option is valid when --faceParsing_ckpt=segenext')
parser.add_argument('--save_vis', action='store_true')
parser.add_argument('--seg12',default=True, action='store_true')
opt = parser.parse_args()
faceParsing_model = init_faceParsing_pretrained_model(opt.faceParser_name, opt.faceParsing_ckpt, opt.segnext_config)
Image_path='dataset/FaceData/CelebAMask-HQ/Val'
mask_real_path='dataset/FaceData/CelebAMask-HQ/src_mask'
mask_path='dataset/FaceData/CelebAMask-HQ/Val_cropped_mask'
save_path='dataset/FaceData/CelebAMask-HQ/Val_cropped'
if not os.path.exists(mask_path):
os.makedirs(mask_path)
if not os.path.exists(save_path):
os.makedirs(save_path)
# get image list
image_list = glob.glob(os.path.join(Image_path, '*.png'))
for i in tqdm(range(29000,30000)):
try:
image_path_name=Image_path+ "/" +str(i)+ '.jpg'
save_path_name=save_path+ "/" +str(i)+ '.jpg'
mask_path_name=mask_path+ "/" +str(i)+ '.png'
mask_real_path_name=mask_real_path+ "/" +str(i)+ '.png'
frame = cv2.imread(image_path_name)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Image.fromarray(frame).save(os.path.join(Image_path, f'{i}.png'))
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
crops, orig_images, quads, inv_transforms = crop_and_align_face([image_path_name])
crops = [crop.convert("RGB") for crop in crops]
T = crops[0]
# inv_transforms_all.append(inv_transforms[0])
T.save(save_path_name)
real_mask = cv2.imread(mask_real_path_name)
if real_mask is None:
pil_im = T.resize((1024,1024), Image.BILINEAR)
mask = faceParsing_demo(faceParsing_model, pil_im, convert_to_seg12=opt.seg12, model_name=opt.faceParser_name)
Image.fromarray(mask).save(mask_path_name)
# save T
else:
real_mask = Image.fromarray(real_mask)
real_mask=real_mask.resize((1024,1024), Image.BILINEAR)
#crop using quads
real_mask = real_mask.crop((quads[0][0][0], quads[0][0][1], quads[0][2][0], quads[0][2][1]))
real_mask=real_mask.resize((512,512), Image.BILINEAR)
real_mask.save(mask_path_name)
# save T
except:
print("error")
# read image as pil
T = Image.open(image_path_name)
# inv_transforms_all.append(inv_transforms[0])
real_mask = cv2.imread(mask_real_path_name)
T.save(save_path_name)
if real_mask is None:
pil_im = T.resize((1024,1024), Image.BILINEAR)
mask = faceParsing_demo(faceParsing_model, pil_im, convert_to_seg12=opt.seg12, model_name=opt.faceParser_name)
Image.fromarray(mask).save(mask_path_name)
# save T
else:
real_mask = Image.fromarray(real_mask)
real_mask=real_mask.resize((1024,1024), Image.BILINEAR)
#crop using quads
real_mask = real_mask.crop((quads[0][0][0], quads[0][0][1], quads[0][2][0], quads[0][2][1]))
real_mask=real_mask.resize((512,512), Image.BILINEAR)
real_mask.save(mask_path_name)
# continue
if __name__ == "__main__":
main()