forked from e4s2024/E4S2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap_face_video.py
executable file
·66 lines (49 loc) · 2.39 KB
/
swap_face_video.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
from PIL import Image
import torch
import numpy as np
import glob
import os
from face_swap_for_video import faceSwapping_pipeline
from options.our_swap_face_pipeline_options import OurSwapFacePipelineOptions
from models.networks import Net3
from utils import torch_utils
source = '/apdcephfs/share_1290939/zhianliu/datasets/video_swapping_demo/celebrate/zelensky.jpg'
target_path = '/apdcephfs_cq2/share_1290939/branchwang/projects/E4S/swap_face_video_res/swap_zelensky_to_target1/crop/'
n = 200
target_frames = sorted(glob.glob(target_path + '*.png'))
source_name = os.path.basename(source).split('.')[0]
# target_name = target_path.split('/')[-3]
target_name = '056'
# swap_' + source_name + '_to_' + target_name # + '_optim_w'
save_name = 'swap_zelensky_to_target1'
save_dir = '/apdcephfs_cq2/share_1290939/branchwang/projects/E4S/swap_face_video_res/' + save_name
if not os.path.exists(save_dir):
os.makedirs(save_dir)
opts = OurSwapFacePipelineOptions().parse()
# opts.PTI_checkpoint_path = '/apdcephfs/share_1290939/zhianliu/py_projects/pytorch-DDP-demo/work_dirs/v_18_video_swapping/28494_to_874/finetuned_G_lr0.001000_iters150.pth'
net = Net3(opts)
net = net.to(opts.device)
# save_dict = torch.load(opts.PTI_checkpoint_path)
save_dict = torch.load(opts.checkpoint_path)
net.load_state_dict(torch_utils.remove_module_prefix(save_dict["state_dict"], prefix="module."))
net.latent_avg = save_dict['latent_avg'].to(opts.device)
print("Load LocalStyleGAN pre-trained model success!")
for target in target_frames[:n]:
name = os.path.basename(target).split('.')[0]
res = faceSwapping_pipeline(source=source,
target=target,
opts=opts,
net=net,
save_dir=save_dir,
target_mask=None,
need_crop =False,
verbose=False,
only_target_crop=False,
only_source_crop=True,
optimize_W=False,
finetune_net=False,
copy_face=False,
pose_drive='faceVid2Vid',
face_enhancement='gpen',
name=name)
# res.save(os.path.join(save_dir, name + '.png'))