File size: 2,476 Bytes
e7bba1f
 
 
 
 
4b82956
 
e7bba1f
 
 
 
4b82956
e7bba1f
 
 
 
 
 
 
4b82956
e7bba1f
 
 
 
 
 
 
 
 
 
 
4b82956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import gradio as gr
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
import torch
import numpy as np
from PIL import Image
import os
import cv2

feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")

def get_image_depth(image):
    # prepare image for the model
    encoding = feature_extractor(image, return_tensors="pt")
    
    # forward pass
    with torch.no_grad():
       outputs = model(**encoding)
       predicted_depth = outputs.predicted_depth

    # interpolate to original size
    prediction = torch.nn.functional.interpolate(
                        predicted_depth.unsqueeze(1),
                        size=image.size[::-1],
                        mode="bicubic",
                        align_corners=False,
                 ).squeeze()
    output = prediction.cpu().numpy()
    formatted = (output * 255 / np.max(output)).astype('uint8')
    img = Image.fromarray(formatted)
    return img


def process_sequence(files):
    file_paths = [file.name for file in files]
    for file_path in file_paths:
        image = Image.open(file_path)
        depth_image = get_image_depth(image)
        depth_image.save(os.path.join('output', os.path.basename(file_path)))
    return file_paths, gr.Info("This is some info")

title = "# Depth estimation demo"
description = "Demo for Intel's DPT"

with gr.Blocks() as iface:

    gr.Markdown(title)
    gr.Markdown(description)

    with gr.Row():

        with gr.Column():
            with gr.Tab(label='Singel image'):
                image = gr.Image(type="pil")
                button = gr.Button(value="Get depth", interactive=True, variant="primary")
                image_output=gr.Image(type="pil", label="predicted depth")
        
        with gr.Column():
            with gr.Tab(label='Frames'):
                file_output = gr.File(visible=False)
                upload_button = gr.UploadButton("Select directory", file_types=["image"], file_count="directory")
                upload_button.upload(process_sequence, upload_button, file_output)
                
                #output=gr.Video(label="Predicted Depth")
                message=gr.Text(value="Check output folder for the depth frames.")
        
    button.click(
        fn=get_image_depth,
        inputs=[image],
        outputs=[image_output]
    )

iface.queue(concurrency_count=1)
iface.launch(debug=True, enable_queue=True)