aliabd HF Staff commited on
Commit
5a357f9
·
1 Parent(s): 4663d40

Delete app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +0 -118
app.py DELETED
@@ -1,118 +0,0 @@
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
-
51
- def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
52
- depth_o3d = o3d.geometry.Image(depth_image)
53
- image_o3d = o3d.geometry.Image(rgb_image)
54
- rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
55
- image_o3d, depth_o3d, convert_rgb_to_intensity=False)
56
- w = int(depth_image.shape[1])
57
- h = int(depth_image.shape[0])
58
-
59
- camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
60
- camera_intrinsic.set_intrinsics(w, h, 500, 500, w/2, h/2)
61
-
62
- pcd = o3d.geometry.PointCloud.create_from_rgbd_image(
63
- rgbd_image, camera_intrinsic)
64
-
65
- print('normals')
66
- pcd.normals = o3d.utility.Vector3dVector(
67
- np.zeros((1, 3))) # invalidate existing normals
68
- pcd.estimate_normals(
69
- search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.01, max_nn=30))
70
- pcd.orient_normals_towards_camera_location(
71
- camera_location=np.array([0., 0., 1000.]))
72
- pcd.transform([[1, 0, 0, 0],
73
- [0, -1, 0, 0],
74
- [0, 0, -1, 0],
75
- [0, 0, 0, 1]])
76
- pcd.transform([[-1, 0, 0, 0],
77
- [0, 1, 0, 0],
78
- [0, 0, 1, 0],
79
- [0, 0, 0, 1]])
80
-
81
- print('run Poisson surface reconstruction')
82
- with o3d.utility.VerbosityContextManager(o3d.utility.VerbosityLevel.Debug) as cm:
83
- mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
84
- pcd, depth=depth, width=0, scale=1.1, linear_fit=True)
85
-
86
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 256
87
- print(f'voxel_size = {voxel_size:e}')
88
- mesh = mesh_raw.simplify_vertex_clustering(
89
- voxel_size=voxel_size,
90
- contraction=o3d.geometry.SimplificationContraction.Average)
91
-
92
- # vertices_to_remove = densities < np.quantile(densities, 0.001)
93
- # mesh.remove_vertices_by_mask(vertices_to_remove)
94
- bbox = pcd.get_axis_aligned_bounding_box()
95
- mesh_crop = mesh.crop(bbox)
96
- gltf_path = f'./{image_path.stem}.gltf'
97
- o3d.io.write_triangle_mesh(
98
- gltf_path, mesh_crop, write_triangle_uvs=True)
99
- return gltf_path
100
-
101
- title = "Demo: zero-shot depth estimation with DPT + 3D Point Cloud"
102
- 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."
103
- examples = [["examples/1-jonathan-borba-CgWTqYxHEkg-unsplash.jpg"]]
104
-
105
- iface = gr.Interface(fn=process_image,
106
- inputs=[gr.Image(
107
- type="filepath", label="Input Image")],
108
- outputs=[gr.Image(label="predicted depth", type="pil"),
109
- gr.Model3D(label="3d mesh reconstruction", clear_color=[
110
- 1.0, 1.0, 1.0, 1.0]),
111
- gr.File(label="3d gLTF")],
112
- title=title,
113
- description=description,
114
- examples=examples,
115
- allow_flagging="never",
116
- cache_examples=False)
117
-
118
- iface.launch(debug=True, enable_queue=False)