jens commited on
Commit
d2d76c9
·
1 Parent(s): 465a347

masked pointcloud

Browse files
Files changed (3) hide show
  1. app.py +1 -1
  2. inference.py +20 -4
  3. utils.py +21 -1
app.py CHANGED
@@ -128,7 +128,7 @@ with block:
128
  def on_depth_reconstruction_btn_click(inputs):
129
  print("depth reconstruction")
130
  image = inputs[input_image]
131
- path = dpt.generate_obj_rgb(image=image, n_samples=inputs[n_samples], cube_size=inputs[cube_size]) #
132
  return {pcl_figure: path}
133
  depth_reconstruction_btn.click(on_depth_reconstruction_btn_click, components, [pcl_figure], queue=False)
134
 
 
128
  def on_depth_reconstruction_btn_click(inputs):
129
  print("depth reconstruction")
130
  image = inputs[input_image]
131
+ path = dpt.generate_obj_masks2(image=image, cube_size=inputs[cube_size], masks=masks) #
132
  return {pcl_figure: path}
133
  depth_reconstruction_btn.click(on_depth_reconstruction_btn_click, components, [pcl_figure], queue=False)
134
 
inference.py CHANGED
@@ -10,6 +10,7 @@ import open3d as o3d
10
  import pandas as pd
11
  import plotly.express as px
12
  import matplotlib.pyplot as plt
 
13
 
14
  class DepthPredictor:
15
  def __init__(self):
@@ -103,10 +104,6 @@ class DepthPredictor:
103
  # Create cubes and add them to the mesh
104
  cs = [(255,0,0),(0,255,0),(0,0,255)]
105
  for c,(mask, _) in zip(cs, masks):
106
- #if len(mask) == len(point_cloud):
107
- # mask = mask.ravel()
108
- #else:
109
- # mask = mask.ravel()[:-1]
110
  mask = mask.ravel()
111
  point_cloud_subset, color_array_subset = point_cloud[mask], color_array[mask]
112
  idxs = np.random.choice(len(point_cloud_subset), int(n_samples))
@@ -121,6 +118,25 @@ class DepthPredictor:
121
  o3d.io.write_triangle_mesh(output_file, mesh)
122
  return output_file
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
 
126
 
 
10
  import pandas as pd
11
  import plotly.express as px
12
  import matplotlib.pyplot as plt
13
+ from utils import PCL
14
 
15
  class DepthPredictor:
16
  def __init__(self):
 
104
  # Create cubes and add them to the mesh
105
  cs = [(255,0,0),(0,255,0),(0,0,255)]
106
  for c,(mask, _) in zip(cs, masks):
 
 
 
 
107
  mask = mask.ravel()
108
  point_cloud_subset, color_array_subset = point_cloud[mask], color_array[mask]
109
  idxs = np.random.choice(len(point_cloud_subset), int(n_samples))
 
118
  o3d.io.write_triangle_mesh(output_file, mesh)
119
  return output_file
120
 
121
+ def generate_obj_masks2(self, image, masks, cube_size):
122
+ # Generate a point cloud
123
+ depth = self.predict(image)
124
+ image = np.array(image)
125
+ mesh = o3d.geometry.TriangleMesh()
126
+ # Create cubes and add them to the mesh
127
+ cs = [(255,0,0),(0,255,0),(0,0,255)]
128
+ for c,(mask, _) in zip(cs, masks):
129
+ points, _ = PCL(mask, depth)
130
+ for point in points:
131
+ cube = o3d.geometry.TriangleMesh.create_box(width=cube_size, height=cube_size, depth=cube_size)
132
+ cube.translate(-point)
133
+ cube.paint_uniform_color(c)
134
+ mesh += cube
135
+ # Save the mesh to an .obj file
136
+ output_file = "./cloud.obj"
137
+ o3d.io.write_triangle_mesh(output_file, mesh)
138
+ return output_file
139
+
140
 
141
 
142
 
utils.py CHANGED
@@ -193,4 +193,24 @@ def PCL3(image):
193
  # Step 6: Create a 3D scatter plot using Plotly Express
194
  #fig = px.scatter_3d(df, x='x', y='y', z='z', color=colors, size="sizes")
195
 
196
- return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  # Step 6: Create a 3D scatter plot using Plotly Express
194
  #fig = px.scatter_3d(df, x='x', y='y', z='z', color=colors, size="sizes")
195
 
196
+ return fig
197
+
198
+
199
+ def PCL(mask, depth):
200
+ assert mask.shape == depth.shape
201
+ assert type(mask) == np.ndarray
202
+ assert type(depth) == np.ndarray
203
+ rgb_mask = np.zeros((mask.shape[0], mask.shape[1], 3))
204
+ rgb_mask[mask] = (1.0, 0.0, 0.0)
205
+ depth_o3d = o3d.geometry.Image(depth)
206
+ image_o3d = o3d.geometry.Image(mask)
207
+ rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(image_o3d, depth_o3d, convert_rgb_to_intensity=False)
208
+ # Step 3: Create a PointCloud from the RGBD image
209
+ pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, o3d.camera.PinholeCameraIntrinsic(o3d.camera.PinholeCameraIntrinsicParameters.PrimeSenseDefault))
210
+ # Step 4: Convert PointCloud data to a NumPy array
211
+ points = np.asarray(pcd.points)
212
+ colors = np.asarray(pcd.colors)
213
+ mask = (colors[:, 0] == 1.0)
214
+ points = points[mask]
215
+ colors = colors[mask]
216
+ return points, colors