File size: 811 Bytes
81def50
a310278
02cc722
a310278
81def50
 
b3652fb
a310278
 
 
b3652fb
a310278
 
 
b3652fb
a310278
 
81def50
a310278
 
 
81def50
a310278
 
 
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
import torch
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
from PIL import Image
import requests

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load model và chuyển sang GPU
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large").to(device)

# Tải ảnh
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)

# Chuẩn bị input
inputs = feature_extractor(images=image, return_tensors="pt").to(device)

# Dự đoán depth map
with torch.no_grad():
    outputs = model(**inputs)

# Lấy output và chuyển về dạng ảnh
depth_map = outputs.predicted_depth
depth_map = depth_map.squeeze().cpu().numpy()