gangweix commited on
Commit
bf288aa
·
verified ·
1 Parent(s): 501a258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -112,26 +112,34 @@ def main(share=True):
112
  # metric depth -> point cloud
113
  pcd = depth2pcd(metric_depth, intrinsics, color=cv2.cvtColor(resize_image, cv2.COLOR_BGR2RGB), input_mask=mask, ret_pcd=True)
114
  if apply_filter:
115
- cl, ind = pcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=3.0)
116
  pcd = pcd.select_by_index(ind)
117
 
 
 
 
 
118
  # save pcd to temporary .ply
119
  tmp_ply = tempfile.NamedTemporaryFile(suffix='.ply', delete=False)
120
- o3d.io.write_point_cloud(tmp_ply.name, pcd)
121
 
122
  vertices = np.asarray(pcd.points)
123
  vertex_colors = (np.asarray(pcd.colors) * 255).astype(np.uint8)
124
  vertices[:, 2] = -vertices[:, 2]
125
  vertices[:, 1] = -vertices[:, 1]
126
  mesh = trimesh.PointCloud(vertices=vertices, colors=vertex_colors)
127
- tmp_glb = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
 
128
  mesh.export(tmp_glb.name)
129
 
130
 
131
  # save raw depth (npy)
132
  depth = cv2.resize(ppd_depth, (W, H), interpolation=cv2.INTER_LINEAR)
133
- tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.npy', delete=False)
134
- np.save(tmp_raw_depth.name, depth)
 
 
 
135
 
136
  depth_vis = (depth - depth.min()) / (depth.max() - depth.min() + 1e-5) * 255.0
137
  depth_vis = depth_vis.astype(np.uint8)
@@ -139,10 +147,19 @@ def main(share=True):
139
 
140
  split_region = np.ones((image.shape[0], 50, 3), dtype=np.uint8) * 255
141
  combined_result = cv2.hconcat([image[:, :, ::-1], split_region, colored_depth[:, :, ::-1]])
142
- tmp_concat = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
143
- cv2.imwrite(tmp_concat.name, combined_result)
144
 
145
- return [(image, colored_depth), tmp_glb.name, tmp_concat.name, tmp_raw_depth.name, tmp_ply.name]
 
 
 
 
 
 
 
 
 
 
 
146
 
147
  with gr.Blocks(css=css) as demo:
148
  gr.Markdown(title)
@@ -156,25 +173,29 @@ def main(share=True):
156
  with gr.Accordion(label="Settings", open=False):
157
  denoise_steps = gr.Slider(label="Denoising Steps", minimum=1, maximum=50, value=10, step=1)
158
  apply_filter = gr.Checkbox(label="Apply filter points", value=True)
159
- submit_btn = gr.Button(value="Predict Depth")
160
 
161
  # Right: 3D point cloud + depth
162
  with gr.Column():
163
  with gr.Tabs():
164
- with gr.Tab("3D View"):
165
- model_3d = gr.Model3D(display_mode="solid", label="3D Point Map", clear_color=[1,1,1,1], height="60vh")
166
  with gr.Tab("Depth"):
167
  depth_map = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
 
 
 
 
168
 
169
- concat_file = gr.File(label="Concatenated visualization (image+depth)", elem_id="image-depth-download")
170
- raw_depth_file = gr.File(label="Raw depth output (saved as .npy)", elem_id="download")
171
- pcd_file = gr.File(label="Point Cloud (.ply)", elem_id="download-ply")
172
 
173
  submit_btn.click(
174
- on_submit,
175
- inputs=[input_image, denoise_steps, apply_filter],
176
- outputs=[depth_map, model_3d, concat_file, raw_depth_file, pcd_file]
177
- )
 
 
178
 
179
  example_files = os.listdir('assets/examples')
180
  example_files.sort()
@@ -182,7 +203,7 @@ def main(share=True):
182
  examples = gr.Examples(
183
  examples=example_files,
184
  inputs=[input_image],
185
- outputs=[depth_map, model_3d, concat_file, raw_depth_file, pcd_file],
186
  fn=on_submit
187
  )
188
 
 
112
  # metric depth -> point cloud
113
  pcd = depth2pcd(metric_depth, intrinsics, color=cv2.cvtColor(resize_image, cv2.COLOR_BGR2RGB), input_mask=mask, ret_pcd=True)
114
  if apply_filter:
115
+ cl, ind = pcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=2.0)
116
  pcd = pcd.select_by_index(ind)
117
 
118
+ tempdir = tempfile.TemporaryDirectory()
119
+ out_dir = tempdir.name
120
+ pointcloud_path = os.path.join(out_dir, 'pointcloud.ply')
121
+
122
  # save pcd to temporary .ply
123
  tmp_ply = tempfile.NamedTemporaryFile(suffix='.ply', delete=False)
124
+ o3d.io.write_point_cloud(pointcloud_path, pcd)
125
 
126
  vertices = np.asarray(pcd.points)
127
  vertex_colors = (np.asarray(pcd.colors) * 255).astype(np.uint8)
128
  vertices[:, 2] = -vertices[:, 2]
129
  vertices[:, 1] = -vertices[:, 1]
130
  mesh = trimesh.PointCloud(vertices=vertices, colors=vertex_colors)
131
+ glb_path = os.path.join(out_dir, 'pointcloud.glb')
132
+ # tmp_glb = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
133
  mesh.export(tmp_glb.name)
134
 
135
 
136
  # save raw depth (npy)
137
  depth = cv2.resize(ppd_depth, (W, H), interpolation=cv2.INTER_LINEAR)
138
+ raw_depth_path = os.path.join(out_dir, 'raw_depth.npy')
139
+ np.save(raw_depth_path, depth)
140
+
141
+ # tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.npy', delete=False)
142
+ # np.save(tmp_raw_depth.name, depth)
143
 
144
  depth_vis = (depth - depth.min()) / (depth.max() - depth.min() + 1e-5) * 255.0
145
  depth_vis = depth_vis.astype(np.uint8)
 
147
 
148
  split_region = np.ones((image.shape[0], 50, 3), dtype=np.uint8) * 255
149
  combined_result = cv2.hconcat([image[:, :, ::-1], split_region, colored_depth[:, :, ::-1]])
 
 
150
 
151
+ vis_path = os.path.join(out_dir, 'image_depth_vis.png')
152
+ cv2.imwrite(vis_path, combined_result)
153
+
154
+ # tmp_concat = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
155
+ # cv2.imwrite(tmp_concat.name, combined_result)
156
+
157
+ # download_files = [tmp_concat.name, tmp_raw_depth.name, tmp_ply.name]
158
+ download_files = [vis_path, raw_depth_path, pointcloud_path]
159
+
160
+ return [(image, colored_depth), tmp_glb.name, download_files]
161
+
162
+ # return [(image, colored_depth), tmp_glb.name, tmp_concat.name, tmp_raw_depth.name, tmp_ply.name]
163
 
164
  with gr.Blocks(css=css) as demo:
165
  gr.Markdown(title)
 
173
  with gr.Accordion(label="Settings", open=False):
174
  denoise_steps = gr.Slider(label="Denoising Steps", minimum=1, maximum=50, value=10, step=1)
175
  apply_filter = gr.Checkbox(label="Apply filter points", value=True)
176
+ submit_btn = gr.Button(value="Predict")
177
 
178
  # Right: 3D point cloud + depth
179
  with gr.Column():
180
  with gr.Tabs():
 
 
181
  with gr.Tab("Depth"):
182
  depth_map = ImageSlider(label="Depth Map with Slider View", elem_id='img-display-output', position=0.5)
183
+ with gr.Tab("3D View"):
184
+ model_3d = gr.Model3D(display_mode="solid", label="3D Point Map", clear_color=[1,1,1,1], height="60vh")
185
+ with gr.Tab("Download"):
186
+ download_files = gr.File(type='file', label="Download Files", file_types=[".ply", ".glb", ".npy"], file_types_mode="multiple")
187
 
188
+ # concat_file = gr.File(label="Concatenated visualization (image+depth)", elem_id="image-depth-download")
189
+ # raw_depth_file = gr.File(label="Raw depth output (saved as .npy)", elem_id="download")
190
+ # pcd_file = gr.File(label="Point Cloud (.ply)", elem_id="download-ply")
191
 
192
  submit_btn.click(
193
+ fn=lambda: [None, None, None, "", "", ""],
194
+ outputs=[depth_map, model_3d, download_files]
195
+ ).then(
196
+ fn=on_submit,
197
+ inputs=[input_image, denoise_steps, apply_filter],
198
+ outputs=[depth_map, model_3d, download_files]
199
 
200
  example_files = os.listdir('assets/examples')
201
  example_files.sort()
 
203
  examples = gr.Examples(
204
  examples=example_files,
205
  inputs=[input_image],
206
+ outputs=[depth_map, model_3d, download_files],
207
  fn=on_submit
208
  )
209