Spaces:
Running
on
Zero
Running
on
Zero
Charlie Amalet
commited on
Commit
·
ae4bd74
1
Parent(s):
b6b254b
Upload mesh.py
Browse files
mesh.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from zoedepth.utils.geometry import depth_to_points, create_triangles
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import trimesh
|
| 6 |
+
from functools import partial
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
def depth_edges_mask(depth):
|
| 10 |
+
"""Returns a mask of edges in the depth map.
|
| 11 |
+
Args:
|
| 12 |
+
depth: 2D numpy array of shape (H, W) with dtype float32.
|
| 13 |
+
Returns:
|
| 14 |
+
mask: 2D numpy array of shape (H, W) with dtype bool.
|
| 15 |
+
"""
|
| 16 |
+
# Compute the x and y gradients of the depth map.
|
| 17 |
+
depth_dx, depth_dy = np.gradient(depth)
|
| 18 |
+
# Compute the gradient magnitude.
|
| 19 |
+
depth_grad = np.sqrt(depth_dx ** 2 + depth_dy ** 2)
|
| 20 |
+
# Compute the edge mask.
|
| 21 |
+
mask = depth_grad > 0.05
|
| 22 |
+
return mask
|
| 23 |
+
|
| 24 |
+
def predict_depth(model, image):
|
| 25 |
+
depth = model.infer_pil(image)
|
| 26 |
+
return depth
|
| 27 |
+
|
| 28 |
+
def get_mesh(model, image: Image.Image, keep_edges=True):
|
| 29 |
+
image.thumbnail((1024,1024)) # limit the size of the input image
|
| 30 |
+
|
| 31 |
+
depth = predict_depth(model, image)
|
| 32 |
+
pts3d = depth_to_points(depth[None])
|
| 33 |
+
pts3d = pts3d.reshape(-1, 3)
|
| 34 |
+
|
| 35 |
+
# Create a trimesh mesh from the points
|
| 36 |
+
# Each pixel is connected to its 4 neighbors
|
| 37 |
+
# colors are the RGB values of the image
|
| 38 |
+
|
| 39 |
+
verts = pts3d.reshape(-1, 3)
|
| 40 |
+
image = np.array(image)
|
| 41 |
+
if keep_edges:
|
| 42 |
+
triangles = create_triangles(image.shape[0], image.shape[1])
|
| 43 |
+
else:
|
| 44 |
+
triangles = create_triangles(image.shape[0], image.shape[1], mask=~depth_edges_mask(depth))
|
| 45 |
+
colors = image.reshape(-1, 3)
|
| 46 |
+
mesh = trimesh.Trimesh(vertices=verts, faces=triangles, vertex_colors=colors)
|
| 47 |
+
|
| 48 |
+
# Save as glb
|
| 49 |
+
glb_file = tempfile.NamedTemporaryFile(suffix='.glb', delete=False)
|
| 50 |
+
glb_path = glb_file.name
|
| 51 |
+
mesh.export(glb_path)
|
| 52 |
+
return glb_path
|
| 53 |
+
|
| 54 |
+
def mesh_interface(model):
|
| 55 |
+
with gr.Row():
|
| 56 |
+
with gr.Column():
|
| 57 |
+
inputs=[gr.Image(label="Input Image", type='pil'), gr.Checkbox(label="Keep occlusion edges", value=True)]
|
| 58 |
+
outputs=gr.Model3D(label="3D Mesh", clear_color=[1.0, 1.0, 1.0, 1.0])
|
| 59 |
+
generate_btn = gr.Button(value="Generate")
|
| 60 |
+
generate_btn.click(partial(get_mesh, model), inputs=inputs, outputs=outputs, api_name="generate_mesh")
|