-
Notifications
You must be signed in to change notification settings - Fork 7
/
video.py
88 lines (69 loc) · 2.71 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from options.video_options import VideoOptions
from data import CreateDataLoader
from models import create_model
from itertools import islice
from util import util
import numpy as np
import moviepy.editor
import os
def get_random_z(opt):
z_samples = np.random.normal(0, 1, (opt.n_samples + 1, opt.nz))
return z_samples
def produce_frame(t):
k = int(t * opt.fps)
return np.concatenate(frame_rows[k], axis=1 - use_vertical)
# hard-code opt
opt = VideoOptions().parse()
opt.nThreads = 1 # test code only supports nThreads=1
opt.batchSize = 1 # test code only supports batchSize=1
opt.no_encode = True # do not use encoder
data_loader = CreateDataLoader(opt)
dataset = data_loader.load_data()
model = create_model(opt)
model.eval()
interp_mode = 'slerp'
use_vertical = 1 if opt.align_mode == 'vertical' else 0
print('Loading model %s' % opt.model)
# create website
results_dir = opt.results_dir
util.mkdir(results_dir)
total_frames = opt.num_frames * opt.n_samples
z_samples = get_random_z(opt)
frame_rows = [[] for n in range(total_frames)]
for i, data in enumerate(islice(dataset, opt.how_many)):
print('process input image %3.3d/%3.3d' % (i, opt.how_many))
model.set_input(data)
real_A, real_B = model.real_data()
wb = opt.border
hb = opt.border
h = real_A.shape[0]
w = real_A.shape[1] # border
real_A_b = np.full((h + hb, w + wb, opt.output_nc), 255, real_A.dtype)
real_A_b[hb:, wb:, :] = real_A
frames = [[real_A_b] for n in range(total_frames)]
for n in range(opt.n_samples):
z0 = z_samples[n]
z1 = z_samples[n + 1]
zs = util.interp_z(z0, z1, num_frames=opt.num_frames,
interp_mode=interp_mode)
for k in range(opt.num_frames):
_, _, fake_B, _, _ = model.test_simple(zs[k], encode_real_B=False)
fake_B_b = np.full(
(h + hb, w + wb, opt.output_nc), 255, fake_B.dtype)
fake_B_b[hb:, wb:, :] = fake_B
frames[k + opt.num_frames * n].append(fake_B_b)
for k in range(total_frames):
frame_row = np.concatenate(frames[k], axis=use_vertical)
frame_rows[k].append(frame_row)
# compile it to a vdieo
images_dir = os.path.join(results_dir, 'frames_seed%4.4d' % opt.seed)
util.mkdir(images_dir)
for k in range(total_frames):
final_frame = np.concatenate(frame_rows[k], axis=1 - use_vertical)
util.save_image(final_frame, os.path.join(
images_dir, 'frame_%4.4d.jpg' % k))
video_file = os.path.join(
results_dir, 'morphing_video_seed%4.4d_fps%d.mp4' % (opt.seed, opt.fps))
video = moviepy.editor.VideoClip(
produce_frame, duration=float(total_frames) / opt.fps)
video.write_videofile(video_file, fps=30, codec='libx264', bitrate='16M')