-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloadnerf.py
More file actions
87 lines (62 loc) · 2.46 KB
/
Copy pathloadnerf.py
File metadata and controls
87 lines (62 loc) · 2.46 KB
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
import os
import json
import torch
import numpy as np
from pytorch3d.renderer import PerspectiveCameras
from visualizer import load_img
# setup
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# pytorch3d camera from nerf transforms.json
def transforms_cam(path):
with open(path, 'r') as filename:
data = json.load(filename)
angle = data['camera_angle_x']
transforms = torch.Tensor([ frame['transform_matrix'] for frame in data['frames'] ]) # .to(device)
rotations = []
translations = []
focals = []
# code from load_blender_data() function in pytorch3d/implicitron/dataset/load_blender.py
# and _interpret_blender_cameras() function in pytorch3d/implicitron/dataset/single_sequence_dataset.py
for pose in transforms:
f = 1 / np.tan(0.5 * angle)
f = torch.FloatTensor([f, f])
pose = pose[:3, :4]
matrix = torch.eye(4, dtype=pose.dtype)
matrix[:3, :3] = pose[:3, :3].t()
matrix[3, :3] = pose[:, 3]
matrix = matrix.inverse()
matrix[:, [0, 2]] *= -1 # flip xz coordinates
R, T = matrix[:, :3].split([3, 1], dim=0)
rotations.append(R)
translations.append(T)
focals.append(f)
rotations = torch.stack(rotations)
translations = torch.cat(translations)
focals = torch.stack(focals)
cameras = PerspectiveCameras(focal_length=focals, R=rotations, T=translations).to(device)
del data, angle, transforms, rotations, translations, focals
return cameras
# images tensor from nerf transforms.json
def transforms_img(path, alpha=True, background=[0, 0, 0]):
folderpath = os.path.dirname(path)
with open(path, 'r') as filename:
data = json.load(filename)
imgpaths = [frame['file_path'] for frame in data['frames']]
exts = ['', '.png', '.jpg', '.jpeg']
for ext in exts:
try:
alpha *= ( load_img(os.path.join(folderpath, imgpaths[0]) + ext).shape[-1] == 4 )
break
except:
pass
images = []
background = torch.Tensor(background + [0]) # add arbitrary alpha channel
for imgpath in imgpaths:
img = load_img( os.path.join(folderpath, imgpath) + ext )
if not alpha:
img[...,:3] *= img[...,3:4]
img += (1.0 - img[...,3:4]) * background
images.append(img)
images = torch.stack(images).to(device)
del data, imgpaths, background, folderpath
return images if alpha else images[...,:3]