codic commited on
Commit
b3933da
·
verified ·
1 Parent(s): 75ec4a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -118,29 +118,33 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
118
  y, x = np.mgrid[:h, :w]
119
  z = depth_image_sharpened
120
  points_3d = np.vstack((x.flatten(), y.flatten(), z.flatten())).T
121
- # Create Trimesh object from 3D points and depth-based faces
 
 
 
 
122
  try:
123
  # Construct mesh from point cloud
124
  mesh = trimesh.Trimesh(vertices=points_3d, process=True)
125
- if mesh.is_empty:
126
- raise ValueError("Mesh creation failed: No valid mesh generated.")
127
-
128
- # Apply Trimesh filters for further refinement
129
- mesh.update_faces(mesh.nondegenerate_faces())
130
- mesh.update_faces(mesh.unique_faces())
131
- mesh.fill_holes()
132
-
133
- # Simplify with smaller voxel size for more detail
134
- voxel_size = max(mesh.bounds[1] - mesh.bounds[0]) / 3200
135
- mesh = mesh.simplify_vertex_clustering(
136
- voxel_size=voxel_size,
137
- contraction='average',
138
- )
139
  except Exception as e:
140
  print(f"Error generating Trimesh mesh: {e}")
141
  return None
142
 
143
- # Generate Open3D point cloud for additional transformations
144
  pcd = o3d.geometry.PointCloud()
145
  pcd.points = o3d.utility.Vector3dVector(points_3d)
146
 
@@ -148,12 +152,9 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
148
  if len(pcd.points) < 100:
149
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
150
 
151
- # Estimate normals with refined parameters
152
  pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.02, max_nn=80))
153
-
154
- # Orient normals towards a distant camera position
155
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 2000.0]))
156
-
157
  # Crop point cloud to main subject bounds
158
  bbox = pcd.get_axis_aligned_bounding_box()
159
  bbox.scale(0.5, bbox.get_center()) # Adjust scale to fit the main subject tightly
 
118
  y, x = np.mgrid[:h, :w]
119
  z = depth_image_sharpened
120
  points_3d = np.vstack((x.flatten(), y.flatten(), z.flatten())).T
121
+
122
+ # Validate that points are available to create a mesh
123
+ if points_3d.shape[0] < 100: # Adjust this threshold as necessary
124
+ raise RuntimeError("Insufficient points to create a mesh.")
125
+
126
  try:
127
  # Construct mesh from point cloud
128
  mesh = trimesh.Trimesh(vertices=points_3d, process=True)
129
+ if not mesh.is_empty:
130
+ # Apply Trimesh filters for further refinement
131
+ mesh.update_faces(mesh.nondegenerate_faces())
132
+ mesh.update_faces(mesh.unique_faces())
133
+ mesh.fill_holes()
134
+
135
+ # Simplify with smaller voxel size for more detail
136
+ voxel_size = max(mesh.bounds[1] - mesh.bounds[0]) / 3200
137
+ mesh = mesh.simplify_vertex_clustering(
138
+ voxel_size=voxel_size,
139
+ contraction='average',
140
+ )
141
+ else:
142
+ raise ValueError("Mesh creation returned an empty object.")
143
  except Exception as e:
144
  print(f"Error generating Trimesh mesh: {e}")
145
  return None
146
 
147
+ # Continue with Open3D processing if mesh creation succeeds
148
  pcd = o3d.geometry.PointCloud()
149
  pcd.points = o3d.utility.Vector3dVector(points_3d)
150
 
 
152
  if len(pcd.points) < 100:
153
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
154
 
 
155
  pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.02, max_nn=80))
 
 
156
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 2000.0]))
157
+
158
  # Crop point cloud to main subject bounds
159
  bbox = pcd.get_axis_aligned_bounding_box()
160
  bbox.scale(0.5, bbox.get_center()) # Adjust scale to fit the main subject tightly