forked from svip-lab/impersonator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_swap.py
74 lines (53 loc) · 2.16 KB
/
run_swap.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
import os
from models.swapper import Swapper
from options.test_options import TestOptions
from utils.visdom_visualizer import VisdomVisualizer
from utils.util import mkdir
def get_img_name(img_path: str):
"""
Get the name from the image path.
Args:
img_path (str): a/b.jpg or a/b.png ...
Returns:
name (str): a/b.jpg -> b
"""
image_name = os.path.split(img_path)[-1].split('.')[0]
return image_name
def save_results(src_path, tgt_path, output_dir, preds):
"""
Save the results.
"""
import utils.cv_utils as cv_utils
src_name = get_img_name(src_path)
tgt_name = get_img_name(tgt_path)
preds = preds[0].permute(1, 2, 0)
preds = preds.cpu().numpy()
filepath = os.path.join(output_dir, '{}->{}.png'.format(src_name, tgt_name))
cv_utils.save_cv2_img(preds, filepath, normalize=True)
print('\n\t\t\tSaving results to {}'.format(filepath))
if __name__ == "__main__":
opt = TestOptions().parse()
# set imitator
swapper = Swapper(opt=opt)
if opt.ip:
visualizer = VisdomVisualizer(env=opt.name, ip=opt.ip, port=opt.port)
else:
visualizer = None
src_path = opt.src_path
tgt_path = opt.tgt_path
swapper.swap_setup(src_path, tgt_path)
if opt.post_tune:
print('\n\t\t\tPersonalization: meta cycle finetune...')
swapper.post_personalize(opt.output_dir, visualizer=None, verbose=False)
# swapper.post_personalize(opt.output_dir, visualizer=visualizer, verbose=True, bidirection=opt.bidirection)
print('\n\t\t\tPersonalization: completed...')
# if a->b
print('\n\t\t\tSwapping: {} wear the clothe of {}...'.format(src_path, tgt_path))
preds = swapper.swap(src_info=swapper.src_info, tgt_info=swapper.tsf_info,
target_part=opt.swap_part, visualizer=visualizer)
if opt.save_res:
pred_output_dir = mkdir(os.path.join(opt.output_dir, 'swappers'))
save_results(src_path, tgt_path, pred_output_dir, preds)
# # else b->a
# preds = swapper.swap(src_info=swapper.tgt_info, tgt_info=swapper.src_info,
# target_part=opt.swap_part, visualizer=visualizer)