Update app.py
Browse files
app.py
CHANGED
|
@@ -89,12 +89,12 @@ def process_image(image_path, depth_map_path=None):
|
|
| 89 |
try:
|
| 90 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path)
|
| 91 |
img = Image.fromarray(blended_depth_image)
|
| 92 |
-
return [img, gltf_path
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 95 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path, depth=9)
|
| 96 |
img = Image.fromarray(blended_depth_image)
|
| 97 |
-
return [img, gltf_path
|
| 98 |
|
| 99 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 100 |
# Resize background to match the original image size (no cropping)
|
|
@@ -171,39 +171,31 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
|
| 171 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 172 |
|
| 173 |
# Validate the mesh before cropping
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
raise RuntimeError("Bounding box is empty. Cannot crop mesh.")
|
| 177 |
|
| 178 |
-
#
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
gltf_path = f"{image_path.stem}_model.gltf"
|
| 183 |
-
o3d.io.write_triangle_mesh(gltf_path, mesh_crop)
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
title = "3D Reconstruction with DPT "
|
| 188 |
-
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."
|
| 189 |
-
examples = [["examples/" + img] for img in os.listdir("examples/")]
|
| 190 |
-
|
| 191 |
-
# Gradio interface
|
| 192 |
-
iface = gr.Interface(
|
| 193 |
fn=process_image,
|
| 194 |
inputs=[
|
| 195 |
-
gr.Image(
|
| 196 |
-
gr.
|
| 197 |
],
|
| 198 |
outputs=[
|
| 199 |
-
gr.Image(label="
|
| 200 |
-
gr.
|
| 201 |
-
gr.File(label="3D gLTF File"),
|
| 202 |
],
|
| 203 |
-
title=
|
| 204 |
-
description=
|
| 205 |
-
examples=examples,
|
| 206 |
-
allow_flagging="never",
|
| 207 |
-
cache_examples=False,
|
| 208 |
)
|
| 209 |
-
|
|
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path)
|
| 91 |
img = Image.fromarray(blended_depth_image)
|
| 92 |
+
return [img, gltf_path]
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
|
| 95 |
gltf_path = create_3d_obj(foreground_flat, blended_depth_image, bg_scaled, image_path, depth=9)
|
| 96 |
img = Image.fromarray(blended_depth_image)
|
| 97 |
+
return [img, gltf_path]
|
| 98 |
|
| 99 |
def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
|
| 100 |
# Resize background to match the original image size (no cropping)
|
|
|
|
| 171 |
mesh = mesh.filter_smooth_simple(number_of_iterations=3)
|
| 172 |
|
| 173 |
# Validate the mesh before cropping
|
| 174 |
+
if mesh.is_empty():
|
| 175 |
+
raise RuntimeError("The mesh is empty after processing.")
|
|
|
|
| 176 |
|
| 177 |
+
# Save mesh to GLTF format
|
| 178 |
+
output_dir = Path("output")
|
| 179 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 180 |
+
gltf_path = output_dir / f"{image_path.stem}.gltf"
|
| 181 |
+
o3d.io.write_triangle_mesh(str(gltf_path), mesh)
|
| 182 |
|
| 183 |
+
return str(gltf_path)
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
# Create Gradio interface
|
| 186 |
+
interface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
fn=process_image,
|
| 188 |
inputs=[
|
| 189 |
+
gr.inputs.Image(label="Upload Image"),
|
| 190 |
+
gr.inputs.File(label="Upload Depth Map (optional)", type="file")
|
| 191 |
],
|
| 192 |
outputs=[
|
| 193 |
+
gr.outputs.Image(label="Processed Image"),
|
| 194 |
+
gr.outputs.File(label="3D Model Output (GLTF)")
|
|
|
|
| 195 |
],
|
| 196 |
+
title="Depth Estimation and 3D Model Generation",
|
| 197 |
+
description="Upload an image and optionally a depth map to generate a 3D model."
|
|
|
|
|
|
|
|
|
|
| 198 |
)
|
| 199 |
+
|
| 200 |
+
if __name__ == "__main__":
|
| 201 |
+
interface.launch()
|