Update app.py
Browse files
app.py
CHANGED
|
@@ -92,10 +92,11 @@ def process_image(image_path, depth_map_path=None):
|
|
| 92 |
img = Image.fromarray(blended_depth_image)
|
| 93 |
return [img, gltf_path, gltf_path]
|
| 94 |
|
| 95 |
-
def create_3d_obj(foreground, depth_image, background, image_path, depth=
|
| 96 |
-
#
|
| 97 |
depth = max(depth, 2)
|
| 98 |
-
|
|
|
|
| 99 |
# Resize and normalize depth image
|
| 100 |
depth_image_resized = np.array(
|
| 101 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
|
@@ -128,10 +129,13 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
|
|
| 128 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=50))
|
| 129 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# Simplify and crop mesh
|
| 137 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
|
@@ -147,6 +151,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
|
|
| 147 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 148 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 149 |
return gltf_path
|
|
|
|
| 150 |
# Gradio Interface
|
| 151 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 152 |
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."
|
|
|
|
| 92 |
img = Image.fromarray(blended_depth_image)
|
| 93 |
return [img, gltf_path, gltf_path]
|
| 94 |
|
| 95 |
+
def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
|
| 96 |
+
# Enforce minimum depth and add debug statement
|
| 97 |
depth = max(depth, 2)
|
| 98 |
+
print(f"Using depth value: {depth}") # Debug statement to trace depth value
|
| 99 |
+
|
| 100 |
# Resize and normalize depth image
|
| 101 |
depth_image_resized = np.array(
|
| 102 |
Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
|
|
|
|
| 129 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=50))
|
| 130 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
|
| 131 |
|
| 132 |
+
try:
|
| 133 |
+
# Perform Poisson surface reconstruction with verified depth
|
| 134 |
+
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 135 |
+
pcd, depth=depth, width=1, scale=2, linear_fit=True
|
| 136 |
+
)
|
| 137 |
+
except RuntimeError as e:
|
| 138 |
+
raise RuntimeError(f"Poisson reconstruction failed with depth={depth}: {e}")
|
| 139 |
|
| 140 |
# Simplify and crop mesh
|
| 141 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
|
|
|
|
| 151 |
gltf_path = f"./{image_path.stem}.gltf"
|
| 152 |
o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 153 |
return gltf_path
|
| 154 |
+
|
| 155 |
# Gradio Interface
|
| 156 |
title = "Depth Estimation & 3D Reconstruction Demo"
|
| 157 |
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."
|