File size: 8,115 Bytes
7648567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np


def get_camera_dir(idx):
    img_scale = self.body['test'].get('img_scale', 1.0)
    view_setting = self.body['test'].get('view_setting', 'free')
    if view_setting == 'camera':
        # training view setting
        cam_id = self.body['test']['render_view_idx']
        intr = self.dataset.intr_mats[cam_id].copy()
        intr[:2] *= img_scale
        extr = self.dataset.extr_mats[cam_id].copy()
        img_h, img_w = int(self.dataset.img_heights[cam_id] * img_scale), int(self.dataset.img_widths[cam_id] * img_scale)
    elif view_setting.startswith('free'):
        # free view setting
        # frame_num_per_circle = 360
        # print(self.opt['test'].get('global_orient', False))
        frame_num_per_circle = 360
        rot_Y = (idx % frame_num_per_circle) / float(frame_num_per_circle) * 2 * np.pi

        extr = visualize_util.calc_free_mv(object_center,
                                           tar_pos = np.array([0, 0, 2.5]),
                                           rot_Y = rot_Y,
                                           rot_X = 0.3 if view_setting.endswith('bird') else 0.,
                                           global_orient = global_orient if self.body['test'].get('global_orient', False) else None)
        intr = np.array([[1100, 0, 512], [0, 1100, 512], [0, 0, 1]], np.float32)
        intr[:2] *= img_scale
        img_h = int(1024 * img_scale)
        img_w = int(1024 * img_scale)
        
        extr_list.append(extr)
        intr_list.append(intr)
        img_h_list.append(img_h)
        img_w_list.append(img_w)
        
    elif view_setting.startswith('degree120'):
        print('we render 120 degree')
        # +- 60 degree
        frame_per_cycle = 480
        max_degree = 60
        frame_half_cycle = frame_per_cycle // 2
        if idx%frame_per_cycle < frame_per_cycle/2:
            rot_Y = -max_degree + (2 * max_degree / frame_half_cycle) * (idx%frame_half_cycle)
        # rot_Y = (idx % frame_per_60) / float(frame_per_60) * 2 * np.pi
        else:
            rot_Y = max_degree - (2 * max_degree / frame_half_cycle) * (idx%frame_half_cycle)
        
        # to radian
        rot_Y = rot_Y * np.pi / 180
        if rot_Y<0:
            rot_Y = rot_Y + 2 * np.pi
        # print('rot_Y: ', rot_Y)
        extr = visualize_util.calc_free_mv(object_center,
                                           tar_pos = np.array([0, 0, 2.5]),
                                           rot_Y = rot_Y,
                                           rot_X = 0.3 if view_setting.endswith('bird') else 0.,
                                           global_orient = global_orient if self.body['test'].get('global_orient', False) else None)
        intr = np.array([[1100, 0, 512], [0, 1100, 512], [0, 0, 1]], np.float32)
        intr[:2] *= img_scale
        img_h = int(1024 * img_scale)
        img_w = int(1024 * img_scale)
        
        extr_list.append(extr)
        intr_list.append(intr)
        img_h_list.append(img_h)
        img_w_list.append(img_w)
    
    elif view_setting.startswith('degree90'):
        print('we render 90 degree')
        # +- 60 degree
        frame_per_cycle = 360
        max_degree = 45
        frame_half_cycle = frame_per_cycle // 2
        if idx%frame_per_cycle < frame_per_cycle/2:
            rot_Y = -max_degree + (2 * max_degree / frame_half_cycle) * (idx%frame_half_cycle)
        # rot_Y = (idx % frame_per_60) / float(frame_per_60) * 2 * np.pi
        else:
            rot_Y = max_degree - (2 * max_degree / frame_half_cycle) * (idx%frame_half_cycle)
        
        # to radian
        rot_Y = rot_Y * np.pi / 180
        if rot_Y<0:
            rot_Y = rot_Y + 2 * np.pi
        # print('rot_Y: ', rot_Y)
        extr = visualize_util.calc_free_mv(object_center,
                                           tar_pos = np.array([0, 0, 2.5]),
                                           rot_Y = rot_Y,
                                           rot_X = 0.3 if view_setting.endswith('bird') else 0.,
                                           global_orient = global_orient if self.body['test'].get('global_orient', False) else None)
        intr = np.array([[1100, 0, 512], [0, 1100, 512], [0, 0, 1]], np.float32)
        intr[:2] *= img_scale
        img_h = int(1024 * img_scale)
        img_w = int(1024 * img_scale)
        
        extr_list.append(extr)
        intr_list.append(intr)
        img_h_list.append(img_h)
        img_w_list.append(img_w)
        
        
    elif view_setting.startswith('front'):
        # front view setting
        extr = visualize_util.calc_free_mv(object_center,
                                           tar_pos = np.array([0, 0, 2.5]),
                                           rot_Y = 0.,
                                           rot_X = 0.3 if view_setting.endswith('bird') else 0.,
                                           global_orient = global_orient if self.body['test'].get('global_orient', False) else None)
        intr = np.array([[1100, 0, 512], [0, 1100, 512], [0, 0, 1]], np.float32)
        intr[:2] *= img_scale
        img_h = int(1024 * img_scale)
        img_w = int(1024 * img_scale)
        
        extr_list.append(extr)
        intr_list.append(intr)
        img_h_list.append(img_h)
        img_w_list.append(img_w)
        
        # print('extr: ', extr)
        # print('intr: ', intr)
        # print('img_h: ', img_h)
        # print('img_w: ', img_w)
        # exit()
        
        
        
    elif view_setting.startswith('back'):
        # back view setting
        extr = visualize_util.calc_free_mv(object_center,
                                           tar_pos = np.array([0, 0, 2.5]),
                                           rot_Y = np.pi,
                                           rot_X = 0.5 * np.pi / 4. if view_setting.endswith('bird') else 0.,
                                           global_orient = global_orient if self.body['test'].get('global_orient', False) else None)
        intr = np.array([[1100, 0, 512], [0, 1100, 512], [0, 0, 1]], np.float32)
        intr[:2] *= img_scale
        img_h = int(1024 * img_scale)
        img_w = int(1024 * img_scale)
    elif view_setting.startswith('moving'):
        # moving camera setting
        extr = visualize_util.calc_free_mv(object_center,
                                           # tar_pos = np.array([0, 0, 3.0]),
                                           # rot_Y = -0.3,
                                           tar_pos = np.array([0, 0, 2.5]),
                                           rot_Y = 0.,
                                           rot_X = 0.3 if view_setting.endswith('bird') else 0.,
                                           global_orient = global_orient if self.body['test'].get('global_orient', False) else None)
        intr = np.array([[1100, 0, 512], [0, 1100, 512], [0, 0, 1]], np.float32)
        intr[:2] *= img_scale
        img_h = int(1024 * img_scale)
        img_w = int(1024 * img_scale)
    elif view_setting.startswith('cano'):
        cano_center = self.dataset.cano_bounds.mean(0)
        extr = np.identity(4, np.float32)
        extr[:3, 3] = -cano_center
        rot_x = np.identity(4, np.float32)
        rot_x[:3, :3] = cv.Rodrigues(np.array([np.pi, 0, 0], np.float32))[0]
        extr = rot_x @ extr
        f_len = 5000
        extr[2, 3] += f_len / 512
        intr = np.array([[f_len, 0, 512], [0, f_len, 512], [0, 0, 1]], np.float32)
        # item = self.dataset.getitem(idx,
        #                             training = False,
        #                             extr = extr,
        #                             intr = intr,
        #                             img_w = 1024,
        #                             img_h = 1024)
        img_w, img_h = 1024, 1024
        # item['live_smpl_v'] = item['cano_smpl_v']
        # item['cano2live_jnt_mats'] = torch.eye(4, dtype = torch.float32)[None].expand(item['cano2live_jnt_mats'].shape[0], -1, -1)
        # item['live_bounds'] = item['cano_bounds']
    else:
        raise ValueError('Invalid view setting for animation!')