codic commited on
Commit
a9871ba
·
verified ·
1 Parent(s): f5f77c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -14
app.py CHANGED
@@ -132,25 +132,71 @@ def process_image(image_path, depth_map_path=None):
132
  model_3d = gr.Model3D() # Add a placeholder for Model3D output
133
  return [img, model_3d, gltf_path] # Return all three outputs
134
 
135
- def create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path, depth=9):
136
- # Create or process your PointCloud here (pcd)
137
- pcd = o3d.geometry.PointCloud()
138
 
139
- # Assuming you have your points and colors defined here
140
- # For example, if `points` and `colors` are numpy arrays of shape (N, 3)
141
- pcd.points = o3d.utility.Vector3dVector(points)
142
- pcd.colors = o3d.utility.Vector3dVector(colors)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
- # Optionally, create a mesh from the point cloud
145
- if len(pcd.points) > 0: # Check if the point cloud is not empty
146
- # Use Ball Pivoting or Poisson Reconstruction for triangulation
147
- mesh, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=depth)
148
 
149
- # Save the mesh
150
- output_path = '/tmp/gradio/24205a92124e29f4a4573fa91d1951b8dba3e8b0/3D_Model.glb'
151
- o3d.io.write_triangle_mesh(output_path, mesh)
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  return output_path
 
154
  # Gradio Interface
155
  title = "Depth Estimation & 3D Reconstruction Demo"
156
  description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
 
132
  model_3d = gr.Model3D() # Add a placeholder for Model3D output
133
  return [img, model_3d, gltf_path] # Return all three outputs
134
 
135
+ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
136
+ # Resize background to match the original image size (no cropping)
137
+ background_resized = cv2.resize(background, (foreground.shape[1], foreground.shape[0]), interpolation=cv2.INTER_LINEAR)
138
 
139
+ # Resize and normalize the depth image
140
+ depth_image_resized = np.array(
141
+ Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
142
+ )
143
+
144
+ # Normalize depth for float32 representation
145
+ depth_image_normalized = (depth_image_resized - depth_image_resized.min()) / (
146
+ depth_image_resized.max() - depth_image_resized.min()
147
+ )
148
+
149
+ # Ensure depth image is valid
150
+ if np.all(depth_image_normalized == 0):
151
+ raise RuntimeError("Depth image normalization resulted in all zeros.")
152
+
153
+ depth_image_normalized = depth_image_normalized.astype(np.float32)
154
+
155
+ # Create flat background depth with a small gap for empty space
156
+ background_depth = np.zeros_like(depth_image_normalized) + 1.5
157
+
158
+ # Combine depths to create the illusion of an empty space
159
+ combined_depth = np.maximum(depth_image_normalized * 0.5, background_depth)
160
+
161
+ # Create Open3D images
162
+ depth_o3d = o3d.geometry.Image(combined_depth)
163
+ image_o3d = o3d.geometry.Image(foreground)
164
+
165
+ # Create RGBD image directly using the Open3D images
166
+ rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
167
+ image_o3d, depth_o3d, convert_rgb_to_intensity=False
168
+ )
169
+
170
+ # Convert the Open3D color image to a NumPy array to get dimensions
171
+ rgbd_color_array = np.asarray(rgbd_image.color)
172
+ rgbd_height, rgbd_width = rgbd_color_array.shape[:2] # Get height and width from the NumPy array
173
+
174
+ # Check if the depth and foreground sizes are compatible
175
+ if rgbd_width != foreground.shape[1] or rgbd_height != foreground.shape[0]:
176
+ raise RuntimeError("RGBD image dimensions do not match foreground dimensions.")
177
 
178
+ # Camera intrinsic setup
179
+ camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
180
+ camera_intrinsic.set_intrinsics(rgbd_width, rgbd_height, 1500, 1500, rgbd_width / 2, rgbd_height / 2)
 
181
 
182
+ # Create the point cloud
183
+ pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
184
+
185
+ # Check if the point cloud is valid
186
+ print(f"Point Cloud Size: {len(pcd.points)}")
187
+ if len(pcd.points) < 100:
188
+ raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
189
+
190
+ # Estimate normals and orient
191
+ pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.1, max_nn=30))
192
+ pcd.orient_normals_towards_camera_location(camera_location=np.array([0, 0, 0]))
193
+
194
+ # Save the point cloud to a GLB file
195
+ output_path = os.path.join(Path(image_path).parent, "3D_Model.glb")
196
+ o3d.io.write_triangle_mesh(output_path, pcd)
197
 
198
  return output_path
199
+
200
  # Gradio Interface
201
  title = "Depth Estimation & 3D Reconstruction Demo"
202
  description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."