Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Load the depth estimation model
|
| 9 |
+
depth_estimator = pipeline(task="depth-estimation", model="Intel/dpt-hybrid-midas")
|
| 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 |
+
)
|
| 24 |
+
|
| 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()
|