codic commited on
Commit
5fcdff5
·
verified ·
1 Parent(s): 152e057

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -130,8 +130,9 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
130
 
131
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
132
 
133
- if len(pcd.points) < 100:
134
- raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
 
135
 
136
  # Orient normals and smooth
137
  if not pcd.has_normals():
@@ -147,6 +148,10 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
147
  pcd, depth=depth, width=0, scale=1.2, linear_fit=True
148
  )
149
 
 
 
 
 
150
  # Simplify mesh for better performance
151
  voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
152
  mesh = mesh_raw.simplify_vertex_clustering(
@@ -155,15 +160,20 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
155
  )
156
  mesh = mesh.filter_smooth_simple(number_of_iterations=3)
157
 
158
- # Crop and align model
159
  bbox = pcd.get_axis_aligned_bounding_box()
 
 
 
 
 
 
160
  mesh_crop = mesh.crop(bbox)
161
 
162
  gltf_path = f"./{image_path.stem}.gltf"
163
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
164
  return gltf_path
165
 
166
-
167
  # Gradio Interface
168
  title = "Depth Estimation & 3D Reconstruction Demo"
169
  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."
 
130
 
131
  pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
132
 
133
+ # Check if the point cloud has points
134
+ if len(pcd.points) == 0:
135
+ raise RuntimeError("Point cloud is empty. Check the depth image and foreground.")
136
 
137
  # Orient normals and smooth
138
  if not pcd.has_normals():
 
148
  pcd, depth=depth, width=0, scale=1.2, linear_fit=True
149
  )
150
 
151
+ # Check if the raw mesh has triangles
152
+ if not mesh_raw.has_triangles():
153
+ raise RuntimeError("No triangles found in the generated mesh. Check the point cloud data.")
154
+
155
  # Simplify mesh for better performance
156
  voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
157
  mesh = mesh_raw.simplify_vertex_clustering(
 
160
  )
161
  mesh = mesh.filter_smooth_simple(number_of_iterations=3)
162
 
163
+ # Validate and crop the mesh
164
  bbox = pcd.get_axis_aligned_bounding_box()
165
+
166
+ # Check if bounding box is valid
167
+ if bbox.is_empty():
168
+ raise RuntimeError("Bounding box is empty. Cannot crop mesh.")
169
+
170
+ # Perform cropping
171
  mesh_crop = mesh.crop(bbox)
172
 
173
  gltf_path = f"./{image_path.stem}.gltf"
174
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
175
  return gltf_path
176
 
 
177
  # Gradio Interface
178
  title = "Depth Estimation & 3D Reconstruction Demo"
179
  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."