Spaces:
Build error
Build error
examples and voxels version
Browse files
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Dpt Depth Estimation + 3D Voxels
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: Dpt Depth Estimation + 3D Voxels
|
| 3 |
+
emoji: 🧊
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 3 |
import torch
|
|
@@ -11,7 +12,8 @@ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
|
| 11 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 12 |
|
| 13 |
|
| 14 |
-
def process_image(image_path):
|
|
|
|
| 15 |
image_path = Path(image_path)
|
| 16 |
image_raw = Image.open(image_path)
|
| 17 |
image = image_raw.resize(
|
|
@@ -36,20 +38,16 @@ def process_image(image_path):
|
|
| 36 |
output = prediction.cpu().numpy()
|
| 37 |
depth_image = (output * 255 / np.max(output)).astype('uint8')
|
| 38 |
try:
|
| 39 |
-
gltf_path =
|
|
|
|
| 40 |
img = Image.fromarray(depth_image)
|
| 41 |
return [img, gltf_path, gltf_path]
|
| 42 |
except Exception as e:
|
| 43 |
-
gltf_path = create_3d_obj(
|
| 44 |
-
np.array(image), depth_image, image_path, depth=8)
|
| 45 |
-
img = Image.fromarray(depth_image)
|
| 46 |
-
return [img, gltf_path, gltf_path]
|
| 47 |
-
except:
|
| 48 |
print("Error reconstructing 3D model")
|
| 49 |
raise Exception("Error reconstructing 3D model")
|
| 50 |
|
| 51 |
|
| 52 |
-
def
|
| 53 |
depth_o3d = o3d.geometry.Image(depth_image)
|
| 54 |
image_o3d = o3d.geometry.Image(rgb_image)
|
| 55 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
@@ -79,38 +77,46 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
|
| 79 |
[0, 0, 1, 0],
|
| 80 |
[0, 0, 0, 1]])
|
| 81 |
|
| 82 |
-
print('
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
gltf_path = f'./{image_path.stem}.gltf'
|
| 98 |
-
o3d.io.write_triangle_mesh(
|
| 99 |
-
gltf_path, mesh_crop, write_triangle_uvs=True)
|
| 100 |
return gltf_path
|
| 101 |
|
| 102 |
|
| 103 |
-
title = "Demo: zero-shot depth estimation with DPT + 3D
|
| 104 |
-
description = "This demo is a variation from the original <a href='https://huggingface.co/spaces/nielsr/dpt-depth-estimation' target='_blank'>DPT Demo</a>. It uses the DPT model to predict the depth of an image and then
|
| 105 |
-
examples = [["examples/" + img] for img in os.listdir("examples/")]
|
| 106 |
|
| 107 |
iface = gr.Interface(fn=process_image,
|
| 108 |
-
inputs=[
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
title=title,
|
| 115 |
description=description,
|
| 116 |
examples=examples,
|
|
|
|
| 1 |
+
from email.policy import default
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 4 |
import torch
|
|
|
|
| 12 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 13 |
|
| 14 |
|
| 15 |
+
def process_image(image_path, voxel_s):
|
| 16 |
+
voxel_s = max(voxel_s/500, 0.0001)
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path)
|
| 19 |
image = image_raw.resize(
|
|
|
|
| 38 |
output = prediction.cpu().numpy()
|
| 39 |
depth_image = (output * 255 / np.max(output)).astype('uint8')
|
| 40 |
try:
|
| 41 |
+
gltf_path = create_3d_voxels_obj(
|
| 42 |
+
np.array(image), depth_image, image_path, voxel_s)
|
| 43 |
img = Image.fromarray(depth_image)
|
| 44 |
return [img, gltf_path, gltf_path]
|
| 45 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
print("Error reconstructing 3D model")
|
| 47 |
raise Exception("Error reconstructing 3D model")
|
| 48 |
|
| 49 |
|
| 50 |
+
def create_3d_voxels_obj(rgb_image, depth_image, image_path, voxel_s):
|
| 51 |
depth_o3d = o3d.geometry.Image(depth_image)
|
| 52 |
image_o3d = o3d.geometry.Image(rgb_image)
|
| 53 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
|
|
| 77 |
[0, 0, 1, 0],
|
| 78 |
[0, 0, 0, 1]])
|
| 79 |
|
| 80 |
+
print('voxels')
|
| 81 |
+
|
| 82 |
+
# ref https://towardsdatascience.com/how-to-automate-voxel-modelling-of-3d-point-cloud-with-python-459f4d43a227
|
| 83 |
+
voxel_size = round(
|
| 84 |
+
max(pcd.get_max_bound()-pcd.get_min_bound())*voxel_s, 10)
|
| 85 |
+
print("Voxel size", voxel_size, "voxel_s", voxel_s)
|
| 86 |
+
voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(
|
| 87 |
+
pcd, voxel_size=voxel_size)
|
| 88 |
+
voxels = voxel_grid.get_voxels()
|
| 89 |
+
|
| 90 |
+
vox_mesh = o3d.geometry.TriangleMesh()
|
| 91 |
+
for v in voxels:
|
| 92 |
+
cube = o3d.geometry.TriangleMesh.create_box(width=1, height=1, depth=1)
|
| 93 |
+
cube.paint_uniform_color(v.color)
|
| 94 |
+
cube.translate(v.grid_index, relative=False)
|
| 95 |
+
vox_mesh += cube
|
| 96 |
+
print(voxel_grid, vox_mesh)
|
| 97 |
+
|
| 98 |
gltf_path = f'./{image_path.stem}.gltf'
|
| 99 |
+
o3d.io.write_triangle_mesh(gltf_path, vox_mesh, write_triangle_uvs=True)
|
|
|
|
| 100 |
return gltf_path
|
| 101 |
|
| 102 |
|
| 103 |
+
title = "Demo: zero-shot depth estimation with DPT + 3D Voxels reconstruction"
|
| 104 |
+
description = "This demo is a variation from the original <a href='https://huggingface.co/spaces/nielsr/dpt-depth-estimation' target='_blank'>DPT Demo</a>. It uses the DPT model to predict the depth of an image and then reconstruct the 3D model as voxels."
|
| 105 |
+
examples = [["examples/" + img, 10] for img in os.listdir("examples/")]
|
| 106 |
|
| 107 |
iface = gr.Interface(fn=process_image,
|
| 108 |
+
inputs=[
|
| 109 |
+
gr.inputs.Image(
|
| 110 |
+
type="filepath", label="Input Image"),
|
| 111 |
+
gr.inputs.Slider(
|
| 112 |
+
5, 100, step=1, label="Voxel Size", default=10)
|
| 113 |
+
],
|
| 114 |
+
outputs=[
|
| 115 |
+
gr.outputs.Image(label="predicted depth", type="pil"),
|
| 116 |
+
gr.outputs.Image3D(label="3d mesh reconstruction", clear_color=[
|
| 117 |
+
1.0, 1.0, 1.0, 1.0]),
|
| 118 |
+
gr.outputs.File(label="3d gLTF")
|
| 119 |
+
],
|
| 120 |
title=title,
|
| 121 |
description=description,
|
| 122 |
examples=examples,
|
examples/1-tim-gouw-JsjXnWlh8-g-unsplash.jpg
ADDED
|
examples/jeremiah-del-mar-6wEM5ZJWVDQ-unsplash.jpg
DELETED
|
Binary file (129 kB)
|
|
|
examples/suheyl-burak-AwKokEFkLhM-unsplash.jpg
ADDED
|