-
Notifications
You must be signed in to change notification settings - Fork 31
/
export_to_facewarper.py
84 lines (76 loc) · 3.3 KB
/
export_to_facewarper.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
import numpy as np
import torch
from torch.autograd import (Variable,
grad)
import imp
import argparse
import os
import glob
from depthnet_gan import DepthNetGAN
from depthnet_gan_learnm import DepthNetGAN_M
from interactive import warp_to_target_face
'''
Process arguments.
'''
def parse_args():
parser = argparse.ArgumentParser(
description="Warp a source face to multiple target faces.")
parser.add_argument('--src_kpts_file', type=str, required=True)
parser.add_argument('--tgt_kpts_dir', type=str, required=True,
help="Directory of target keypoints. The filenames of " +
"this dir should match those in tgt_img_dir (except the " +
"file extension).")
parser.add_argument('--src_img_file', type=str, required=True)
parser.add_argument('--tgt_img_dir', type=str, required=True)
parser.add_argument('--output_dir', type=str, required=True)
parser.add_argument('--output_size', type=str, default='tgt',
help="Output size of the warp. Can be an integer, or 'tgt' or 'src'.")
parser.add_argument('--checkpoint', type=str, required=True,
help="Model checkpoint .pkl")
parser.add_argument('--save_plots', action='store_true')
parser.add_argument('--learn_m', action='store_true',
help="This flag must be set if you're using the DepthNet " +
"model which explicitly predicts the affine parameters.")
parser.add_argument('--kpt_file_separator', type=str, default=',',
help="The separator used for the keypoint files. For example, if " +
"these are CSV then ',' should be used (which is the default value).")
parser.add_argument('--network', type=str, required=True)
parser.add_argument('--cpu', action='store_true')
args = parser.parse_args()
return args
args = parse_args()
# Dynamically load network module.
net_module = imp.load_source('network', args.network)
gen_fn, disc_fn = getattr(net_module, 'get_network')()
if args.learn_m:
gan_class = DepthNetGAN_M
else:
gan_class = DepthNetGAN
gan_kwargs = {
'g_fn': gen_fn,
'd_fn': disc_fn,
'use_cuda': False if args.cpu else 'detect'
}
net = gan_class(**gan_kwargs)
if args.checkpoint is not None:
print("Loading model: %s" % args.checkpoint)
net.load(args.checkpoint)
if args.output_size not in ['src', 'tgt']:
output_size = float(args.output_size)
else:
output_size = args.output_size
for img_filename in glob.glob("%s/*.png" % args.tgt_img_dir):
basename = os.path.basename(img_filename)
kpt_filename = "%s/%s.txt" % (args.tgt_kpts_dir,
basename.replace(".png",""))
print(img_filename, kpt_filename)
warp_to_target_face(net,
src_kpts_file=args.src_kpts_file,
tgt_kpts_file=kpt_filename,
src_img_file=args.src_img_file,
tgt_img_file=img_filename,
output_dir=args.output_dir,
output_size=output_size,
kpt_file_separator=args.kpt_file_separator,
basename_prefix='tgt',
save_plots=args.save_plots)