Spaces:
Runtime error
Runtime error
jens commited on
Commit ·
706546d
1
Parent(s): d777e23
cube size slider, masked pointcloud
Browse files- app.py +5 -3
- inference.py +22 -2
app.py
CHANGED
|
@@ -35,7 +35,9 @@ with block:
|
|
| 35 |
input_image = gr.Image(label='Input', type='pil', source='webcam', tool=None)
|
| 36 |
masks_annotated_image = gr.AnnotatedImage(label='Segments', height=512)
|
| 37 |
pcl_figure = gr.Model3D(label="3-D Reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0])
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column(scale=1):
|
| 41 |
with gr.Row():
|
|
@@ -48,7 +50,7 @@ with block:
|
|
| 48 |
# components
|
| 49 |
components = {point_coords, point_labels, raw_image, masks, cutout_idx, input_image,
|
| 50 |
point_label_radio, text, reset_btn, sam_sgmt_everything_btn,
|
| 51 |
-
sam_decode_btn, depth_reconstruction_btn, masks_annotated_image, n_samples}
|
| 52 |
|
| 53 |
# event - init coords
|
| 54 |
def on_reset_btn_click(raw_image):
|
|
@@ -86,7 +88,7 @@ with block:
|
|
| 86 |
def on_depth_reconstruction_btn_click(inputs):
|
| 87 |
print("depth reconstruction")
|
| 88 |
image = inputs[input_image]
|
| 89 |
-
path = dpt.
|
| 90 |
return {pcl_figure: path}
|
| 91 |
depth_reconstruction_btn.click(on_depth_reconstruction_btn_click, components, [pcl_figure], queue=False)
|
| 92 |
|
|
|
|
| 35 |
input_image = gr.Image(label='Input', type='pil', source='webcam', tool=None)
|
| 36 |
masks_annotated_image = gr.AnnotatedImage(label='Segments', height=512)
|
| 37 |
pcl_figure = gr.Model3D(label="3-D Reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0])
|
| 38 |
+
with gr.Column():
|
| 39 |
+
n_samples = gr.Slider(minimum=1e3, maximum=1e6, step=1e3, default=1e3, label='Number of Samples')
|
| 40 |
+
cube_size = gr.Slider(minimum=0.000001, maximum=0.001, step=0.000001, default=0.00001, label='Cube size')
|
| 41 |
with gr.Row():
|
| 42 |
with gr.Column(scale=1):
|
| 43 |
with gr.Row():
|
|
|
|
| 50 |
# components
|
| 51 |
components = {point_coords, point_labels, raw_image, masks, cutout_idx, input_image,
|
| 52 |
point_label_radio, text, reset_btn, sam_sgmt_everything_btn,
|
| 53 |
+
sam_decode_btn, depth_reconstruction_btn, masks_annotated_image, n_samples, cube_size}
|
| 54 |
|
| 55 |
# event - init coords
|
| 56 |
def on_reset_btn_click(raw_image):
|
|
|
|
| 88 |
def on_depth_reconstruction_btn_click(inputs):
|
| 89 |
print("depth reconstruction")
|
| 90 |
image = inputs[input_image]
|
| 91 |
+
path = dpt.generate_obj_masks(image, inputs[n_samples], inputs[cube_size])
|
| 92 |
return {pcl_figure: path}
|
| 93 |
depth_reconstruction_btn.click(on_depth_reconstruction_btn_click, components, [pcl_figure], queue=False)
|
| 94 |
|
inference.py
CHANGED
|
@@ -70,7 +70,7 @@ class DepthPredictor:
|
|
| 70 |
ax.scatter(points,size=0.01, c=colors, marker='o')
|
| 71 |
return fig
|
| 72 |
|
| 73 |
-
def
|
| 74 |
# Step 1: Create a point cloud
|
| 75 |
point_cloud, color_array = self.generate_pcl(image)
|
| 76 |
#point_cloud, color_array = point_cloud[mask.ravel()[:-1]], color_array[mask.ravel()[:-1]]
|
|
@@ -82,7 +82,7 @@ class DepthPredictor:
|
|
| 82 |
mesh = o3d.geometry.TriangleMesh()
|
| 83 |
# Create cubes and add them to the mesh
|
| 84 |
for point, color in zip(point_cloud, color_array):
|
| 85 |
-
cube = o3d.geometry.TriangleMesh.create_box(width=
|
| 86 |
cube.translate(-point)
|
| 87 |
cube.paint_uniform_color(color)
|
| 88 |
mesh += cube
|
|
@@ -91,6 +91,26 @@ class DepthPredictor:
|
|
| 91 |
o3d.io.write_triangle_mesh(output_file, mesh)
|
| 92 |
return output_file
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
|
| 96 |
|
|
|
|
| 70 |
ax.scatter(points,size=0.01, c=colors, marker='o')
|
| 71 |
return fig
|
| 72 |
|
| 73 |
+
def generate_obj_rgb(self, image, n_samples, cube_size):
|
| 74 |
# Step 1: Create a point cloud
|
| 75 |
point_cloud, color_array = self.generate_pcl(image)
|
| 76 |
#point_cloud, color_array = point_cloud[mask.ravel()[:-1]], color_array[mask.ravel()[:-1]]
|
|
|
|
| 82 |
mesh = o3d.geometry.TriangleMesh()
|
| 83 |
# Create cubes and add them to the mesh
|
| 84 |
for point, color in zip(point_cloud, color_array):
|
| 85 |
+
cube = o3d.geometry.TriangleMesh.create_box(width=cube_size, height=cube_size, depth=cube_size)
|
| 86 |
cube.translate(-point)
|
| 87 |
cube.paint_uniform_color(color)
|
| 88 |
mesh += cube
|
|
|
|
| 91 |
o3d.io.write_triangle_mesh(output_file, mesh)
|
| 92 |
return output_file
|
| 93 |
|
| 94 |
+
def generate_obj_masks(self, image, n_samples, masks, cube_size):
|
| 95 |
+
# Generate a point cloud
|
| 96 |
+
point_cloud, color_array = self.generate_pcl(image)
|
| 97 |
+
mesh = o3d.geometry.TriangleMesh()
|
| 98 |
+
# Create cubes and add them to the mesh
|
| 99 |
+
cs = [(255,0,0),(0,255,0),(0,0,255)]
|
| 100 |
+
for c,(mask, _) in zip(cs, masks):
|
| 101 |
+
mask = mask.ravel()[:-1]
|
| 102 |
+
point_cloud_subset, color_array_subset = point_cloud[mask], color_array[mask]
|
| 103 |
+
idxs = np.random.choice(len(point_cloud_subset), int(n_samples))
|
| 104 |
+
point_cloud_subset = point_cloud_subset[idxs]
|
| 105 |
+
for point in point_cloud_subset:
|
| 106 |
+
cube = o3d.geometry.TriangleMesh.create_box(width=cube_size, height=cube_size, depth=cube_size)
|
| 107 |
+
cube.translate(-point)
|
| 108 |
+
cube.paint_uniform_color(c)
|
| 109 |
+
mesh += cube
|
| 110 |
+
# Save the mesh to an .obj file
|
| 111 |
+
output_file = "./cloud.obj"
|
| 112 |
+
o3d.io.write_triangle_mesh(output_file, mesh)
|
| 113 |
+
return output_file
|
| 114 |
|
| 115 |
|
| 116 |
|