codic commited on
Commit
0487198
·
verified ·
1 Parent(s): 1572b8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -114,26 +114,35 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
114
  # Keep depth values only within the edge mask area (isolating the main subject)
115
  depth_image_sharpened = np.where(edge_mask, depth_image_normalized, 0)
116
 
117
- # Create mesh data using Trimesh with the refined depth map
118
- vertices, faces, normals, values = trimesh.voxel.creation.voxelize(
119
- depth_image_sharpened, pitch=1
120
- )
121
- mesh = trimesh.Trimesh(vertices=vertices, faces=faces, process=True)
122
-
123
- # Apply Trimesh filters for further refinement
124
- mesh.remove_degenerate_faces()
125
- mesh.remove_duplicate_faces()
126
- mesh.fill_holes()
127
-
128
- # Additional cleanup step: Remove small, low-volume faces
129
- mesh = mesh.remove_small_faces(min_area=500)
130
 
131
- # Smooth the mesh (optional)
132
- mesh = mesh.smooth()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  # Generate Open3D point cloud for additional transformations
135
  pcd = o3d.geometry.PointCloud()
136
- pcd.points = o3d.utility.Vector3dVector(vertices)
137
 
138
  # Check for minimum point count
139
  if len(pcd.points) < 100:
@@ -192,6 +201,7 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
192
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
193
 
194
  return gltf_path
 
195
  # Gradio Interface
196
  title = "Depth Estimation & 3D Reconstruction Demo"
197
  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."
 
114
  # Keep depth values only within the edge mask area (isolating the main subject)
115
  depth_image_sharpened = np.where(edge_mask, depth_image_normalized, 0)
116
 
117
+ # Convert depth image to 3D points
118
+ h, w = depth_image_sharpened.shape
119
+ y, x = np.mgrid[:h, :w]
120
+ z = depth_image_sharpened
121
+ points_3d = np.vstack((x.flatten(), y.flatten(), z.flatten())).T
 
 
 
 
 
 
 
 
122
 
123
+ # Create Trimesh object from 3D points and depth-based faces
124
+ try:
125
+ # Construct mesh from point cloud
126
+ mesh = trimesh.Trimesh(vertices=points_3d, process=True)
127
+
128
+ # Apply Trimesh filters for further refinement
129
+ mesh.remove_degenerate_faces()
130
+ mesh.remove_duplicate_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
 
147
  # Check for minimum point count
148
  if len(pcd.points) < 100:
 
201
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
202
 
203
  return gltf_path
204
+
205
  # Gradio Interface
206
  title = "Depth Estimation & 3D Reconstruction Demo"
207
  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."