-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
154 lines (117 loc) · 5.04 KB
/
demo.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
import time
import cv2
import numpy as np
import argparse
import open3d as o3d
import TSDFVolume
from utils import visualize
vis_param = argparse.Namespace()
vis_param.n_steps_left = 0
vis_param.index = 0
vis_param.n_imgs = 1000
vis_param.stop = True
vis_param.current_pose = np.eye(4)
def updateGeometry(geom, name, vis):
"""
Update geometry in open3D visualization
"""
if name in vis_param.__dict__.keys():
vis.remove_geometry(geom, reset_bounding_box=False)
vis.add_geometry(geom, reset_bounding_box=False)
vis_param.__dict__[name] = geom
def drawNextFrame(vis):
"""
Update the TSDF of next frame
"""
if vis_param.index >= vis_param.n_imgs or vis_param.n_steps_left <= 0 :
return False
print("Fusing frame {}/{}".format(vis_param.index + 1, vis_param.n_imgs))
# Read RGB-D image and camera pose
color_image = cv2.cvtColor(cv2.imread("data/demo/frame-%06d.color.jpg" % (vis_param.index)), cv2.COLOR_BGR2RGB)
depth_im = cv2.imread("data/demo/frame-%06d.depth.png" % (vis_param.index), -1).astype(float)
depth_im /= 1000.
depth_im[depth_im == 65.535] = 0
cam_pose = np.loadtxt("data/demo/frame-%06d.pose.txt" % (vis_param.index))
# Update camera transform with regarded to previous frame
transform_mat = np.dot(cam_pose, np.linalg.inv(vis_param.current_pose))
camera.transform(transform_mat)
vis_param.current_pose = cam_pose
# TODO: View camera control, view follow the camera pose
# view_cam = vis_param.view_control.convert_to_pinhole_camera_parameters()
# view_cam.extrinsic = cam_pose
# vis_param.view_control.convert_from_pinhole_camera_parameters(view_cam)
print("Camera Pose: {}".format(camera.get_center()))
# updateGeometry(camera, "camera_geometry", vis)
vis.update_geometry(camera)
vis.update_renderer()
# Integrate observation into voxel volume (assume color aligned with depth)
tsdf_vol.integrate(depth_im, cam_intr, cam_pose, weight=1.)
if(vis_param.index % 20 == 0):
tsdf_vol.extract_mesh()
updateGeometry(tsdf_vol.get_mesh(), "mesh", vis)
vis_param.n_steps_left -= 1
vis_param.index += 1
return True
def play(vis):
vis_param.n_steps_left += 10000
return False
def step(vis):
vis_param.n_steps_left = 1
return False
if __name__ == "__main__":
# ======================================================================================================== #
# (Optional) This is an example of how to compute the 3D bounds
# in world coordinates of the convex hull of all camera view
# frustums in the dataset
# ======================================================================================================== #
# print("Estimating voxel volume bounds...")
cam_intr = np.loadtxt("data/demo/camera-intrinsics.txt", delimiter=' ')
# vol_bnds = np.zeros((3, 2))
# for i in range(vis_param.n_imgs):
# # Read depth image and camera pose
# depth_im = cv2.imread("data/frame-%06d.depth.png" % (i), -1).astype(float)
# depth_im /= 1000. # depth is saved in 16-bit PNG in millimeters
# depth_im[depth_im == 65.535] = 0 # set invalid depth to 0 (specific to 7-scenes dataset)
# cam_pose = np.loadtxt("data/frame-%06d.pose.txt" % (i)) # 4x4 rigid transformation matrix
# # Compute camera view frustum and extend convex hull
# view_frust_pts = fusion.get_view_frustum(depth_im, cam_intr, cam_pose)
# vol_bnds[:, 0] = np.minimum(vol_bnds[:, 0], np.amin(view_frust_pts, axis=1))
# vol_bnds[:, 1] = np.maximum(vol_bnds[:, 1], np.amax(view_frust_pts, axis=1))
# Initialize voxel volume
print("Initializing voxel volume...")
vol_bnds = [
[-4.22106438, 3.86798203],
[-2.6663104 , 2.60146141],
[0. , 5.76272371]
]
tsdf_vol = TSDFVolume.TSDFVolume(vol_bnds, vox_size=0.02, use_gpu=True, verbose=True)
window = o3d.visualization.VisualizerWithKeyCallback()
# window = o3d.visualization.Visualizer()
window.create_window(window_name="TSDF", width=1280, height=720, visible=True)
window.get_render_option().mesh_show_back_face = True
# ctr = window.get_view_control()
# ctr.translate(0,0,-5.0)
window.register_key_callback(key=ord(" "), callback_func=step)
window.register_key_callback(key=ord("."), callback_func=play)
# TODO: View camera control, view follow the camera pose
# vis_param.view_control = window.get_view_control()
# view_cam = vis_param.view_control.convert_to_pinhole_camera_parameters()
# view_cam.extrinsic = np.eye(4)
# vis_param.view_control.convert_from_pinhole_camera_parameters(view_cam)
origin = o3d.geometry.TriangleMesh.create_coordinate_frame()
camera = visualize.camera(np.eye(4), scale=0.1)
bbox = visualize.wireframe_bbox([-5., -5., -5.], [5., 5., 5.])
# grid = visualize.grid()
# grid.translate((0., -5., 0.))
window.add_geometry(bbox)
window.remove_geometry(bbox, reset_bounding_box=False)
window.add_geometry(origin)
updateGeometry(camera, "camera_geometry", window)
# updateGeometry(grid, "grid", window)
# view_control.set_lookat([0,0,1])
# vis_param.view_control.set_zoom(0.1)
# vis_param.view_control.camera_local_translate(1,0,0)
# view_control.set_up([0,0,1])
window.register_animation_callback(callback_func=drawNextFrame)
window.run()
window.destroy_window()