|
import torch |
|
from transformers import DPTFeatureExtractor, DPTForDepthEstimation |
|
from PIL import Image |
|
import requests |
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large") |
|
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(device) |
|
|
|
|
|
url = "http://images.cocodataset.org/val2017/000000039769.jpg" |
|
image = Image.open(requests.get(url, stream=True).raw) |
|
|
|
|
|
inputs = feature_extractor(images=image, return_tensors="pt").to(device) |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
|
|
|
|
depth_map = outputs.predicted_depth |
|
depth_map = depth_map.squeeze().cpu().numpy() |
|
|