Update app.py
Browse files
app.py
CHANGED
|
@@ -120,39 +120,40 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
|
|
| 120 |
z = depth_image_sharpened
|
| 121 |
points_3d = np.vstack((x.flatten(), y.flatten(), z.flatten())).T
|
| 122 |
|
| 123 |
-
# Validate that
|
| 124 |
-
if points_3d.shape[0] < 100: # Adjust
|
| 125 |
raise RuntimeError("Insufficient points to create a mesh.")
|
| 126 |
|
| 127 |
try:
|
| 128 |
-
#
|
| 129 |
mesh = trimesh.Trimesh(vertices=points_3d, process=True)
|
| 130 |
-
if
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error generating Trimesh mesh: {e}")
|
| 146 |
-
return None
|
| 147 |
|
| 148 |
# Continue with Open3D processing if mesh creation succeeds
|
| 149 |
pcd = o3d.geometry.PointCloud()
|
| 150 |
pcd.points = o3d.utility.Vector3dVector(points_3d)
|
| 151 |
|
| 152 |
-
# Check for minimum point count
|
| 153 |
if len(pcd.points) < 100:
|
| 154 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 155 |
|
|
|
|
| 156 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.02, max_nn=80))
|
| 157 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 2000.0]))
|
| 158 |
|
|
|
|
| 120 |
z = depth_image_sharpened
|
| 121 |
points_3d = np.vstack((x.flatten(), y.flatten(), z.flatten())).T
|
| 122 |
|
| 123 |
+
# Validate that there are enough points to create a mesh
|
| 124 |
+
if points_3d.shape[0] < 100: # Adjust threshold as needed
|
| 125 |
raise RuntimeError("Insufficient points to create a mesh.")
|
| 126 |
|
| 127 |
try:
|
| 128 |
+
# Attempt to create a mesh from the points
|
| 129 |
mesh = trimesh.Trimesh(vertices=points_3d, process=True)
|
| 130 |
+
if mesh.is_empty:
|
| 131 |
+
raise ValueError("Generated mesh is empty.")
|
| 132 |
+
|
| 133 |
+
# Apply filters and simplification
|
| 134 |
+
mesh.update_faces(mesh.nondegenerate_faces())
|
| 135 |
+
mesh.update_faces(mesh.unique_faces())
|
| 136 |
+
mesh.fill_holes()
|
| 137 |
+
|
| 138 |
+
# Simplify with smaller voxel size for more detail
|
| 139 |
+
voxel_size = max(mesh.bounds[1] - mesh.bounds[0]) / 3200
|
| 140 |
+
mesh = mesh.simplify_vertex_clustering(
|
| 141 |
+
voxel_size=voxel_size,
|
| 142 |
+
contraction='average',
|
| 143 |
+
)
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error generating Trimesh mesh: {e}")
|
| 146 |
+
return None # Return None if mesh creation fails
|
| 147 |
|
| 148 |
# Continue with Open3D processing if mesh creation succeeds
|
| 149 |
pcd = o3d.geometry.PointCloud()
|
| 150 |
pcd.points = o3d.utility.Vector3dVector(points_3d)
|
| 151 |
|
| 152 |
+
# Check for minimum point count in the point cloud for normals estimation
|
| 153 |
if len(pcd.points) < 100:
|
| 154 |
raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
|
| 155 |
|
| 156 |
+
# Estimate normals
|
| 157 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.02, max_nn=80))
|
| 158 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 2000.0]))
|
| 159 |
|