-
Notifications
You must be signed in to change notification settings - Fork 41
/
viewer.py
581 lines (489 loc) · 22.8 KB
/
viewer.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import os
from pathlib import Path
import math
import glob
import time
import json
import yaml
import argparse
from typing import Tuple, Literal, List
import numpy as np
import viser
import viser.transforms as vtf
import torch
from gaussian_renderer import render_viewer
from utils.general_utils import parse_cfg
from scene.viewer import ClientThread, ViewerRenderer
from scene.viewer.ui import populate_render_tab, TransformPanel, EditPanel
DROPDOWN_USE_DIRECT_APPEARANCE_EMBEDDING_VALUE = "@Direct"
class Viewer:
def __init__(
self,
model_path: str,
host: str = "0.0.0.0",
port: int = 8080,
background_color: Tuple = (0, 0, 0),
image_format: Literal["jpeg", "png"] = "jpeg",
reorient: Literal["auto", "enable", "disable"] = "auto",
sh_degree: int = 3,
enable_transform: bool = False,
show_cameras: bool = False,
cameras_json: str = None,
):
self.device = torch.device("cuda")
self.model_path = model_path
self.host = host
self.port = port
self.background_color = background_color
self.image_format = image_format
self.sh_degree = sh_degree
self.enable_transform = enable_transform
self.show_cameras = show_cameras
self.up_direction = np.asarray([0., 0., 1.])
load_from = self._search_load_file(model_path)
self.simplified_model = True
self.show_edit_panel = True
self.show_render_panel = True
# TODO: load multiple models more elegantly
# load and create models
model, renderer, training_output_base_dir, dataset_type, self.checkpoint = self._load_model_from_file(load_from)
def get_load_iteration() -> int:
return int(os.path.basename(os.path.dirname(load_from)).replace("iteration_", ""))
# reorient the scene
cameras_json_path = cameras_json
if cameras_json_path is None:
cameras_json_path = os.path.join(training_output_base_dir, "cameras.json")
self.camera_transform = self._reorient(cameras_json_path, mode=reorient, dataset_type=dataset_type)
# load camera poses
self.camera_poses = self.load_camera_poses(cameras_json_path)
self.available_appearance_options = None
self.loaded_model_count = 1
self.gaussian_model = model
# create renderer
self.viewer_renderer = ViewerRenderer(
model,
render_viewer,
torch.tensor(background_color, dtype=torch.float, device=self.device),
)
self.clients = {}
@staticmethod
def _search_load_file(model_path: str) -> str:
# if a directory path is provided, auto search checkpoint or ply
if os.path.isdir(model_path) is False:
return model_path
# search checkpoint
checkpoint_dir = os.path.join(model_path, "checkpoints")
# find checkpoint with max iterations
load_from = None
previous_checkpoint_iteration = -1
for i in glob.glob(os.path.join(checkpoint_dir, "*.ckpt")):
try:
checkpoint_iteration = int(i[i.rfind("=") + 1:i.rfind(".")])
except Exception as err:
print("error occurred when parsing iteration from {}: {}".format(i, err))
continue
if checkpoint_iteration > previous_checkpoint_iteration:
previous_checkpoint_iteration = checkpoint_iteration
load_from = i
# not a checkpoint can be found, search point cloud
if load_from is None:
previous_point_cloud_iteration = -1
for i in glob.glob(os.path.join(model_path, "point_cloud", "iteration_*")):
try:
point_cloud_iteration = int(os.path.basename(i).replace("iteration_", ""))
except Exception as err:
print("error occurred when parsing iteration from {}: {}".format(i, err))
continue
if point_cloud_iteration > previous_point_cloud_iteration:
previous_point_cloud_iteration = point_cloud_iteration
load_from = os.path.join(i, "point_cloud.ply")
assert load_from is not None, "not a checkpoint or point cloud can be found"
return load_from
def _reorient(self, cameras_json_path: str, mode: str, dataset_type: str = None):
transform = torch.eye(4, dtype=torch.float)
if mode == "disable":
return transform
# detect whether cameras.json exists
is_cameras_json_exists = os.path.exists(cameras_json_path)
if is_cameras_json_exists is False:
if mode == "enable":
raise RuntimeError("{} not exists".format(cameras_json_path))
else:
return transform
# skip reorient if dataset type is blender
if dataset_type in ["blender", "nsvf"] and mode == "auto":
print("skip reorient for {} dataset".format(dataset_type))
return transform
print("load {}".format(cameras_json_path))
with open(cameras_json_path, "r") as f:
cameras = json.load(f)
up = torch.zeros(3)
for i in cameras:
up += torch.tensor(i["rotation"])[:3, 1]
up = -up / torch.linalg.norm(up)
print("up vector = {}".format(up))
self.up_direction = up.numpy()
return transform
# rotation = rotation_matrix(up, torch.Tensor([0, 0, 1]))
# transform[:3, :3] = rotation
# transform = torch.linalg.inv(transform)
#
# return transform
def load_camera_poses(self, cameras_json_path: str):
if os.path.exists(cameras_json_path) is False:
return []
with open(cameras_json_path, "r") as f:
return json.load(f)
def add_cameras_to_scene(self, viser_server):
if len(self.camera_poses) == 0:
return
self.camera_handles = []
camera_pose_transform = np.linalg.inv(self.camera_transform.cpu().numpy())
for camera in self.camera_poses:
name = camera["img_name"]
c2w = np.eye(4)
c2w[:3, :3] = np.asarray(camera["rotation"])
c2w[:3, 3] = np.asarray(camera["position"])
c2w[:3, 1:3] *= -1
c2w = np.matmul(camera_pose_transform, c2w)
R = vtf.SO3.from_matrix(c2w[:3, :3])
R = R @ vtf.SO3.from_x_radians(np.pi)
cx = camera["width"] // 2
cy = camera["height"] // 2
fx = camera["fx"]
camera_handle = viser_server.add_camera_frustum(
name="cameras/{}".format(name),
fov=float(2 * np.arctan(cx / fx)),
scale=0.1,
aspect=float(cx / cy),
wxyz=R.wxyz,
position=c2w[:3, 3],
color=(205, 25, 0),
)
@camera_handle.on_click
def _(event: viser.SceneNodePointerEvent[viser.CameraFrustumHandle]) -> None:
with event.client.atomic():
event.client.camera.position = event.target.position
event.client.camera.wxyz = event.target.wxyz
self.camera_handles.append(camera_handle)
self.camera_visible = True
def toggle_camera_visibility(_):
with viser_server.atomic():
self.camera_visible = not self.camera_visible
for i in self.camera_handles:
i.visible = self.camera_visible
# def update_camera_scale(_):
# with viser_server.atomic():
# for i in self.camera_handles:
# i.scale = self.camera_scale_slider.value
with viser_server.add_gui_folder("Cameras"):
self.toggle_camera_button = viser_server.add_gui_button("Toggle Camera Visibility")
# self.camera_scale_slider = viser_server.add_gui_slider(
# "Camera Scale",
# min=0.,
# max=1.,
# step=0.01,
# initial_value=0.1,
# )
self.toggle_camera_button.on_click(toggle_camera_visibility)
# self.camera_scale_slider.on_update(update_camera_scale)
@staticmethod
def _do_initialize_models_from_vq(point_cloud_path: str, sh_degree, device):
# if simplified is True:
# return GaussianModelLoader.initialize_simplified_model_from_point_cloud(point_cloud_path, sh_degree, device)
from scene.gaussian_model import GaussianModelLOD
model = GaussianModelLOD(sh_degree=sh_degree, device=device)
model.load_vq(point_cloud_path)
return model, render_viewer
@staticmethod
def _do_initialize_models_from_point_cloud(point_cloud_path: str, sh_degree, device):
# if simplified is True:
# return GaussianModelLoader.initialize_simplified_model_from_point_cloud(point_cloud_path, sh_degree, device)
from scene.gaussian_model import GaussianModel
model = GaussianModel(sh_degree=sh_degree)
model.load_ply(point_cloud_path)
return model, render_viewer
def _initialize_models_from_point_cloud(self, point_cloud_path: str):
return self._do_initialize_models_from_point_cloud(point_cloud_path, self.sh_degree, self.device)
def _load_model_from_file(self, load_from: str):
print("load model from {}".format(load_from))
checkpoint = None
dataset_type = None
if load_from.endswith(".yaml") is True:
from scene.gaussian_model import GatheredGaussian, BlockedGaussian
with open(load_from) as f:
cfg = yaml.load(f, Loader=yaml.FullLoader)
config_name = os.path.splitext(os.path.basename(load_from))[0]
lp, op, pp = parse_cfg(cfg, None)
lp.model_path = os.path.join("output/", config_name) if lp.model_path == '' else lp.model_path
if lp.aabb is None:
lp.aabb = np.load(os.path.join(lp.source_path, "data_partitions", f"{lp.partition_name}_aabb.npy")).tolist()
print(f"Use default AABB of {[round(x, 2) for x in lp.aabb]}")
training_output_base_dir = lp.model_path
self.sh_degree = lp.sh_degree
with torch.no_grad():
lod_gs_list = []
for i in range(len(lp.lod_configs)):
pcd_path = lp.lod_configs[i]
lod_gs, renderer = self._do_initialize_models_from_vq(pcd_path, self.sh_degree, self.device)
lod_gs = BlockedGaussian(lod_gs, lp, compute_cov3D_python=pp.compute_cov3D_python)
lod_gs_list.append(lod_gs)
model = lod_gs_list
del lod_gs_list, lod_gs
elif load_from.endswith(".ply") is True:
model, renderer = self._initialize_models_from_point_cloud(load_from)
training_output_base_dir = os.path.dirname(os.path.dirname(os.path.dirname(load_from)))
self.sh_degree = model.max_sh_degree
else:
raise ValueError("unsupported file {}".format(load_from))
return model, renderer, training_output_base_dir, dataset_type, checkpoint
def start(self):
# create viser server
server = viser.ViserServer(host=self.host, port=self.port)
server.configure_theme(
control_layout="collapsible",
show_logo=False,
)
# register hooks
server.on_client_connect(self._handle_new_client)
server.on_client_disconnect(self._handle_client_disconnect)
tabs = server.add_gui_tab_group()
with tabs.add_tab("General"):
reset_up_button = server.add_gui_button(
"Reset up direction",
icon=viser.Icon.ARROW_AUTOFIT_UP,
hint="Reset the orbit up direction.",
)
@reset_up_button.on_click
def _(event: viser.GuiEvent) -> None:
assert event.client is not None
event.client.camera.up_direction = vtf.SO3(event.client.camera.wxyz) @ np.array([0.0, -1.0, 0.0])
# add cameras
if self.show_cameras is True:
self.add_cameras_to_scene(server)
# add render options
with server.add_gui_folder("Render"):
self.max_res_when_static = server.add_gui_slider(
"Max Res",
min=128,
max=3840,
step=128,
initial_value=1920,
)
self.max_res_when_static.on_update(self._handle_option_updated)
self.jpeg_quality_when_static = server.add_gui_slider(
"JPEG Quality",
min=0,
max=100,
step=1,
initial_value=100,
)
self.jpeg_quality_when_static.on_update(self._handle_option_updated)
self.max_res_when_moving = server.add_gui_slider(
"Max Res when Moving",
min=128,
max=3840,
step=128,
initial_value=1280,
)
self.jpeg_quality_when_moving = server.add_gui_slider(
"JPEG Quality when Moving",
min=0,
max=100,
step=1,
initial_value=60,
)
with server.add_gui_folder("Model"):
self.scaling_modifier = server.add_gui_slider(
"Scaling Modifier",
min=0.,
max=1.,
step=0.1,
initial_value=1.,
)
self.scaling_modifier.on_update(self._handle_option_updated)
if self.sh_degree > 0:
self.active_sh_degree_slider = server.add_gui_slider(
"Active SH Degree",
min=0,
max=self.sh_degree,
step=1,
initial_value=self.sh_degree,
)
self.active_sh_degree_slider.on_update(self._handle_activate_sh_degree_slider_updated)
if self.available_appearance_options is not None:
# find max appearance id
max_input_id = 0
available_option_values = list(self.available_appearance_options.values())
if isinstance(available_option_values[0], list) or isinstance(available_option_values[0], tuple):
for i in available_option_values:
if i[0] > max_input_id:
max_input_id = i[0]
else:
# convert to tuple, compatible with previous version
for i in self.available_appearance_options:
self.available_appearance_options[i] = (0, self.available_appearance_options[i])
self.available_appearance_options[DROPDOWN_USE_DIRECT_APPEARANCE_EMBEDDING_VALUE] = None
self.appearance_id = server.add_gui_slider(
"Appearance Direct",
min=0,
max=max_input_id,
step=1,
initial_value=0,
visible=max_input_id > 0
)
self.normalized_appearance_id = server.add_gui_slider(
"Normalized Appearance Direct",
min=0.,
max=1.,
step=0.01,
initial_value=0.,
)
appearance_options = list(self.available_appearance_options.keys())
self.appearance_group_dropdown = server.add_gui_dropdown(
"Appearance Group",
options=appearance_options,
initial_value=appearance_options[0],
)
self.appearance_id.on_update(self._handle_appearance_embedding_slider_updated)
self.normalized_appearance_id.on_update(self._handle_appearance_embedding_slider_updated)
self.appearance_group_dropdown.on_update(self._handel_appearance_group_dropdown_updated)
self.time_slider = server.add_gui_slider(
"Time",
min=0.,
max=1.,
step=0.01,
initial_value=0.,
)
self.time_slider.on_update(self._handle_option_updated)
if self.show_edit_panel is True:
with tabs.add_tab("Edit") as edit_tab:
self.edit_panel = EditPanel(server, self, edit_tab)
self.transform_panel: TransformPanel = None
if self.enable_transform is True:
with tabs.add_tab("Transform"):
self.transform_panel = TransformPanel(server, self, self.loaded_model_count)
if self.show_render_panel is True:
with tabs.add_tab("Render"):
populate_render_tab(
server,
self,
self.model_path,
Path("./"),
orientation_transform=torch.linalg.inv(self.camera_transform).cpu().numpy(),
enable_transform=self.enable_transform,
background_color=self.background_color,
sh_degree=self.sh_degree,
)
while True:
time.sleep(999)
def _handle_appearance_embedding_slider_updated(self, event: viser.GuiEvent):
"""
Change appearance group dropdown to "@Direct" on slider updated
"""
if event.client is None: # skip if not updated by client
return
self.appearance_group_dropdown.value = DROPDOWN_USE_DIRECT_APPEARANCE_EMBEDDING_VALUE
self._handle_option_updated(event)
def _handle_activate_sh_degree_slider_updated(self, _):
self.viewer_renderer.gaussian_model.active_sh_degree = self.active_sh_degree_slider.value
self._handle_option_updated(_)
def get_appearance_id_value(self):
"""
Return appearance id according to the slider and dropdown value
"""
# no available appearance options, simply return zero
if self.available_appearance_options is None:
return (0, 0.)
name = self.appearance_group_dropdown.value
# if the value of dropdown is "@Direct", or not in available_appearance_options, return the slider's values
if name == DROPDOWN_USE_DIRECT_APPEARANCE_EMBEDDING_VALUE or name not in self.available_appearance_options:
return (self.appearance_id.value, self.normalized_appearance_id.value)
# else return the values according to the dropdown
return self.available_appearance_options[name]
def _handel_appearance_group_dropdown_updated(self, event: viser.GuiEvent):
"""
Update slider's values when dropdown updated
"""
if event.client is None: # skip if not updated by client
return
# get appearance ids according to the dropdown value
appearance_id, normalized_appearance_id = self.available_appearance_options[self.appearance_group_dropdown.value]
# update sliders
self.appearance_id.value = appearance_id
self.normalized_appearance_id.value = normalized_appearance_id
# rerender
self._handle_option_updated(event)
def _handle_option_updated(self, _):
"""
Simply push new render to all client
"""
return self.rerender_for_all_client()
def handle_option_updated(self, _):
return self._handle_option_updated(_)
def rerender_for_client(self, client_id: int):
"""
Render for specific client
"""
try:
# switch to low resolution mode first, then notify the client to render
self.clients[client_id].state = "low"
self.clients[client_id].render_trigger.set()
except:
# ignore errors
pass
def rerender_for_all_client(self):
for i in self.clients:
self.rerender_for_client(i)
def _handle_new_client(self, client: viser.ClientHandle) -> None:
"""
Create and start a thread for every new client
"""
# create client thread
client_thread = ClientThread(self, self.viewer_renderer, client)
client_thread.start()
# store this thread
self.clients[client.client_id] = client_thread
def _handle_client_disconnect(self, client: viser.ClientHandle):
"""
Destroy client thread when client disconnected
"""
try:
self.clients[client.client_id].stop()
del self.clients[client.client_id]
except Exception as err:
print(err)
if __name__ == "__main__":
# define arguments
parser = argparse.ArgumentParser()
parser.add_argument("model_path", type=str)
parser.add_argument("--host", "-a", type=str, default="0.0.0.0")
parser.add_argument("--port", "-p", type=int, default=8080)
parser.add_argument("--background_color", "--background_color", "--bkg_color", "-b",
type=str, nargs="+", default=["black"],
help="e.g.: white, black, 0 0 0, 1 1 1")
parser.add_argument("--image_format", "--image-format", "-f", type=str, default="jpeg")
parser.add_argument("--reorient", "-r", type=str, default="auto",
help="whether reorient the scene, available values: auto, enable, disable")
parser.add_argument("--sh_degree", "--sh-degree", "--sh",
type=int, default=3)
parser.add_argument("--enable_transform", "--enable-transform",
action="store_true", default=False,
help="Enable transform options on Web UI. May consume more memory")
parser.add_argument("--show_cameras", "--show-cameras",
action="store_true")
parser.add_argument("--cameras-json", "--cameras_json", type=str, default=None)
args = parser.parse_args()
# arguments post process
if len(args.background_color) == 1 and isinstance(args.background_color[0], str):
if args.background_color[0] == "white":
args.background_color = (1., 1., 1.)
else:
args.background_color = (0., 0., 0.)
else:
args.background_color = tuple([float(i) for i in args.background_color])
# create viewer
viewer_init_args = {key: getattr(args, key) for key in vars(args)}
viewer = Viewer(**viewer_init_args)
# start viewer server
viewer.start()