Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,7 +48,7 @@ def process_image(image_path):
|
|
| 48 |
except Exception:
|
| 49 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path, depth=8)
|
| 50 |
|
| 51 |
-
return [Image.fromarray(depth_image), gltf_path
|
| 52 |
|
| 53 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
| 54 |
depth_o3d = o3d.geometry.Image(depth_image)
|
|
@@ -60,13 +60,11 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 60 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 61 |
camera_intrinsic.set_intrinsics(w, h, 500, 500, w / 2, h / 2)
|
| 62 |
|
| 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 |
-
|
| 70 |
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
|
| 71 |
mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 72 |
pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
|
|
@@ -74,7 +72,6 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 74 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
|
| 75 |
mesh = mesh_raw.simplify_vertex_clustering(voxel_size=voxel_size)
|
| 76 |
|
| 77 |
-
|
| 78 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 79 |
mesh_crop = mesh.crop(bbox)
|
| 80 |
|
|
@@ -86,22 +83,29 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 86 |
title = "Zero-shot Depth Estimation with DPT + 3D Point Cloud"
|
| 87 |
description = "DPT model predicts depth from an image, followed by 3D Point Cloud reconstruction."
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
gr.Image(label="Predicted Depth", type="pil")
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
|
|
|
|
| 48 |
except Exception:
|
| 49 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path, depth=8)
|
| 50 |
|
| 51 |
+
return [Image.fromarray(depth_image), gltf_path]
|
| 52 |
|
| 53 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
| 54 |
depth_o3d = o3d.geometry.Image(depth_image)
|
|
|
|
| 60 |
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
| 61 |
camera_intrinsic.set_intrinsics(w, h, 500, 500, w / 2, h / 2)
|
| 62 |
|
|
|
|
| 63 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 64 |
pcd.estimate_normals(
|
| 65 |
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
| 66 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0., 0., 1000.]))
|
| 67 |
|
|
|
|
| 68 |
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
|
| 69 |
mesh_raw, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
| 70 |
pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
|
|
|
|
| 72 |
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
|
| 73 |
mesh = mesh_raw.simplify_vertex_clustering(voxel_size=voxel_size)
|
| 74 |
|
|
|
|
| 75 |
bbox = pcd.get_axis_aligned_bounding_box()
|
| 76 |
mesh_crop = mesh.crop(bbox)
|
| 77 |
|
|
|
|
| 83 |
title = "Zero-shot Depth Estimation with DPT + 3D Point Cloud"
|
| 84 |
description = "DPT model predicts depth from an image, followed by 3D Point Cloud reconstruction."
|
| 85 |
|
| 86 |
+
with gr.Blocks() as iface:
|
| 87 |
+
gr.Markdown("# Zero-shot Depth Estimation with DPT + 3D Point Cloud")
|
| 88 |
+
with gr.Row():
|
| 89 |
+
image_input = gr.Image(type="filepath", label="Input Image")
|
| 90 |
+
depth_output = gr.Image(label="Predicted Depth", type="pil")
|
| 91 |
+
gltf_output = gr.File(label="Download 3D gLTF")
|
| 92 |
+
|
| 93 |
+
# Embed an iframe for previewing the .gltf
|
| 94 |
+
with gr.Row():
|
| 95 |
+
gr.HTML('<iframe id="gltf-viewer" width="100%" height="400px"></iframe>')
|
| 96 |
+
|
| 97 |
+
def update_gltf_viewer(image_path):
|
| 98 |
+
gltf_path = process_image(image_path)[1]
|
| 99 |
+
iframe_html = f'''
|
| 100 |
+
<script>
|
| 101 |
+
document.getElementById('gltf-viewer').src = 'https://gltf-viewer.donmccurdy.com/?url=file://{gltf_path}';
|
| 102 |
+
</script>
|
| 103 |
+
'''
|
| 104 |
+
return process_image(image_path)[0], gltf_path, iframe_html
|
| 105 |
+
|
| 106 |
+
image_input.change(update_gltf_viewer, inputs=[image_input], outputs=[depth_output, gltf_output, gr.HTML()])
|
| 107 |
+
|
| 108 |
+
iface.launch()
|
| 109 |
|
| 110 |
|
| 111 |
|