-
Notifications
You must be signed in to change notification settings - Fork 117
/
gui.py
196 lines (154 loc) · 7.03 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
# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
from Utils import *
import dearpygui.dearpygui as dpg
class BundleSdfGui:
def __init__(self, img_height=300):
dpg.create_context()
code_dir = os.path.dirname(os.path.realpath(__file__))
dpg.create_viewport(large_icon=f'{code_dir}/dpg.jpg')
dpg.setup_dearpygui()
self.H = int(img_height)
self.W = None
self.K = None
self.mesh = None
self.renderer = None
self.ob_in_cam = None
self.ob_in_cam_view = None
self.mesh_min_xyz = None
self.mesh_max_xyz = None
with dpg.window(label="",tag="main"):
with dpg.group(horizontal=True,tag='buttons'):
dpg.add_button(label="clean_mesh", tag="clean_mesh", callback=self.clean_mesh)
dpg.add_file_dialog(directory_selector=True, show=False, callback=self.export_mesh, tag="export_mesh_file_dialog", label='export mesh', default_filename='1.obj', height=600, width=1000)
dpg.add_button(label="export_mesh", tag="export_mesh", callback=lambda: dpg.show_item("export_mesh_file_dialog"))
def reset_mesh_view():
self.ob_in_cam_view = self.ob_in_cam
self.update_render_mesh()
dpg.add_button(label="reset_mesh_view", callback=reset_mesh_view)
with dpg.handler_registry():
dpg.add_mouse_drag_handler(button=dpg.mvMouseButton_Left, callback=self.drag_rotate_pose)
dpg.add_mouse_drag_handler(button=dpg.mvMouseButton_Right, callback=self.drag_move_pose)
dpg.add_group(horizontal=True,tag='row0',parent='main')
dpg.add_group(horizontal=True,tag='row1',parent='main')
dpg.add_group(horizontal=True,tag='row2',parent='main')
dpg.add_group(horizontal=True,tag='row3',parent='main')
dpg.add_text("frame: 0",tag='frame_id',color=[0,255,0], parent='row3')
dpg.add_text("keyframe_num: 0",tag='keyframe_num',color=[0,255,0], parent='row3')
dpg.add_text("nerf_num_frames: X",tag='nerf_num_frames',color=[0,255,0], parent='row3')
dpg.set_primary_window("main", True)
dpg.set_viewport_title('BundleSDF')
dpg.show_viewport()
def clean_mesh(self):
try:
ms = trimesh_split(self.mesh)
best_size = 0
best = None
for m in ms:
if m.vertices.shape[0]>best_size:
best_size = m.vertices.shape[0]
best = m
self.mesh = trimesh_clean(best)
except Exception as e:
logging.info(e)
def drag_rotate_pose(self, sender, app_data):
if self.ob_in_cam_view is not None and self.mesh is not None:
dx = app_data[1]
dy = app_data[2]
speed = 0.1
rx = dy/180.0*np.pi*speed
ry = -dx/180.0*np.pi*speed
pts = np.stack([self.mesh_min_xyz, self.mesh_max_xyz], axis=0).reshape(2,3)
pts = transform_pts(pts, self.ob_in_cam_view)
center = (pts.max(axis=0) + pts.min(axis=0))/2
tf = np.eye(4)
tf[:3,3] = -center
tf_ = euler_matrix(rx, ry, 0)
tf = tf_@tf
tf_ = np.eye(4)
tf_[:3,3] = center
tf = tf_@tf
self.ob_in_cam_view = tf@self.ob_in_cam_view
self.update_render_mesh()
def drag_move_pose(self, sender, app_data):
if self.ob_in_cam_view is not None and self.mesh is not None:
dx = app_data[1]
dy = app_data[2]
speed = 1/self.K[0,0]*self.ob_in_cam_view[2,3]*0.1
dx = dx*speed
dy = dy*speed
tf = np.eye(4)
tf[:2,3] = [dx, dy]
self.ob_in_cam_view = tf@self.ob_in_cam_view
self.update_render_mesh()
def update_render_mesh(self):
if self.ob_in_cam_view is not None and self.mesh is not None:
color, mask = self.renderer.render(self.ob_in_cam_view)
# color, depth = self.renderer.render(ob_in_cam=ob_in_cam)
rgba = np.concatenate((color, np.zeros((self.H,self.W,1))), axis=-1)
rgba[mask>0,...,3] = 255
dpg.set_value(f"mesh_render", rgba.reshape(-1)/255.0)
def export_mesh(self, sender, app_data):
file = app_data['file_path_name']
self.mesh.export(file)
logging.info(f"exported to: {file}")
def update_frame(self, rgb, mask, ob_in_cam, id_str, K, n_keyframe):
if self.K is None:
self.K = K.copy()
if self.W is None:
scale = 1/rgb.shape[0]*self.H
self.W = int(rgb.shape[1]*scale)
self.K[:2] *= scale
self.ob_in_cam = ob_in_cam
if self.ob_in_cam_view is None:
self.ob_in_cam_view = self.ob_in_cam.copy()
rgb = cv2.resize(rgb, dsize=(self.W,self.H), interpolation=cv2.INTER_LINEAR)
mask = cv2.resize(mask, dsize=(self.W,self.H), interpolation=cv2.INTER_NEAREST)
vis = draw_xyz_axis(rgb[...,::-1], ob_in_cam=ob_in_cam, scale=0.1, K=self.K, transparency=0, thickness=5)
vis = vis[...,::-1]
rgba = np.concatenate((vis, np.ones((self.H,self.W,1))*255), axis=-1)
masked_rgba = np.concatenate((rgb, np.ones((self.H,self.W,1))*255), axis=-1)
masked_rgba[mask==0,...,:3] = 0
if dpg.get_value("rgb") is None:
with dpg.texture_registry(show=False):
dpg.add_dynamic_texture(self.W, self.H, rgba.reshape(-1)/255.0, tag="rgb_init")
dpg.add_dynamic_texture(self.W, self.H, masked_rgba.reshape(-1)/255.0, tag="masked_rgb_init")
dpg.add_dynamic_texture(self.W, self.H, rgba.reshape(-1)/255.0, tag="rgb")
dpg.add_dynamic_texture(self.W, self.H, masked_rgba.reshape(-1)/255.0, tag="masked_rgb")
dpg.add_image("rgb_init", parent='row0')
dpg.add_image("masked_rgb_init", parent='row0')
dpg.add_image("rgb", parent='row1')
dpg.add_image("masked_rgb", parent='row1')
else:
dpg.set_value("rgb", rgba.reshape(-1)/255.0)
dpg.set_value("masked_rgb", masked_rgba.reshape(-1)/255.0)
dpg.set_value('frame_id', f"frame_id: {id_str}")
dpg.set_value('keyframe_num', f"keyframe_num: {n_keyframe}")
if dpg.get_value("mesh_render") is None:
with dpg.texture_registry(show=False):
dpg.add_dynamic_texture(self.W, self.H, np.zeros((4),dtype=np.float32), tag=f"mesh_render")
dpg.add_image(f"mesh_render", parent='row2')
if self.renderer is not None:
self.update_render_mesh()
def update_mesh(self, mesh):
self.mesh = mesh
self.clean_mesh()
if self.renderer is None:
from offscreen_renderer import TinyRenderer
self.renderer = TinyRenderer(H=self.H, W=self.W, K=self.K) #!NOTE other renderer needs openGL context and not work
self.renderer.clear_meshes()
self.renderer.add_mesh(self.mesh, color=[255,255,255])
self.mesh_min_xyz = self.mesh.vertices.min(axis=0)
self.mesh_max_xyz = self.mesh.vertices.max(axis=0)
def set_nerf_num_frames(self, nerf_num_frames):
dpg.set_value('nerf_num_frames', f"nerf_num_frames: {nerf_num_frames}")
if __name__=="__main__":
gui = BundleSdfGui()
while dpg.is_dearpygui_running():
dpg.render_dearpygui_frame()
dpg.destroy_context()