Spaces:
Runtime error
Runtime error
Commit ·
a092fbb
1
Parent(s): 41f13d5
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import GLPNFeatureExtractor, GLPNForDepthEstimation
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
| 10 |
+
# image = Image.open(requests.get(url, stream=True).raw)
|
| 11 |
+
|
| 12 |
+
feature_extractor = GLPNFeatureExtractor.from_pretrained("vinvino02/glpn-nyu")
|
| 13 |
+
model = GLPNForDepthEstimation.from_pretrained("vinvino02/glpn-nyu")
|
| 14 |
+
|
| 15 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 16 |
+
|
| 17 |
+
def predict(image):
|
| 18 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model(**inputs)
|
| 21 |
+
predicted_depth = outputs.predicted_depth
|
| 22 |
+
# interpolate to original size
|
| 23 |
+
prediction = torch.nn.functional.interpolate(
|
| 24 |
+
predicted_depth.unsqueeze(1),
|
| 25 |
+
size=image.size[::-1],
|
| 26 |
+
mode="bicubic",
|
| 27 |
+
align_corners=False,
|
| 28 |
+
)
|
| 29 |
+
# visualize the prediction
|
| 30 |
+
output = prediction.squeeze().cpu().numpy()
|
| 31 |
+
formatted = (output * 255 / np.max(output)).astype("uint8")
|
| 32 |
+
depth_image = Image.fromarray(formatted)
|
| 33 |
+
return depth_image
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Gradio App
|
| 37 |
+
title="Image Segmentation GAN"
|
| 38 |
+
description="This segments a Normal Image"
|
| 39 |
+
|
| 40 |
+
demo=gr.Interface(fn=predict,
|
| 41 |
+
inputs=gr.Image(type='pil'),
|
| 42 |
+
outputs=gr.Image(type='pil'),
|
| 43 |
+
title=title ,
|
| 44 |
+
examples=example_list,
|
| 45 |
+
description=description)
|
| 46 |
+
demo.launch(debug=False)
|