yuragoithf commited on
Commit
f963ab3
·
1 Parent(s): 461002c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -17
app.py CHANGED
@@ -1,22 +1,45 @@
1
- import os, io
2
  import gradio as gr
3
- from transformers import pipeline
 
 
 
4
 
5
 
6
- SECRET_TOKEN = os.getenv("SECRET_TOKEN")
7
- headers = {"Authorization": f'Bearer {SECRET_TOKEN}'}
8
 
9
- estimator = pipeline("depth-estimation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- def rb(img):
12
- result = estimator(img)
13
- return result["depth"]
14
-
15
- description = """Upload an image and get the depth visualization"""
16
- title = """Depth Estimation"""
17
- examples=[["house.jpg"], ["plane.webp"], ["room.webp"]]
18
- inputs = gr.inputs.Image(type="pil", label="Upload an image")
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)