gen_video.py 6.72 KB
Newer Older
Nianchen Deng's avatar
Nianchen Deng committed
1
2
3
4
5
6
7
8
import sys
import os
import argparse
import torch
import cv2
from torchvision.io.video import write_video
import matplotlib.pyplot as plt

Nianchen Deng's avatar
sync    
Nianchen Deng committed
9
10
sys.path.append(os.path.abspath(sys.path[0] + '/../'))

Nianchen Deng's avatar
Nianchen Deng committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
parser = argparse.ArgumentParser()
parser.add_argument('--device', type=int, default=0,
                    help='Which CUDA device to use.')
parser.add_argument('--view-file', type=str, default='hmd.csv')
parser.add_argument('--fps', type=int, default=50)
parser.add_argument('--add-hint', action='store_true')
parser.add_argument('--output-video', action='store_true')
parser.add_argument('scene', type=str)
opt = parser.parse_args()

# Select device
torch.cuda.set_device(opt.device)
print("Set CUDA:%d as current device." % torch.cuda.current_device())
torch.autograd.set_grad_enabled(False)

Nianchen Deng's avatar
sync    
Nianchen Deng committed
26
27
28
29
30
31
32
33
34
35
from data.spherical_view_syn import *
from configs.spherical_view_syn import SphericalViewSynConfig
from utils import netio
from utils import misc
from utils import img
from utils import device
from utils import view
from utils import sphere
from components.gen_final import GenFinal
from utils.progress_bar import progress_bar
Nianchen Deng's avatar
Nianchen Deng committed
36
37
38
39
40
41
42


def load_net(path):
    config = SphericalViewSynConfig()
    config.from_id(path[:-4])
    config.SAMPLE_PARAMS['perturb_sample'] = False
    # config.print()
Nianchen Deng's avatar
sync    
Nianchen Deng committed
43
44
    net = config.create_net().to(device.default())
    netio.load(path, net)
Nianchen Deng's avatar
Nianchen Deng committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    return net


def find_file(prefix):
    for path in os.listdir():
        if path.startswith(prefix):
            return path
    return None

rot_range = {
    'gas': [-15, 15, -15, 15],
    'mc': [-20, 20, -20, 20],
    'bedroom': [-40, 40, -40, 40],
    'lobby': [-20, 20, -20, 20],
    'gallery': [-50, 50, -10, 10]
}
trans_range = [-0.15, 0.15, -0.15, 0.15, -0.15, 0.15]
def clamp_gaze(gaze):
    return gaze
Nianchen Deng's avatar
sync    
Nianchen Deng committed
64
    scoord = sphere.cartesian2spherical(gaze)
Nianchen Deng's avatar
Nianchen Deng committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87


def load_views(data_desc_file) -> Tuple[view.Trans, torch.Tensor]:
    with open(data_desc_file, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        n = len(lines) // 7
        gazes = torch.empty(n * 2, 3)
        views = torch.empty(n * 2, 4, 4)
        view_idx = 0
        for i in range(0, len(lines), 7):
            gazes[view_idx * 2] = clamp_gaze(torch.tensor([
                float(str) for str in lines[i + 1].split(',')
            ]))
            gazes[view_idx * 2 + 1] = clamp_gaze(torch.tensor([
                float(str) for str in lines[i + 2].split(',')
            ]))
            views[view_idx * 2] = torch.tensor([
                float(str) for str in lines[i + 3].split(',')
            ]).view(4, 4)
            views[view_idx * 2 + 1] = torch.tensor([
                float(str) for str in lines[i + 4].split(',')
            ]).view(4, 4)
            view_idx += 1
Nianchen Deng's avatar
sync    
Nianchen Deng committed
88
89
        gazes = gazes.to(device.default())
        views = views.to(device.default())
Nianchen Deng's avatar
Nianchen Deng committed
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
    return view.Trans(views[:, :3, 3], views[:, :3, :3]), gazes


fov_list = [20, 45, 110]
res_list = [(128, 128), (256, 256), (256, 230)]  # (192,256)]
res_full = (1600, 1440)


scenes = {
    'gas': '__0_user_study/us_gas_all_in_one',
    'mc': '__0_user_study/us_mc_all_in_one',
    'bedroom': 'bedroom_all_in_one',
    'gallery': 'gallery_all_in_one',
    'lobby': 'lobby_all_in_one'
}
os.chdir(sys.path[0] + '/../data/' + scenes[opt.scene])
print('Change working directory to ', os.getcwd())

fovea_net = load_net(find_file('fovea'))
periph_net = load_net(find_file('periph'))

# Load Dataset
views, gazes = load_views(opt.view_file)
n_views = views.size()[0] // 2
print('Dataset loaded.')
print('views:', n_views)

gen = GenFinal(fov_list, res_list, res_full, fovea_net, periph_net,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
118
               device=device.default())
Nianchen Deng's avatar
Nianchen Deng committed
119
120
121
122
123
124
125
gaze_centers = gen.full_cam.proj(gazes, center_as_origin=True)

videodir = sys.path[0] + '/../data/__3_video/'
inferoutdir = videodir + \
    '%s_%s/' % (opt.scene, os.path.splitext(opt.view_file)[0])
hintoutdir = videodir + \
    '%s_%s_with_hint/' % (opt.scene, os.path.splitext(opt.view_file)[0])
Nianchen Deng's avatar
sync    
Nianchen Deng committed
126
hint = img.load(sys.path[0] + '/fovea_hint.png', rgb_only=False)
Nianchen Deng's avatar
Nianchen Deng committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146


def add_hint(img, center):
    fovea_origin = (
        int(center[0]) + res_full[1] // 2 - hint.size(-1) // 2,
        int(center[1]) + res_full[0] // 2 - hint.size(-2) // 2
    )
    fovea_region = (
        ...,
        slice(fovea_origin[1], fovea_origin[1] + hint.size(-2)),
        slice(fovea_origin[0], fovea_origin[0] + hint.size(-1)),
    )
    img[fovea_region] = img[fovea_region] * (1 - hint[:, 3:]) + \
        hint[:, :3] * hint[:, 3:]


imgs = torch.empty(n_views, 3, res_full[0], res_full[1] * 2)

if opt.add_hint and os.path.exists(inferoutdir + '/view0000.png'):
    for view_idx in range(n_views):
Nianchen Deng's avatar
sync    
Nianchen Deng committed
147
        img = img.load(inferoutdir + '/view%04d.png' % view_idx)
Nianchen Deng's avatar
Nianchen Deng committed
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
175
176
177
178
179
180
        left_center = (gaze_centers[view_idx * 2][0].item(),
                       gaze_centers[view_idx * 2][1].item())
        right_center = (gaze_centers[view_idx * 2 + 1][0].item(),
                        gaze_centers[view_idx * 2 + 1][1].item())
        add_hint(img, left_center)
        add_hint(img[..., res_full[1]:], right_center)
        imgs[view_idx:view_idx + 1] = img
        progress_bar(view_idx, n_views, 'Frame %4d processed' % view_idx)
else:
    for view_idx in range(n_views):
        left_center = (gaze_centers[view_idx * 2][0].item(),
                       gaze_centers[view_idx * 2][1].item())
        right_center = (gaze_centers[view_idx * 2 + 1][0].item(),
                        gaze_centers[view_idx * 2 + 1][1].item())
        left_view = views.get(view_idx * 2)
        right_view = views.get(view_idx * 2 + 1)
        mono_trans = view.Trans((left_view.t + right_view.t) / 2, left_view.r)
        left_image = gen.gen(left_center, left_view,
                             mono_trans=mono_trans)['blended'].cpu()
        right_image = gen.gen(right_center, right_view,
                              mono_trans=mono_trans)['blended'].cpu()
        if opt.add_hint:
            add_hint(left_image, left_center)
            add_hint(right_image, right_center)
        imgs[view_idx:view_idx + 1] = torch.cat([left_image, right_image], -1)
        progress_bar(view_idx, n_views, 'Frame %4d inferred' % view_idx)


if opt.output_video:
    video_file = videodir + '%s_%s_%s.mp4' % (
        opt.scene, os.path.splitext(opt.view_file)[0],
        'with_hint' if opt.add_hint else '')
    print('Write video file ' + os.path.abspath(video_file))
Nianchen Deng's avatar
sync    
Nianchen Deng committed
181
    imgs = img.torch2np(imgs)
Nianchen Deng's avatar
Nianchen Deng committed
182
183
184
185
186
187
188
    fourcc = cv2.VideoWriter_fourcc(*'X264')
    out = cv2.VideoWriter(video_file,fourcc, 50.0, (1440*2, 1600))
    for view_idx in range(n_views):
        out.write(imgs[view_idx])
    out.release()
else:
    outdir = hintoutdir if opt.add_hint else inferoutdir
Nianchen Deng's avatar
sync    
Nianchen Deng committed
189
    misc.create_dir(outdir)
Nianchen Deng's avatar
Nianchen Deng committed
190
    for view_idx in range(n_views):
Nianchen Deng's avatar
sync    
Nianchen Deng committed
191
        img.save(imgs[view_idx], outdir + 'view%04d.png' % view_idx)
Nianchen Deng's avatar
Nianchen Deng committed
192
        progress_bar(view_idx, n_views, 'Frame %4d saved' % view_idx)