Spaces:
Runtime error
Runtime error
Jose M Delgado
commited on
Commit
•
bf29269
1
Parent(s):
985dbce
sample app
Browse files
app.py
CHANGED
@@ -1,7 +1,117 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import open3d as o3d
|
7 |
+
from pathlib import Path
|
8 |
|
9 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
10 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
11 |
|
12 |
+
def process_image(image_path):
|
13 |
+
image_path = Path(image_path)
|
14 |
+
image_raw = Image.open(image_path)
|
15 |
+
image = image_raw.resize(
|
16 |
+
(800, int(800 * image_raw.size[1] / image_raw.size[0])),
|
17 |
+
Image.Resampling.LANCZOS)
|
18 |
+
|
19 |
+
# prepare image for the model
|
20 |
+
encoding = feature_extractor(image, return_tensors="pt")
|
21 |
+
|
22 |
+
# forward pass
|
23 |
+
with torch.no_grad():
|
24 |
+
outputs = model(**encoding)
|
25 |
+
predicted_depth = outputs.predicted_depth
|
26 |
+
|
27 |
+
# interpolate to original size
|
28 |
+
prediction = torch.nn.functional.interpolate(
|
29 |
+
predicted_depth.unsqueeze(1),
|
30 |
+
size=image.size[::-1],
|
31 |
+
mode="bicubic",
|
32 |
+
align_corners=False,
|
33 |
+
).squeeze()
|
34 |
+
output = prediction.cpu().numpy()
|
35 |
+
depth_image = (output * 255 / np.max(output)).astype('uint8')
|
36 |
+
try:
|
37 |
+
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
38 |
+
img = Image.fromarray(depth_image)
|
39 |
+
return [img, gltf_path, gltf_path]
|
40 |
+
except Exception:
|
41 |
+
gltf_path = create_3d_obj(
|
42 |
+
np.array(image), depth_image, image_path, depth=8)
|
43 |
+
img = Image.fromarray(depth_image)
|
44 |
+
return [img, gltf_path, gltf_path]
|
45 |
+
except:
|
46 |
+
print("Error reconstructing 3D model")
|
47 |
+
raise Exception("Error reconstructing 3D model")
|
48 |
+
|
49 |
+
|
50 |
+
def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
|
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(
|
54 |
+
image_o3d, depth_o3d, convert_rgb_to_intensity=False)
|
55 |
+
w = int(depth_image.shape[1])
|
56 |
+
h = int(depth_image.shape[0])
|
57 |
+
|
58 |
+
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
59 |
+
camera_intrinsic.set_intrinsics(w, h, 500, 500, w/2, h/2)
|
60 |
+
|
61 |
+
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
|
62 |
+
rgbd_image, camera_intrinsic)
|
63 |
+
|
64 |
+
print('normals')
|
65 |
+
pcd.normals = o3d.utility.Vector3dVector(
|
66 |
+
np.zeros((1, 3))) # invalidate existing normals
|
67 |
+
pcd.estimate_normals(
|
68 |
+
search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
|
69 |
+
pcd.orient_normals_towards_camera_location(
|
70 |
+
camera_location=np.array([0., 0., 1000.]))
|
71 |
+
pcd.transform([[1, 0, 0, 0],
|
72 |
+
[0, -1, 0, 0],
|
73 |
+
[0, 0, -1, 0],
|
74 |
+
[0, 0, 0, 1]])
|
75 |
+
pcd.transform([[-1, 0, 0, 0],
|
76 |
+
[0, 1, 0, 0],
|
77 |
+
[0, 0, 1, 0],
|
78 |
+
[0, 0, 0, 1]])
|
79 |
+
|
80 |
+
print('run Poisson surface reconstruction')
|
81 |
+
with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug):
|
82 |
+
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
83 |
+
pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
|
84 |
+
|
85 |
+
voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
|
86 |
+
print(f'voxel_size = {voxel_size:e}')
|
87 |
+
mesh = mesh_raw.simplify_vertex_clustering(
|
88 |
+
voxel_size=voxel_size,
|
89 |
+
contraction=o3d.geometry.SimplificationContraction.Average)
|
90 |
+
|
91 |
+
# vertices_to_remove = densities < np.quantile(densities, 0.001)
|
92 |
+
# mesh.remove_vertices_by_mask(vertices_to_remove)
|
93 |
+
bbox = pcd.get_axis_aligned_bounding_box()
|
94 |
+
mesh_crop = mesh.crop(bbox)
|
95 |
+
gltf_path = f'./{image_path.stem}.gltf'
|
96 |
+
o3d.io.write_triangle_mesh(
|
97 |
+
gltf_path, mesh_crop, write_triangle_uvs=True)
|
98 |
+
return gltf_path
|
99 |
+
|
100 |
+
title = "Demo: zero-shot depth estimation with DPT + 3D Point Cloud"
|
101 |
+
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 uses 3D Point Cloud to create a 3D object."
|
102 |
+
examples = [["examples/1-jonathan-borba-CgWTqYxHEkg-unsplash.jpg"]]
|
103 |
+
|
104 |
+
iface = gr.Interface(fn=process_image,
|
105 |
+
inputs=[gr.Image(
|
106 |
+
type="filepath", label="Input Image")],
|
107 |
+
outputs=[gr.Image(label="predicted depth", type="pil"),
|
108 |
+
gr.Model3D(label="3d mesh reconstruction", clear_color=[
|
109 |
+
1.0, 1.0, 1.0, 1.0]),
|
110 |
+
gr.File(label="3d gLTF")],
|
111 |
+
title=title,
|
112 |
+
description=description,
|
113 |
+
examples=examples,
|
114 |
+
allow_flagging="never",
|
115 |
+
cache_examples=False)
|
116 |
+
|
117 |
+
iface.launch(debug=True, enable_queue=False)
|