dlaima commited on
Commit
29ae38d
·
verified ·
1 Parent(s): ea4b324

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -10,14 +10,26 @@ depth_estimator = pipeline(task="depth-estimation", model="Intel/dpt-hybrid-mida
10
 
11
  # Function to process the image and return depth map
12
  def launch(input_image):
13
- out = depth_estimator(input_image)
 
 
 
 
 
14
 
15
- predicted_depth = out["predicted_depth"].view(1, 1, 480, 640) # Assuming single image.
 
16
 
17
- # Resize the prediction to match the raw image size (H, W).
 
 
 
 
 
 
18
  prediction = torch.nn.functional.interpolate(
19
  predicted_depth,
20
- size=input_image.size[::-1], # Match raw image size (H, W).
21
  mode="bicubic",
22
  align_corners=False,
23
  )
@@ -25,15 +37,19 @@ def launch(input_image):
25
  # Normalize the prediction
26
  output = prediction.squeeze().numpy()
27
  formatted = (output * 255 / np.max(output)).astype("uint8")
 
 
28
  depth = Image.fromarray(formatted)
 
29
  return depth
30
 
31
  # Define the Gradio interface
32
  iface = gr.Interface(
33
- fn=launch,
34
- inputs=gr.Image(type='pil'),
35
- outputs=gr.Image(type='pil')
36
  )
37
 
38
  # Launch the interface
39
  iface.launch()
 
 
10
 
11
  # Function to process the image and return depth map
12
  def launch(input_image):
13
+ # Ensure the input image is in RGB mode
14
+ if input_image.mode != "RGB":
15
+ input_image = input_image.convert("RGB")
16
+
17
+ # Print input image details for debugging
18
+ print(f"Received image with size: {input_image.size}")
19
 
20
+ # Run depth estimation
21
+ out = depth_estimator(input_image)
22
 
23
+ # Check if the model output contains 'predicted_depth'
24
+ if "predicted_depth" in out:
25
+ predicted_depth = out["predicted_depth"].view(1, 1, 480, 640) # Assuming single image
26
+ else:
27
+ raise ValueError("Model output does not contain 'predicted_depth'.")
28
+
29
+ # Resize the prediction to match the raw image size (H, W)
30
  prediction = torch.nn.functional.interpolate(
31
  predicted_depth,
32
+ size=input_image.size[::-1], # Match raw image size (H, W)
33
  mode="bicubic",
34
  align_corners=False,
35
  )
 
37
  # Normalize the prediction
38
  output = prediction.squeeze().numpy()
39
  formatted = (output * 255 / np.max(output)).astype("uint8")
40
+
41
+ # Convert the depth map to an image
42
  depth = Image.fromarray(formatted)
43
+
44
  return depth
45
 
46
  # Define the Gradio interface
47
  iface = gr.Interface(
48
+ fn=launch,
49
+ inputs=gr.Image(type="pil"), # Ensure input is PIL image
50
+ outputs=gr.Image(type="pil") # Output is also in PIL format
51
  )
52
 
53
  # Launch the interface
54
  iface.launch()
55
+