-
-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathmain_depth_prediction.py
More file actions
executable file
·184 lines (137 loc) · 6.12 KB
/
main_depth_prediction.py
File metadata and controls
executable file
·184 lines (137 loc) · 6.12 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env -S python3 -O
"""
* This file is part of PYSLAM
*
* Copyright (C) 2016-present Luigi Freda <luigi dot freda at gmail dot com>
*
* PYSLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PYSLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PYSLAM. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import cv2
import numpy as np
from pyslam.config import Config
from pyslam.utilities.logging import Printer
from pyslam.utilities.depth import (
depth2pointcloud,
img_from_depth,
filter_shadow_points,
PointCloud,
)
from pyslam.slam import PinholeCamera
from pyslam.slam.feature_tracker_shared import FeatureTrackerShared
from pyslam.depth_estimation.depth_estimator_factory import (
depth_estimator_factory,
DepthEstimatorType,
)
from pyslam.io.dataset_types import DatasetType, SensorType, DatasetEnvironmentType
from pyslam.io.dataset_factory import dataset_factory
from pyslam.local_features.feature_tracker import feature_tracker_factory, FeatureTrackerTypes
from pyslam.local_features.feature_tracker_configs import FeatureTrackerConfigs
from pyslam.config_parameters import Parameters
import torch
import time
from pyslam.viz.viewer3D import Viewer3D
if __name__ == "__main__":
config = Config()
dataset = dataset_factory(config)
cam = PinholeCamera(config)
tracker_config = FeatureTrackerConfigs.ORB2
tracker_config["num_features"] = 2000
print("tracker_config: ", tracker_config)
feature_tracker = feature_tracker_factory(**tracker_config)
# This is normally done by the Slam class we don't have here. We need to set the static field of the class Frame and FeatureTrackerShared.
FeatureTrackerShared.set_feature_tracker(feature_tracker)
# Select your depth estimator (see the file depth_estimator_configs.py).
depth_estimator_type = DepthEstimatorType.DEPTH_PRO
min_depth = 0
max_depth = 50 if dataset.environmentType() == DatasetEnvironmentType.OUTDOOR else 10
precision = torch.float16
depth_estimator = depth_estimator_factory(
depth_estimator_type=depth_estimator_type,
min_depth=min_depth,
max_depth=max_depth,
dataset_env_type=dataset.environmentType(),
precision=precision,
camera=cam,
)
Printer.green(f"Depth estimator: {depth_estimator_type.name}")
viewer3D = Viewer3D(scale=dataset.scale_viewer_3d)
show_directly_point_cloud_if_available = True
key_cv = None
is_paused = False # pause/resume on GUI
img_id = 0 # 180, 340, 400 # you can start from a desired frame id if needed
while not viewer3D.is_closed():
timestamp, img, img_right = None, None, None
if not is_paused:
if dataset.is_ok:
timestamp = dataset.getTimestamp() # get current timestamp
img = dataset.getImageColor(img_id)
img_right = (
dataset.getImageColorRight(img_id)
if dataset.sensor_type == SensorType.STEREO
else None
)
if img is not None:
print("----------------------------------------")
print(f"processing img {img_id}")
start_time = time.time()
depth_prediction, pts3d_prediction = depth_estimator.infer(img, img_right)
print(f"inference time: {time.time() - start_time}")
cv2.imshow("color image", img)
if img_right is not None:
cv2.imshow("color image right", img_right)
if pts3d_prediction is not None and show_directly_point_cloud_if_available:
# We have predicted a 3D point cloud.
print(f"got directly point cloud: {pts3d_prediction.points.shape}")
depth_img = img_from_depth(depth_prediction, img_min=0, img_max=max_depth)
cv2.imshow("depth prediction", depth_img)
# Draw directly the predicted points cloud
if viewer3D is not None:
viewer3D.draw_dense_geometry(point_cloud=pts3d_prediction)
else:
# We use the depth to build a 3D point cloud and visualize it.
# Filter depth
filter_depth = True # do you want to filter the depth?
if filter_depth:
depth_filtered = filter_shadow_points(depth_prediction, delta_depth=None)
else:
depth_filtered = depth_prediction
# Visualize depth map
depth_img = img_from_depth(depth_prediction, img_min=0, img_max=max_depth)
depth_filtered_img = img_from_depth(
depth_filtered, img_min=0, img_max=max_depth
)
# Visualize 3D point cloud
if viewer3D is not None:
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
point_cloud = depth2pointcloud(
depth_filtered, image_rgb, cam.fx, cam.fy, cam.cx, cam.cy, max_depth
)
viewer3D.draw_dense_geometry(point_cloud=point_cloud)
cv2.imshow("depth prediction", depth_img)
cv2.imshow("depth filtered", depth_filtered_img)
else:
time.sleep(0.1)
img_id += 1
else:
time.sleep(0.1)
# get keys
key_cv = cv2.waitKey(1) & 0xFF
if viewer3D is not None:
is_paused = viewer3D.is_paused()
if key_cv == ord("q"):
break
if viewer3D is not None:
viewer3D.quit()