Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
| 14 |
-
|
| 15 |
-
input_image
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|