-
Notifications
You must be signed in to change notification settings - Fork 1
/
Depth-Estimation Python Code.py
54 lines (40 loc) · 1.34 KB
/
Depth-Estimation Python Code.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pip install torch torchvision opencv-python
pip install timm
import torch
import cv2
import matplotlib.pyplot as plt
import timm
model_type = "DPT_Large" # MiDaS v3 - Large model
midas = torch.hub.load("intel-isl/MiDaS", model_type)
midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")
transform = midas_transforms.dpt_transform
img_path = "example.jpg" # Put name of the image you want to use here
img = cv2.imread(img_path)
if img is None:
raise ValueError(f"Image at path '{img_path}' could not be loaded. Please check the file path and try again.")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
input_batch = transform(img).unsqueeze(0)
if len(input_batch.shape) == 5:
input_batch = input_batch.squeeze(0) # Remove the extra dimension
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
midas.to(device)
input_batch = input_batch.to(device)
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()
depth_map = prediction.cpu().numpy()
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(img)
plt.axis("off")
plt.subplot(1, 2, 2)
plt.title("Depth Map")
plt.imshow(depth_map, cmap="inferno")
plt.axis("off")
plt.show()