Spaces:
Running
Running
import gradio as gr | |
import matplotlib.pyplot as plt | |
import cv2 | |
import torch | |
import timm | |
import numpy as np | |
midas = torch.hub.load('intel-isl/MiDaS', 'DPT_Hybrid') | |
midas.to('cpu') | |
midas.eval() | |
transforms = torch.hub.load('intel-isl/MiDaS', 'transforms') | |
transform = transforms.dpt_transform | |
def predict_image(img): | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
input_batch = transform(img).to('cpu') | |
with torch.no_grad(): | |
prediction = midas(input_batch) | |
prediction = torch.nn.functional.interpolate( | |
prediction.unsqueeze(1), | |
size=img.shape[:2], | |
mode="bicubic", | |
align_corners=False, | |
).squeeze() | |
img = prediction.cpu().numpy() | |
a = img.max() | |
img = (img / a)*255 | |
out = (img).astype(np.uint8) | |
return out | |
image = gr.inputs.Image() | |
label = gr.outputs.Label('ok') | |
gr.Interface(fn=predict_image, inputs=image, outputs=image).launch(debug='True') |