codic commited on
Commit
450d66c
·
verified ·
1 Parent(s): d3a50c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -26
app.py CHANGED
@@ -11,7 +11,7 @@ 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(
@@ -19,23 +19,25 @@ def process_image(image_path):
19
  Image.Resampling.LANCZOS,
20
  )
21
 
22
- # prepare image for the model
23
- encoding = feature_extractor(image, return_tensors="pt")
24
-
25
- # forward pass
26
- with torch.no_grad():
27
- outputs = model(**encoding)
28
- predicted_depth = outputs.predicted_depth
29
-
30
- # interpolate to original size
31
- prediction = torch.nn.functional.interpolate(
32
- predicted_depth.unsqueeze(1),
33
- size=image.size[::-1],
34
- mode="bicubic",
35
- align_corners=False,
36
- ).squeeze()
37
- output = prediction.cpu().numpy()
38
- depth_image = (output * 255 / np.max(output)).astype("uint8")
 
 
39
  try:
40
  gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
41
  img = Image.fromarray(depth_image)
@@ -89,8 +91,6 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
89
  contraction=o3d.geometry.SimplificationContraction.Average,
90
  )
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"
@@ -98,17 +98,20 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=10):
98
  return gltf_path
99
 
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/" + img] for img in os.listdir("examples/")]
104
 
105
  iface = gr.Interface(
106
  fn=process_image,
107
- inputs=[gr.Image(type="filepath", label="Input Image")],
 
 
 
108
  outputs=[
109
- gr.Image(label="predicted depth", type="pil"),
110
- gr.Model3D(label="3d mesh reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0]),
111
- gr.File(label="3d gLTF"),
112
  ],
113
  title=title,
114
  description=description,
 
11
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
12
 
13
 
14
+ def process_image(image_path, depth_map_path=None):
15
  image_path = Path(image_path)
16
  image_raw = Image.open(image_path)
17
  image = image_raw.resize(
 
19
  Image.Resampling.LANCZOS,
20
  )
21
 
22
+ if depth_map_path:
23
+ # Load the user-provided depth map
24
+ depth_image = np.array(Image.open(depth_map_path))
25
+ else:
26
+ # Automatically generate the depth map using the model
27
+ encoding = feature_extractor(image, return_tensors="pt")
28
+ with torch.no_grad():
29
+ outputs = model(**encoding)
30
+ predicted_depth = outputs.predicted_depth
31
+
32
+ # Interpolate to original size
33
+ prediction = torch.nn.functional.interpolate(
34
+ predicted_depth.unsqueeze(1),
35
+ size=image.size[::-1],
36
+ mode="bicubic",
37
+ align_corners=False,
38
+ ).squeeze()
39
+ depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
40
+
41
  try:
42
  gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
43
  img = Image.fromarray(depth_image)
 
91
  contraction=o3d.geometry.SimplificationContraction.Average,
92
  )
93
 
 
 
94
  bbox = pcd.get_axis_aligned_bounding_box()
95
  mesh_crop = mesh.crop(bbox)
96
  gltf_path = f"./{image_path.stem}.gltf"
 
98
  return gltf_path
99
 
100
 
101
+ title = "Demo: Depth Estimation & 3D Reconstruction with DPT + Point Cloud"
102
+ description = "This demo allows users to provide an RGB image and optionally a depth map to create a 3D object. If no depth map is provided, the DPT model will generate it."
103
  examples = [["examples/" + img] for img in os.listdir("examples/")]
104
 
105
  iface = gr.Interface(
106
  fn=process_image,
107
+ inputs=[
108
+ gr.Image(type="filepath", label="Input Image"),
109
+ gr.Image(type="filepath", label="Input Depth Map (optional)", optional=True),
110
+ ],
111
  outputs=[
112
+ gr.Image(label="Predicted Depth", type="pil"),
113
+ gr.Model3D(label="3D Mesh Reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0]),
114
+ gr.File(label="3D gLTF File"),
115
  ],
116
  title=title,
117
  description=description,