Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,42 +43,47 @@ def process_image(image_path):
|
|
| 43 |
else:
|
| 44 |
depth_image = np.zeros_like(output, dtype='uint8') # Handle empty output
|
| 45 |
|
| 46 |
-
|
| 47 |
-
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
| 48 |
-
except Exception:
|
| 49 |
-
gltf_path = create_3d_obj(np.array(image), depth_image, image_path, depth=8)
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
def create_3d_obj(rgb_image, depth_image, image_path
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
|
| 69 |
mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 70 |
-
pcd, depth=
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
title = "Zero-shot Depth Estimation with DPT + 3D Model Preview"
|
| 84 |
description = "Upload an image to generate a depth map and reconstruct a 3D model in .gltf format."
|
|
@@ -109,3 +114,4 @@ if __name__ == "__main__":
|
|
| 109 |
|
| 110 |
|
| 111 |
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
depth_image = np.zeros_like(output, dtype='uint8') # Handle empty output
|
| 45 |
|
| 46 |
+
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
if gltf_path and Path(gltf_path).exists():
|
| 49 |
+
return Image.fromarray(depth_image), gltf_path, gltf_path
|
| 50 |
+
else:
|
| 51 |
+
return Image.fromarray(depth_image), None, "3D model generation failed"
|
| 52 |
|
| 53 |
+
def create_3d_obj(rgb_image, depth_image, image_path):
|
| 54 |
+
try:
|
| 55 |
+
depth_o3d = o3d.geometry.Image(depth_image)
|
| 56 |
+
image_o3d = o3d.geometry.Image(rgb_image)
|
| 57 |
+
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
| 58 |
+
image_o3d, depth_o3d, convert_rgb_to_intensity=False)
|
| 59 |
|
| 60 |
+
w, h = depth_image.shape[1], depth_image.shape[0]
|
| 61 |
+
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 62 |
+
camera_intrinsic.set_intrinsics(w, h, 500, 500, w / 2, h / 2)
|
| 63 |
|
| 64 |
+
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 65 |
+
pcd.estimate_normals(
|
| 66 |
+
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
| 67 |
+
pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))
|
| 68 |
|
|
|
|
| 69 |
mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 70 |
+
pcd, depth=10, width=0, scale=1.1, linear_fit=True)
|
| 71 |
|
| 72 |
+
if not mesh_raw.has_triangles():
|
| 73 |
+
return None # Mesh generation failed
|
| 74 |
+
|
| 75 |
+
# Center the mesh for better preview
|
| 76 |
+
bbox = pcd.get_axis_aligned_bounding_box()
|
| 77 |
+
mesh_raw.translate(-bbox.get_center())
|
| 78 |
|
| 79 |
+
# Save the 3D model
|
| 80 |
+
gltf_path = f'./{image_path.stem}.gltf'
|
| 81 |
+
o3d.io.write_triangle_mesh(gltf_path, mesh_raw, write_triangle_uvs=True)
|
| 82 |
+
return gltf_path
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"3D model generation failed: {e}")
|
| 86 |
+
return None
|
| 87 |
|
| 88 |
title = "Zero-shot Depth Estimation with DPT + 3D Model Preview"
|
| 89 |
description = "Upload an image to generate a depth map and reconstruct a 3D model in .gltf format."
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
+
|