Kilos1 commited on
Commit
ab556ae
Β·
verified Β·
1 Parent(s): a3a7c0b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os
9
+
10
+ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
11
+ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
12
+
13
+ def process_image(image_path):
14
+ image_path = Path(image_path)
15
+ image_raw = Image.open(image_path)
16
+ image = image_raw.resize(
17
+ (800, int(800 * image_raw.size[1] / image_raw.size[0])),
18
+ Image.Resampling.LANCZOS)
19
+
20
+ # prepare image for the model
21
+ encoding = feature_extractor(image, return_tensors="pt")
22
+
23
+ # forward pass
24
+ with torch.no_grad():
25
+ outputs = model(**encoding)
26
+ predicted_depth = outputs.predicted_depth
27
+
28
+ # interpolate to original size
29
+ prediction = torch.nn.functional.interpolate(
30
+ predicted_depth.unsqueeze(1),
31
+ size=image.size[::-1],
32
+ mode="bicubic",
33
+ align_corners=False,
34
+ ).squeeze()
35
+ output = prediction.cpu().numpy()
36
+ depth_image = (output * 255 / np.max(output)).astype('uint8')
37
+ try:
38
+ gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
39
+ img = Image.fromarray(depth_image)
40
+ return [img, gltf_path, gltf_path]
41
+ except Exception as e:
42
+ gltf_path = create_3d_obj(
43
+ np.array(image), depth_image, image_path, depth=8)
44
+ img = Image.fromarray(depth_image)
45
+ return [img, gltf_path, gltf_path]
46
+ except:
47
+ print("Error reconstructing 3D model")
48
+ raise Exception("Error reconstructing 3D model")
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) as cm:
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/" + img] for img in os.listdir("examples/")]
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
+ iface.launch(debug=True, enable_queue=False)