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

Update app.py

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