codic commited on
Commit
08736fe
·
verified ·
1 Parent(s): 65fdc29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -3,6 +3,7 @@ import diffusers
3
  import torch
4
  from PIL import Image
5
  import os
 
6
 
7
  if os.environ.get("SPACES_ZERO_GPU") is not None:
8
  import spaces
@@ -48,23 +49,34 @@ def predict_depth(image: Image.Image):
48
  vis = pipe.image_processor.visualize_depth(depth.prediction)
49
  uncertainty = pipe.image_processor.visualize_uncertainty(depth.uncertainty)
50
 
51
- # Convert depth and uncertainty maps to images
52
- depth_image = vis[0]
 
 
 
 
 
 
 
 
 
 
53
  uncertainty_image = uncertainty[0]
54
 
55
- # Return both depth and uncertainty images
56
- return depth_image, uncertainty_image
57
 
58
  # Create Gradio interface for depth prediction
59
  iface = gr.Interface(
60
  fn=predict_depth,
61
  inputs=gr.Image(type="pil", label="Upload Image or Provide URL"),
62
  outputs=[
63
- gr.Image(type="pil", label="Predicted Depth Map"),
64
- gr.Image(type="pil", label="Uncertainty Map")
 
65
  ],
66
  title="Depth Prediction with Marigold",
67
- description="Upload an image or provide a URL to generate its depth map and uncertainty visualization using the Marigold Depth model. The prediction includes both a depth map and an uncertainty map showing the model's confidence."
68
  )
69
 
70
  # Launch the Gradio interface
 
3
  import torch
4
  from PIL import Image
5
  import os
6
+ import numpy as np
7
 
8
  if os.environ.get("SPACES_ZERO_GPU") is not None:
9
  import spaces
 
49
  vis = pipe.image_processor.visualize_depth(depth.prediction)
50
  uncertainty = pipe.image_processor.visualize_uncertainty(depth.uncertainty)
51
 
52
+ # Convert depth map to grayscale for more consistent output
53
+ depth_image = vis[0].convert("L") # Convert to grayscale (mode "L")
54
+
55
+ # Normalize and handle the depth map
56
+ depth_array = np.array(depth_image)
57
+ depth_array = np.clip(depth_array, 0, 255) # Normalize depth range to 0-255
58
+
59
+ # Save depth map as .npy file
60
+ npy_filename = "depth_map.npy"
61
+ np.save(npy_filename, depth_array) # Save as .npy file
62
+
63
+ # Convert uncertainty map to image
64
  uncertainty_image = uncertainty[0]
65
 
66
+ # Return both depth image, uncertainty image, and .npy file path
67
+ return depth_image, uncertainty_image, npy_filename
68
 
69
  # Create Gradio interface for depth prediction
70
  iface = gr.Interface(
71
  fn=predict_depth,
72
  inputs=gr.Image(type="pil", label="Upload Image or Provide URL"),
73
  outputs=[
74
+ gr.Image(type="pil", label="Predicted Depth Map"),
75
+ gr.Image(type="pil", label="Uncertainty Map"),
76
+ gr.File(label="Download Depth Map (.npy)") # Output file for .npy
77
  ],
78
  title="Depth Prediction with Marigold",
79
+ description="Upload an image or provide a URL to generate its depth map, uncertainty visualization, and save the depth map as a .npy file."
80
  )
81
 
82
  # Launch the Gradio interface