codic commited on
Commit
413b1a7
·
verified ·
1 Parent(s): fe3a7e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -124,13 +124,14 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
124
 
125
  depth_o3d = o3d.geometry.Image(combined_depth)
126
  image_o3d = o3d.geometry.Image(foreground)
 
 
127
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
128
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
129
  )
130
 
131
  # Get dimensions of the rgbd_image
132
- rgbd_width = rgbd_image.color.width
133
- rgbd_height = rgbd_image.color.height
134
 
135
  # Check if the depth and foreground sizes are compatible
136
  if rgbd_width != foreground.shape[1] or rgbd_height != foreground.shape[0]:
@@ -177,33 +178,26 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
177
  # Crop the mesh based on the bounding box
178
  mesh_crop = mesh.crop(bbox)
179
 
180
- # Write the final 3D mesh to a GLTF file
181
- gltf_path = f"./{image_path.stem}.gltf"
182
- o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
183
 
184
  return gltf_path
185
 
186
- # Gradio Interface
187
- title = "Depth Estimation & 3D Reconstruction Demo"
188
- description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
189
-
190
- examples = [["examples/" + img] for img in os.listdir("examples/")]
191
-
192
- iface = gr.Interface(
193
  fn=process_image,
194
  inputs=[
195
- gr.Image(type="filepath", label="Input Image"),
196
- gr.File(type="filepath", label="Input Depth Map (optional)"),
197
  ],
198
  outputs=[
199
- gr.Image(label="Predicted Depth", type="pil"),
200
- gr.Model3D(label="3D Mesh Reconstruction", clear_color=[1.0, 1.0, 1.0, 1.0]),
201
- gr.File(label="3D gLTF File"),
202
  ],
203
- title=title,
204
- description=description,
205
- examples=examples,
206
- allow_flagging="never",
207
- cache_examples=False,
208
  )
209
- iface.launch(debug=True, show_api=True, share=True)
 
 
 
124
 
125
  depth_o3d = o3d.geometry.Image(combined_depth)
126
  image_o3d = o3d.geometry.Image(foreground)
127
+
128
+ # Create RGBD image directly without using width and height
129
  rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
130
  image_o3d, depth_o3d, convert_rgb_to_intensity=False
131
  )
132
 
133
  # Get dimensions of the rgbd_image
134
+ rgbd_width, rgbd_height = rgbd_image.color.shape[1], rgbd_image.color.shape[0]
 
135
 
136
  # Check if the depth and foreground sizes are compatible
137
  if rgbd_width != foreground.shape[1] or rgbd_height != foreground.shape[0]:
 
178
  # Crop the mesh based on the bounding box
179
  mesh_crop = mesh.crop(bbox)
180
 
181
+ # Save the mesh in GLTF format
182
+ gltf_path = f"{image_path.stem}_model.gltf"
183
+ o3d.io.write_triangle_mesh(gltf_path, mesh_crop)
184
 
185
  return gltf_path
186
 
187
+ # Gradio interface
188
+ interface = gr.Interface(
 
 
 
 
 
189
  fn=process_image,
190
  inputs=[
191
+ gr.inputs.Image(type="filepath", label="Input Image"),
192
+ gr.inputs.File(label="Depth Map (.npy or .png)", optional=True),
193
  ],
194
  outputs=[
195
+ gr.outputs.Image(type="pil", label="Processed Depth Map"),
196
+ gr.outputs.File(label="3D Model (GLTF)", label="Download GLTF"),
 
197
  ],
198
+ title="3D Model Reconstruction",
199
+ description="Upload an image to generate a 3D model with depth estimation.",
 
 
 
200
  )
201
+
202
+ # Launch the interface
203
+ interface.launch()