Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +30 -12
- requirements.txt +0 -0
app.py
CHANGED
|
@@ -1,35 +1,53 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
from transformers import pipeline
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
-
|
| 9 |
depth_estimator = pipeline(task="depth-estimation", model="Intel/dpt-hybrid-midas")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
out = depth_estimator(input_image)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
prediction = torch.nn.functional.interpolate(
|
| 17 |
out["predicted_depth"].unsqueeze(1),
|
| 18 |
-
size=input_image.size[::-1],
|
| 19 |
mode="bicubic",
|
| 20 |
align_corners=False,
|
| 21 |
)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
output = prediction.squeeze().numpy()
|
| 25 |
formatted = (output * 255 / np.max(output)).astype("uint8")
|
|
|
|
|
|
|
| 26 |
depth = Image.fromarray(formatted)
|
| 27 |
return depth
|
| 28 |
|
| 29 |
-
|
| 30 |
-
iface = gr.Interface(
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from typing import Any
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
| 7 |
from transformers import pipeline
|
| 8 |
from PIL import Image
|
| 9 |
|
| 10 |
+
# Load the depth estimation model from Hugging Face Transformers
|
| 11 |
depth_estimator = pipeline(task="depth-estimation", model="Intel/dpt-hybrid-midas")
|
| 12 |
|
| 13 |
+
def launch(input_image: Image.Image) -> Image.Image:
|
| 14 |
+
"""
|
| 15 |
+
Process an input image to estimate its depth map.
|
| 16 |
|
| 17 |
+
Args:
|
| 18 |
+
input_image: An image object as received from the Gradio interface.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
A PIL Image object representing the depth map.
|
| 22 |
+
"""
|
| 23 |
+
# Generate depth estimation from the input image
|
| 24 |
out = depth_estimator(input_image)
|
| 25 |
|
| 26 |
+
# Resize the prediction to match the input image size
|
| 27 |
prediction = torch.nn.functional.interpolate(
|
| 28 |
out["predicted_depth"].unsqueeze(1),
|
| 29 |
+
size=input_image.size[::-1], # PIL images use width x height, whereas torch uses height x width
|
| 30 |
mode="bicubic",
|
| 31 |
align_corners=False,
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# Normalize the prediction to be in the range [0, 255]
|
| 35 |
output = prediction.squeeze().numpy()
|
| 36 |
formatted = (output * 255 / np.max(output)).astype("uint8")
|
| 37 |
+
|
| 38 |
+
# Convert the numpy array back to a PIL image
|
| 39 |
depth = Image.fromarray(formatted)
|
| 40 |
return depth
|
| 41 |
|
| 42 |
+
# Define the Gradio interface
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=launch,
|
| 45 |
+
inputs=gr.inputs.Image(type='pil'),
|
| 46 |
+
outputs=gr.outputs.Image(type='pil'),
|
| 47 |
+
title="Depth Estimation",
|
| 48 |
+
description="Upload an image to estimate its depth map."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Launch the Gradio app with sharing option enabled
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
|
File without changes
|