-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_render.py
100 lines (84 loc) · 3.2 KB
/
main_render.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
import os
pienerf_dir = os.path.dirname(os.path.abspath(__file__))
from nerf.gui import NeRFSimGUI
from nerf.provider import NeRFDataset
from nerf.trainer import *
from nerf.provider import nerf_matrix_to_ngp
from get_opts import *
import warp as wp
wp.init()
import json
def save_image(image, path, opt):
from PIL import Image
image_data = image
image_data = np.clip(image_data, 0, 1) * 255
image_data = image_data.astype(np.uint8)
W = opt.W
H = opt.H
image_data = image_data.reshape(H, W, 3)
image_to_save = Image.fromarray(image_data, 'RGB')
print("saving to ", os.path.abspath(path))
image_to_save.save(path)
def get_pose(file_dir, frame_str):
try:
file_path = file_dir + "/transforms_train.json"
with open(file_path) as file:
data = json.load(file)
except FileNotFoundError:
try:
file_path = file_dir + "/transforms.json"
with open(file_path) as file:
data = json.load(file)
except FileNotFoundError:
print("no transforms found in ", file_dir, "!!!")
return None
for frame in data["frames"]:
if frame_str in frame["file_path"]:
print("reading pose from ", file_path, frame_str, "...")
return np.array(frame["transform_matrix"], dtype=np.float32)
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser()
opt = get_shared_opts(parser)
from nerf.network import NeRFNetwork
model = NeRFNetwork(
encoding="hashgrid",
bound=opt.bound,
cuda_ray=opt.cuda_ray,
density_scale=1,
min_near=opt.min_near,
density_thresh=opt.density_thresh,
bg_radius=opt.bg_radius,
)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
trainer = Trainer('ngp', opt, model,
device=device, workspace=opt.workspace, use_checkpoint=opt.ckpt)
dataset = NeRFDataset(opt, device=device, type='test')
intrinsics = dataset.intrinsics
data_root = opt.path
pose = None
if opt.workspace.split("/")[-1] == "dinosaur":
pose = get_pose(data_root, "0057")
else:
print("pose not specified!")
exit(-2)
pose = nerf_matrix_to_ngp(pose, scale=opt.scale, offset=opt.offset)
out_img_path = './output_img/'
out_img_dir = out_img_path + '/' + opt.exp_name
if not os.path.exists(out_img_dir):
os.makedirs(out_img_dir)
points = np.load(f"./debug/ip_pos_0.npy")
model.p_ori = torch.from_numpy(points)
model.IP_dx = opt.sim_dx * 1.05
with torch.no_grad():
gui = NeRFSimGUI(opt, trainer, show=False)
for def_frame in range(10,11):
points = np.load(f"./debug/ip_pos_{def_frame}.npy")
F = np.load(f"./debug/ip_F_{def_frame}.npy")
dF = np.load(f"./debug/ip_dF_{def_frame}.npy")
model.p_def = torch.from_numpy(points)
model.IP_F = torch.from_numpy(F)
model.IP_dF = torch.from_numpy(dF)
output_image_path = out_img_dir + "/img_" + str(def_frame) + ".png"
image = gui.get_render_buffer(pose, intrinsics, opt.W, opt.H, render_def=True)
save_image(image, output_image_path, opt)