-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslam_real.py
85 lines (68 loc) · 2.11 KB
/
slam_real.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import torch
import cv2
import numpy as np
import matplotlib.pyplot as plt
from torchvision.transforms import Compose, Resize, ToTensor, Normalize
from PIL import Image
# 1. Load MiDaS_small Model
midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")
midas.eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
midas.to(device)
# 2. Correct manual transforms
transform = Compose([
Resize((256, 256)),
ToTensor(),
Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])
# 3. Load Local Image
image_path = "C:/Users/user/Desktop/0000000000.png"
image = cv2.imread(image_path)
if image is None:
raise ValueError(f"Cannot read image at {image_path}. Check the path.")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# NumPy to PIL.Image
image_pil = Image.fromarray(image)
input_image = transform(image_pil).unsqueeze(0).to(device)
# 4. Predict depth
with torch.no_grad():
prediction = midas(input_image)
prediction = torch.nn.functional.interpolate(
prediction.unsqueeze(1),
size=image.shape[:2],
mode="bicubic",
align_corners=False,
).squeeze()
depth_map = prediction.cpu().numpy()
# 5. Normalize depth map
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
# 6. Visualize Depth Map
plt.figure(figsize=(10,5))
plt.subplot(1,2,1)
plt.imshow(image)
plt.title("Input Image")
plt.subplot(1,2,2)
plt.imshow(depth_map, cmap='plasma')
plt.title("Predicted Depth Map")
plt.colorbar()
plt.show()
# 7. Generate Point Cloud
fx = fy = 500
cx, cy = image.shape[1] / 2, image.shape[0] / 2
points = []
colors = []
for v in range(image.shape[0]):
for u in range(image.shape[1]):
Z = depth_map[v, u]
X = (u - cx) * Z / fx
Y = (v - cy) * Z / fy
points.append((X, Y, Z))
colors.append(image[v, u] / 255.0)
points = np.array(points)
colors = np.array(colors)
# 8. Visualize Point Cloud
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(points[::50,0], points[::50,1], points[::50,2], c=colors[::50], s=0.5)
ax.set_title("3D Mapping from Single Local Image")
plt.show()