sudo-soldier commited on
Commit
854f993
·
verified ·
1 Parent(s): f93b7cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -5,6 +5,7 @@ import numpy as np
5
  from PIL import Image
6
  import open3d as o3d
7
  from pathlib import Path
 
8
 
9
  # Load model and feature extractor
10
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
@@ -43,10 +44,10 @@ def process_image(image_path):
43
  else:
44
  depth_image = np.zeros_like(output, dtype='uint8') # Handle empty output
45
 
46
- gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
47
 
48
- if gltf_path and Path(gltf_path).exists():
49
- return Image.fromarray(depth_image), gltf_path, gltf_path
50
  else:
51
  return Image.fromarray(depth_image), None, "3D model generation failed"
52
 
@@ -70,23 +71,33 @@ def create_3d_obj(rgb_image, depth_image, image_path):
70
  pcd, depth=10, width=0, scale=1.1, linear_fit=True)
71
 
72
  if not mesh_raw.has_triangles():
 
73
  return None # Mesh generation failed
74
 
75
  # Center the mesh for better preview
76
  bbox = pcd.get_axis_aligned_bounding_box()
77
  mesh_raw.translate(-bbox.get_center())
78
 
79
- # Save the 3D model
80
- gltf_path = f'./{image_path.stem}.gltf'
81
  o3d.io.write_triangle_mesh(gltf_path, mesh_raw, write_triangle_uvs=True)
82
- return gltf_path
 
 
 
 
 
 
 
 
 
83
 
84
  except Exception as e:
85
  print(f"3D model generation failed: {e}")
86
  return None
87
 
88
  title = "Zero-shot Depth Estimation with DPT + 3D Model Preview"
89
- description = "Upload an image to generate a depth map and reconstruct a 3D model in .gltf format."
90
 
91
  with gr.Blocks() as demo:
92
  gr.Markdown(f"## {title}")
@@ -99,8 +110,8 @@ with gr.Blocks() as demo:
99
 
100
  with gr.Column(scale=2):
101
  depth_output = gr.Image(label="Predicted Depth", type="pil")
102
- model_output = gr.Model3D(label="3D Model Preview (GLTF)")
103
- file_output = gr.File(label="Download 3D GLTF File")
104
 
105
  generate_button.click(fn=process_image, inputs=[image_input], outputs=[depth_output, model_output, file_output])
106
 
 
5
  from PIL import Image
6
  import open3d as o3d
7
  from pathlib import Path
8
+ import subprocess
9
 
10
  # Load model and feature extractor
11
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
 
44
  else:
45
  depth_image = np.zeros_like(output, dtype='uint8') # Handle empty output
46
 
47
+ glb_path = create_3d_obj(np.array(image), depth_image, image_path)
48
 
49
+ if glb_path and Path(glb_path).exists():
50
+ return Image.fromarray(depth_image), glb_path, glb_path
51
  else:
52
  return Image.fromarray(depth_image), None, "3D model generation failed"
53
 
 
71
  pcd, depth=10, width=0, scale=1.1, linear_fit=True)
72
 
73
  if not mesh_raw.has_triangles():
74
+ print("Mesh generation failed: No triangles in mesh")
75
  return None # Mesh generation failed
76
 
77
  # Center the mesh for better preview
78
  bbox = pcd.get_axis_aligned_bounding_box()
79
  mesh_raw.translate(-bbox.get_center())
80
 
81
+ # Save the 3D model as .gltf
82
+ gltf_path = str(Path.cwd() / f"{image_path.stem}.gltf")
83
  o3d.io.write_triangle_mesh(gltf_path, mesh_raw, write_triangle_uvs=True)
84
+
85
+ # Convert .gltf to .glb
86
+ glb_path = gltf_path.replace(".gltf", ".glb")
87
+ subprocess.run(["npx", "gltf-pipeline", "-i", gltf_path, "-o", glb_path])
88
+
89
+ if Path(glb_path).exists():
90
+ return glb_path
91
+ else:
92
+ print("GLB conversion failed.")
93
+ return None
94
 
95
  except Exception as e:
96
  print(f"3D model generation failed: {e}")
97
  return None
98
 
99
  title = "Zero-shot Depth Estimation with DPT + 3D Model Preview"
100
+ description = "Upload an image to generate a depth map and reconstruct a 3D model in .glb format."
101
 
102
  with gr.Blocks() as demo:
103
  gr.Markdown(f"## {title}")
 
110
 
111
  with gr.Column(scale=2):
112
  depth_output = gr.Image(label="Predicted Depth", type="pil")
113
+ model_output = gr.Model3D(label="3D Model Preview (GLB)")
114
+ file_output = gr.File(label="Download 3D GLB File")
115
 
116
  generate_button.click(fn=process_image, inputs=[image_input], outputs=[depth_output, model_output, file_output])
117