-
Notifications
You must be signed in to change notification settings - Fork 46
/
run_demo_single.py
174 lines (136 loc) · 5.59 KB
/
run_demo_single.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
import os
import cv2
import lmdb
import math
import argparse
import numpy as np
from io import BytesIO
from PIL import Image
import torch
import torch.nn as nn
from networks.generator import Generator
import argparse
import numpy as np
import torchvision
import os
from PIL import Image
from pathlib import Path
from tqdm import tqdm
import collections
def load_image(filename, size):
img = Image.open(filename).convert('RGB')
img = img.resize((size, size))
img = np.asarray(img)
img = np.transpose(img, (2, 0, 1)) # 3 x 256 x 256
return img / 255.0
def load_image1(filename, size):
img = filename.convert('RGB')
img = img.resize((size, size))
img = np.asarray(img)
img = np.transpose(img, (2, 0, 1)) # 3 x 256 x 256
return img / 255.0
def img_preprocessing(img_path, size):
img = load_image1(img_path, size) # [0, 1]
img = torch.from_numpy(img).unsqueeze(0).float() # [0, 1]
imgs_norm = (img - 0.5) * 2.0 # [-1, 1]
return imgs_norm
def vid_preprocessing(vid_path):
vid_dict = torchvision.io.read_video(vid_path, pts_unit='sec')
vid = vid_dict[0].permute(0, 3, 1, 2).unsqueeze(0)
fps = vid_dict[2]['video_fps']
vid_norm = (vid / 255.0 - 0.5) * 2.0 # [-1, 1]
return vid_norm, fps
def save_video(vid_target_recon, save_path, fps):
vid = vid_target_recon.permute(0, 2, 3, 4, 1)
vid = vid.clamp(-1, 1).cpu()
vid = ((vid - vid.min()) / (vid.max() - vid.min()) * 255).type('torch.ByteTensor')
torchvision.io.write_video(save_path, vid[0], fps=fps)
def video2imgs(videoPath):
cap = cv2.VideoCapture(videoPath)
judge = cap.isOpened()
fps = cap.get(cv2.CAP_PROP_FPS)
frames = 1
count = 1
img = []
while judge:
flag, frame = cap.read()
if not flag:
break
else:
img.append(frame)
cap.release()
return img
class Demo(nn.Module):
def __init__(self, args):
super(Demo, self).__init__()
self.args = args
model_path = args.model_path
print('==> loading model')
self.gen = Generator(args.size, args.latent_dim_style, args.latent_dim_motion, args.channel_multiplier).cuda()
weight = torch.load(model_path, map_location=lambda storage, loc: storage)['gen']
self.gen.load_state_dict(weight)
self.gen.eval()
print('==> loading data')
self.save_path = args.output_folder
os.makedirs(self.save_path, exist_ok=True)
pose_img = video2imgs(args.pose_path)
exp_img = video2imgs(args.exp_path)
img = Image.open(args.s_path)
s_img = img_preprocessing(img,256).cuda()
pose = []
for i in pose_img:
img = Image.fromarray(cv2.cvtColor(i,cv2.COLOR_BGR2RGB))
pose.append(img_preprocessing(img,256).cuda())
exp = []
for i in exp_img:
img = Image.fromarray(cv2.cvtColor(i,cv2.COLOR_BGR2RGB))
exp.append(img_preprocessing(img,256).cuda())
self.s_img = s_img
self.pose_img = pose
self.exp_img = exp
self.run()
def run(self):
output_dir = self.save_path
crop_vi = os.path.join(output_dir, 'edit.mp4')
out_edit = cv2.VideoWriter(crop_vi, cv2.VideoWriter_fourcc(*'mp4v'), 25, (256,256))
crop_vi = os.path.join(output_dir, 's.mp4')
out_s = cv2.VideoWriter(crop_vi, cv2.VideoWriter_fourcc(*'mp4v'), 25, (256,256))
print('==> running')
with torch.no_grad():
l = min(len(self.pose_img), len(self.exp_img))
for i in tqdm(range(l)):
img_pose = self.pose_img[i]
img_exp = self.exp_img[i]
img_source = self.s_img
output_dict = self.gen(img_source, img_exp, 'exp')
output_dict = self.gen(output_dict, img_pose, 'pose')
fake = output_dict
fake = fake.cpu().clamp(-1, 1)
video_numpy = fake[:,:3,:,:].clone().cpu().float().detach().numpy()
video_numpy = (np.transpose(video_numpy, (0, 2, 3, 1)) + 1) / 2.0 * 255.0
video_numpy = video_numpy.astype(np.uint8)[0]
video_numpy = cv2.cvtColor(video_numpy, cv2.COLOR_RGB2BGR)
out_edit.write(video_numpy)
video_numpy = img_source[:,:3,:,:].clone().cpu().float().detach().numpy()
video_numpy = (np.transpose(video_numpy, (0, 2, 3, 1)) + 1) / 2.0 * 255.0
video_numpy = video_numpy.astype(np.uint8)[0]
video_numpy = cv2.cvtColor(video_numpy, cv2.COLOR_RGB2BGR)
out_s.write(video_numpy)
out_edit.release()
out_s.release()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--size", type=int, default=256)
parser.add_argument("--channel_multiplier", type=int, default=1)
parser.add_argument("--model", type=str, default='')
parser.add_argument("--latent_dim_style", type=int, default=512)
parser.add_argument("--latent_dim_motion", type=int, default=20)
parser.add_argument("--s_path", type=str, default='./data/crop_video/1.jpg')
parser.add_argument("--pose_path", type=str, default='./data/crop_video/4.mp4')
parser.add_argument("--exp_path", type=str, default='./data/crop_video/4.mp4')
parser.add_argument("--face", type=str, default='both')
parser.add_argument("--model_path", type=str, default='')
parser.add_argument("--output_folder", type=str, default='')
args = parser.parse_args()
# demo
demo = Demo(args)