File size: 930 Bytes
4d5612a
 
09a22a3
 
 
4d5612a
2d3a9b6
 
 
09a22a3
 
 
 
4d5612a
 
09a22a3
 
 
 
 
 
4d5612a
 
09a22a3
 
 
 
4d5612a
09a22a3
4d5612a
 
 
09a22a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import gradio as gr
from transformers import pipeline
import torch
import numpy as np
from PIL import Image


depth_estimator = pipeline(task="depth-estimation",
                        model="Intel/dpt-hybrid-midas")
if __name__ == "__main__":
    
    def launch(input_image):
        out = depth_estimator(input_image)

    # resize the prediction
        prediction = torch.nn.functional.interpolate(
            out["predicted_depth"].unsqueeze(1),
            size=input_image.size[::-1],
            mode="bicubic",
            align_corners=False,
        )

    # normalize the prediction
        output = prediction.squeeze().numpy()
        formatted = (output * 255 / np.max(output)).astype("uint8")
        depth = Image.fromarray(formatted)
        return depth

    iface = gr.Interface(launch,
                     inputs=gr.Image(type='pil'),
                     outputs=gr.Image(type='pil'))

    iface.launch()