Views
No views yet
1from transformers import DPTImageProcessor, DPTForDepthEstimation
2import torch
3import numpy as np
4from PIL import Image
5import requests
6
7url = "http://images.cocodataset.org/val2017/000000039769.jpg"
8image = Image.open(requests.get(url, stream=True).raw)
9
10processor = DPTImageProcessor.from_pretrained("Intel/dpt-beit-base-384")
11model = DPTForDepthEstimation.from_pretrained("Intel/dpt-beit-base-384")
12
13# prepare image for the model
14inputs = processor(images=image, return_tensors="pt")
15
16with torch.no_grad():
17 outputs = model(**inputs)
18 predicted_depth = outputs.predicted_depth
19
20# interpolate to original size
21prediction = torch.nn.functional.interpolate(
22 predicted_depth.unsqueeze(1),
23 size=image.size[::-1],
24 mode="bicubic",
25 align_corners=False,
26)
27
28# visualize the prediction
29output = prediction.squeeze().cpu().numpy()
30formatted = (output * 255 / np.max(output)).astype("uint8")
31depth = Image.fromarray(formatted)1from transformers import pipeline
2
3pipe = pipeline(task="depth-estimation", model="Intel/dpt-beit-base-384")
4result = pipe("http://images.cocodataset.org/val2017/000000039769.jpg")
5result["depth"]