Spaces:
Sleeping
Sleeping
Commit
·
f963ab3
1
Parent(s):
461002c
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,45 @@
|
|
1 |
-
import os, io
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
|
|
|
|
|
|
4 |
|
5 |
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
description
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
outputs = gr.outputs.Image(type="pil",label="Output Image")
|
20 |
-
|
21 |
-
demo = gr.Interface(fn=rb, inputs=inputs, outputs=outputs, examples=examples, title=title, description=description)
|
22 |
-
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
|
7 |
|
8 |
+
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
9 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
10 |
|
11 |
+
def process_image(image):
|
12 |
+
# prepare image for the model
|
13 |
+
encoding = feature_extractor(image, return_tensors="pt")
|
14 |
+
|
15 |
+
# forward pass
|
16 |
+
with torch.no_grad():
|
17 |
+
outputs = model(**encoding)
|
18 |
+
predicted_depth = outputs.predicted_depth
|
19 |
+
|
20 |
+
# interpolate to original size
|
21 |
+
prediction = torch.nn.functional.interpolate(
|
22 |
+
predicted_depth.unsqueeze(1),
|
23 |
+
size=image.size[::-1],
|
24 |
+
mode="bicubic",
|
25 |
+
align_corners=False,
|
26 |
+
).squeeze()
|
27 |
+
output = prediction.cpu().numpy()
|
28 |
+
formatted = (output * 255 / np.max(output)).astype('uint8')
|
29 |
+
img = Image.fromarray(formatted)
|
30 |
+
return img
|
31 |
+
|
32 |
+
return result
|
33 |
+
|
34 |
+
title = "Depth Estimation"
|
35 |
+
description = "Upload an image and get the depth visualization"
|
36 |
+
examples =[["house.jpg"], ["plane.webp"], ["room.webp"]]
|
37 |
|
38 |
+
iface = gr.Interface(fn=process_image,
|
39 |
+
inputs=gr.inputs.Image(type="pil", label="Upload an image"),
|
40 |
+
outputs=gr.outputs.Image(type="pil", label="Predicted depth"),
|
41 |
+
title=title,
|
42 |
+
description=description,
|
43 |
+
examples=examples,
|
44 |
+
enable_queue=True)
|
45 |
+
iface.launch(debug=True)
|
|
|
|
|
|
|
|