forked from 3DTopia/LGM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
362 lines (298 loc) · 11.9 KB
/
gui.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
import os
import tyro
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from core.options import AllConfigs, Options
from core.gs import GaussianRenderer
import dearpygui.dearpygui as dpg
import kiui
from kiui.cam import OrbitCamera
import os
import cv2
from PIL import Image
import torchvision.transforms as T
def apply_colormap(gray, minmax=None, cmap=cv2.COLORMAP_JET):
"""
Input:
gray: gray image, tensor/numpy, (H, W)
Output:
depth: (3, H, W), tensor
"""
if type(gray) is not np.ndarray:
gray = gray.detach().cpu().numpy().astype(np.float32)
gray = gray.squeeze()
assert len(gray.shape) == 2
x = np.nan_to_num(gray) # change nan to 0
if minmax is None:
mi = np.min(x) # get minimum positive value
ma = np.max(x)
else:
mi, ma = minmax
x = (x - mi) / (ma - mi + 1e-8) # normalize to 0~1
x = (255 * x).astype(np.uint8)
x_ = Image.fromarray(cv2.applyColorMap(x, cmap))
x_ = T.ToTensor()(x_) # (3, H, W)
return x_
def apply_gray(gray, minmax=None, cmap=cv2.COLORMAP_JET):
"""
Input:
gray: gray image, tensor/numpy, (H, W)
Output:
depth: (3, H, W), tensor
"""
if type(gray) is not np.ndarray:
gray = gray.detach().cpu().numpy().astype(np.float32)
gray = gray.squeeze()
assert len(gray.shape) == 2
x = np.nan_to_num(gray) # change nan to 0
if minmax is None:
mi = np.min(x) # get minimum positive value
ma = np.max(x)
else:
mi, ma = minmax
x = (x - mi) / (ma - mi + 1e-8) # normalize to 0~1
x = (255 * x).astype(np.uint8)
x_ = T.ToTensor()(x) # (3, H, W)
return x_
class GUI:
def __init__(self, opt: Options):
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
self.opt = opt
self.W = opt.output_size
self.H = opt.output_size
self.cam = OrbitCamera(self.W, self.H, r=opt.cam_radius, fovy=opt.fovy)
self.device = torch.device("cuda")
self.tan_half_fov = np.tan(0.5 * np.deg2rad(opt.fovy))
self.proj_matrix = torch.zeros(4, 4, dtype=torch.float32, device=self.device)
self.proj_matrix[0, 0] = 1 / self.tan_half_fov
self.proj_matrix[1, 1] = 1 / self.tan_half_fov
self.proj_matrix[2, 2] = (opt.zfar + opt.znear) / (opt.zfar - opt.znear)
self.proj_matrix[3, 2] = - (opt.zfar * opt.znear) / (opt.zfar - opt.znear)
self.proj_matrix[2, 3] = 1
self.mode = "image"
self.buffer_image = np.ones((self.W, self.H, 3), dtype=np.float32)
self.need_update = True # update buffer_image
# renderer
self.renderer = GaussianRenderer(opt)
self.gaussain_scale_factor = 1
self.gaussians = self.renderer.load_ply(opt.test_path).to(self.device)
dpg.create_context()
self.register_dpg()
self.test_step()
def __del__(self):
dpg.destroy_context()
@torch.no_grad()
def test_step(self):
# ignore if no need to update
if not self.need_update:
return
starter = torch.cuda.Event(enable_timing=True)
ender = torch.cuda.Event(enable_timing=True)
starter.record()
# should update image
if self.need_update:
# render image
cam_poses = torch.from_numpy(self.cam.pose).unsqueeze(0).to(self.device)
cam_poses[:, :3, 1:3] *= -1 # invert up & forward direction
# cameras needed by gaussian rasterizer
cam_view = torch.inverse(cam_poses).transpose(1, 2) # [V, 4, 4]
cam_view_proj = cam_view @ self.proj_matrix # [V, 4, 4]
cam_pos = - cam_poses[:, :3, 3] # [V, 3]
buffer_image = self.renderer.render(self.gaussians.unsqueeze(0), cam_view.unsqueeze(0), cam_view_proj.unsqueeze(0), cam_pos.unsqueeze(0), scale_modifier=self.gaussain_scale_factor)[self.mode]
bufffers_ = self.renderer.render(self.gaussians.unsqueeze(0), cam_view.unsqueeze(0), cam_view_proj.unsqueeze(0), cam_pos.unsqueeze(0), scale_modifier=self.gaussain_scale_factor)
if self.mode in ['depth', 'Heatmap depth']:
buffer_image = buffer_image[0]
'''
depth_map_pil = T.ToPILImage()(buffer_image)
depth_map_pil.save("bw_depth_example.jpg")
torchvision.utils.save_image(
apply_colormap(buffer_image),
"colormap_depth_example.png",
)
'''
if self.mode == 'Heatmap depth': buffer_image = apply_colormap(buffer_image)
else: buffer_image = apply_gray(buffer_image)
#if self.mode not in ['depth']:
buffer_image = buffer_image.squeeze(1) # [B, C, H, W]
if self.mode not in ['depth', 'Heatmap depth']:
if self.mode in ['alpha']:
buffer_image = buffer_image.repeat(1, 3, 1, 1)
buffer_image = F.interpolate(
buffer_image,
size=(self.H, self.W),
mode="bilinear",
align_corners=False,
).squeeze(0)
#if self.mode not in ['depth']:
self.buffer_image = (
buffer_image.permute(1, 2, 0)
.contiguous()
.clamp(0, 1)
.contiguous()
.detach()
.cpu()
.numpy()
)
#else:
#self.buffer_image = buffer_image
self.need_update = False
ender.record()
torch.cuda.synchronize()
t = starter.elapsed_time(ender)
dpg.set_value("_log_infer_time", f"{t:.4f}ms ({int(1000/t)} FPS)")
dpg.set_value(
"_texture", self.buffer_image
) # buffer must be contiguous, else seg fault!
def register_dpg(self):
### register texture
with dpg.texture_registry(show=False):
dpg.add_raw_texture(
self.W,
self.H,
self.buffer_image,
format=dpg.mvFormat_Float_rgb,
tag="_texture",
)
### register window
# the rendered image, as the primary window
with dpg.window(
tag="_primary_window",
width=self.W,
height=self.H,
pos=[0, 0],
no_move=True,
no_title_bar=True,
no_scrollbar=True,
):
# add the texture
dpg.add_image("_texture")
# dpg.set_primary_window("_primary_window", True)
# control window
with dpg.window(
label="Control",
tag="_control_window",
width=600,
height=self.H,
pos=[self.W, 0],
no_move=True,
no_title_bar=True,
):
# button theme
with dpg.theme() as theme_button:
with dpg.theme_component(dpg.mvButton):
dpg.add_theme_color(dpg.mvThemeCol_Button, (23, 3, 18))
dpg.add_theme_color(dpg.mvThemeCol_ButtonHovered, (51, 3, 47))
dpg.add_theme_color(dpg.mvThemeCol_ButtonActive, (83, 18, 83))
dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 5)
dpg.add_theme_style(dpg.mvStyleVar_FramePadding, 3, 3)
# timer stuff
with dpg.group(horizontal=True):
dpg.add_text("Infer time: ")
dpg.add_text("no data", tag="_log_infer_time")
# rendering options
with dpg.collapsing_header(label="Rendering", default_open=True):
# mode combo
def callback_change_mode(sender, app_data):
self.mode = app_data
self.need_update = True
dpg.add_combo(
("image", "alpha","depth", "Heatmap depth"),
label="mode",
default_value=self.mode,
callback=callback_change_mode,
)
# fov slider
def callback_set_fovy(sender, app_data):
self.cam.fovy = np.deg2rad(app_data)
self.need_update = True
dpg.add_slider_int(
label="FoV (vertical)",
min_value=1,
max_value=120,
format="%d deg",
default_value=np.rad2deg(self.cam.fovy),
callback=callback_set_fovy,
)
def callback_set_gaussain_scale(sender, app_data):
self.gaussain_scale_factor = app_data
self.need_update = True
dpg.add_slider_float(
label="gaussain scale",
min_value=0,
max_value=1,
format="%.2f",
default_value=self.gaussain_scale_factor,
callback=callback_set_gaussain_scale,
)
### register camera handler
def callback_camera_drag_rotate(sender, app_data):
if not dpg.is_item_focused("_primary_window"):
return
dx = app_data[1]
dy = app_data[2]
self.cam.orbit(dx, dy)
self.need_update = True
def callback_camera_wheel_scale(sender, app_data):
if not dpg.is_item_focused("_primary_window"):
return
delta = app_data
self.cam.scale(delta)
self.need_update = True
def callback_camera_drag_pan(sender, app_data):
if not dpg.is_item_focused("_primary_window"):
return
dx = app_data[1]
dy = app_data[2]
self.cam.pan(dx, dy)
self.need_update = True
with dpg.handler_registry():
# for camera moving
dpg.add_mouse_drag_handler(
button=dpg.mvMouseButton_Left,
callback=callback_camera_drag_rotate,
)
dpg.add_mouse_wheel_handler(callback=callback_camera_wheel_scale)
dpg.add_mouse_drag_handler(
button=dpg.mvMouseButton_Middle, callback=callback_camera_drag_pan
)
dpg.create_viewport(
title="Gaussian3D",
width=self.W + 600,
height=self.H + (45 if os.name == "nt" else 0),
resizable=False,
)
### global theme
with dpg.theme() as theme_no_padding:
with dpg.theme_component(dpg.mvAll):
# set all padding to 0 to avoid scroll bar
dpg.add_theme_style(
dpg.mvStyleVar_WindowPadding, 0, 0, category=dpg.mvThemeCat_Core
)
dpg.add_theme_style(
dpg.mvStyleVar_FramePadding, 0, 0, category=dpg.mvThemeCat_Core
)
dpg.add_theme_style(
dpg.mvStyleVar_CellPadding, 0, 0, category=dpg.mvThemeCat_Core
)
dpg.bind_item_theme("_primary_window", theme_no_padding)
dpg.setup_dearpygui()
### register a larger font
# get it from: https://github.com/lxgw/LxgwWenKai/releases/download/v1.300/LXGWWenKai-Regular.ttf
if os.path.exists("LXGWWenKai-Regular.ttf"):
with dpg.font_registry():
with dpg.font("LXGWWenKai-Regular.ttf", 18) as default_font:
dpg.bind_font(default_font)
# dpg.show_metrics()
dpg.show_viewport()
def render(self):
while dpg.is_dearpygui_running():
# update texture every frame
self.test_step()
dpg.render_dearpygui_frame()
opt = tyro.cli(AllConfigs)
# load a saved ply and visualize
assert opt.test_path.endswith('.ply'), '--test_path must be a .ply file saved by infer.py'
gui = GUI(opt)
gui.render()