File size: 1,833 Bytes
6ddce99
679bbb3
 
 
 
 
02acfac
95d18d6
02acfac
679bbb3
 
 
 
02acfac
 
 
7f86340
02acfac
 
679bbb3
02acfac
 
679bbb3
3295ec4
 
02acfac
 
679bbb3
3295ec4
02acfac
3295ec4
02acfac
3295ec4
679bbb3
02acfac
 
bd0efcd
3295ec4
02acfac
 
 
 
76082d2
70d73db
3295ec4
02acfac
fc86955
ef9bcaa
3295ec4
 
76082d2
02acfac
fc40b5b
3295ec4
02acfac
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
from doctest import Example
import gradio as gr
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
import torch
import numpy as np
from PIL import Image
import open3d as o3d
from pathlib import Path
import os

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


def process_image(image_path):
    image_path = Path(image_path)
    image = Image.open(image_path)

    # 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()
    depth_image = (output * 255 / np.max(output)).astype("uint8")
    img = Image.fromarray(depth_image)
    return [img]



title = "Demo: zero-shot depth estimation with DPT + 3D Point Cloud"
description = "This demo is a variation from the original <a href='https://huggingface.co/spaces/nielsr/dpt-depth-estimation' target='_blank'>DPT Demo</a>. It uses the DPT model to predict the depth of an image and then uses 3D Point Cloud to create a 3D object."
examples = [["examples/" + img] for img in os.listdir("examples/")]

iface = gr.Interface(
    fn=process_image,
    inputs=[gr.inputs.Image(type="filepath", label="Input Image")],
    outputs=[gr.outputs.Image(type="pil", label="Predicted Depth")],
    title=title,
    description=description,
    examples=examples,
    allow_flagging="never",
    #cache_examples=False,
)
iface.launch(debug=True, show_api=False)