|
import gradio as gr |
|
from transformers import DPTFeatureExtractor, DPTForDepthEstimation |
|
import torch |
|
import numpy as np |
|
import cv2 |
|
from PIL import Image |
|
|
|
torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg') |
|
|
|
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large") |
|
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large") |
|
|
|
def write_depth(depth, bits): |
|
depth_min = depth.min() |
|
depth_max = depth.max() |
|
|
|
max_val = (2 ** (8 * bits)) - 1 |
|
|
|
if depth_max - depth_min > np.finfo("float").eps: |
|
out = max_val * (depth - depth_min) / (depth_max - depth_min) |
|
else: |
|
out = np.zeros(depth.shape, dtype=depth.dtype) |
|
|
|
return out/65536 |
|
|
|
def process_image(image): |
|
|
|
encoding = feature_extractor(image, return_tensors="pt") |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**encoding) |
|
predicted_depth = outputs.predicted_depth |
|
|
|
|
|
prediction = torch.nn.functional.interpolate( |
|
predicted_depth.unsqueeze(1), |
|
size=image.size[::-1], |
|
mode="bicubic", |
|
align_corners=False, |
|
) |
|
prediction = prediction.squeeze().cpu().numpy() |
|
|
|
|
|
write_depth(prediction, bits=2) |
|
|
|
result = Image.open("result.png") |
|
|
|
return result |
|
|
|
title = "Interactive demo: DPT" |
|
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." |
|
examples =[['cats.jpg']] |
|
|
|
iface = gr.Interface(fn=process_image, |
|
inputs=gr.inputs.Image(type="pil"), |
|
outputs=gr.outputs.Image(label="predicted depth"), |
|
title=title, |
|
description=description, |
|
examples=examples, |
|
enable_queue=True) |
|
iface.launch(debug=True) |