besarismaili commited on
Commit
e7bba1f
·
1 Parent(s): 0d80bc9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
8
+
9
+ feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
10
+ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
11
+
12
+ def process_image(image):
13
+ # prepare image for the model
14
+ encoding = feature_extractor(image, return_tensors="pt")
15
+
16
+ # forward pass
17
+ with torch.no_grad():
18
+ outputs = model(**encoding)
19
+ predicted_depth = outputs.predicted_depth
20
+
21
+ # interpolate to original size
22
+ prediction = torch.nn.functional.interpolate(
23
+ predicted_depth.unsqueeze(1),
24
+ size=image.size[::-1],
25
+ mode="bicubic",
26
+ align_corners=False,
27
+ ).squeeze()
28
+ output = prediction.cpu().numpy()
29
+ formatted = (output * 255 / np.max(output)).astype('uint8')
30
+ img = Image.fromarray(formatted)
31
+ return img
32
+
33
+ return result
34
+
35
+ title = "Demo: zero-shot depth estimation with DPT"
36
+ description = "Demo for Intel's DPT, a Dense Prediction Transformer for state-of-the-art dense prediction tasks such as semantic segmentation and depth estimation."
37
+ examples =[['cats.jpg']]
38
+
39
+ iface = gr.Interface(fn=process_image,
40
+ inputs=gr.inputs.Image(type="pil"),
41
+ outputs=gr.outputs.Image(type="pil", label="predicted depth"),
42
+ title=title,
43
+ description=description,
44
+ examples=examples,
45
+ enable_queue=True)
46
+ iface.launch(debug=True)