-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_example.py
More file actions
193 lines (160 loc) · 8.42 KB
/
Copy pathapi_example.py
File metadata and controls
193 lines (160 loc) · 8.42 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
185
186
187
188
189
190
191
192
193
import sys
import os
import cv2
import numpy as np
# Ensure the root directory is in sys.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from stretch4_emulated_rgbd.api import (
get_emulated_rgbd_stream,
ValidityMaskManager,
DenseDepthImage,
create_point_cloud_from_depth
)
from stretch4_emulated_rgbd.shared_utils import get_arg_parser
from stretch4_emulated_rgbd import emulated_rgbd_config as config
def main():
parser = get_arg_parser("Stretch 4 Emulated RGB-D API Capabilities Demonstration")
args = parser.parse_args()
use_left = args.camera in ["left", "left_right", "all"]
use_right = args.camera in ["right", "left_right", "all"]
use_center = args.camera in ["center", "all"]
use_left_right = (args.camera == "left_right")
use_left_right_center = (args.camera == "all")
use_left_lidar = (args.lidar in ["left", "both"])
use_right_lidar = (args.lidar in ["right", "both"])
print("=" * 60)
print(" Stretch 4 Emulated RGB-D API Capabilities Demonstration")
print("=" * 60)
# 1. Initialize the RGB-D Stream
print("\n[1] Initializing Stream...")
streamer, generator = get_emulated_rgbd_stream(
use_left=use_left,
use_right=use_right,
use_center=use_center,
use_left_right=use_left_right,
use_left_right_center=use_left_right_center,
use_left_lidar=use_left_lidar,
use_right_lidar=use_right_lidar,
emulated_rgbd_fps=args.emulated_rgbd_fps,
camera_fps=args.camera_fps,
resolution_height=args.resolution,
compress=not args.disable_compression,
oak_buffer_size=args.oak_buffer_size,
merge_lidars=args.merge_lidars
)
# 2. Initialize the Validity Mask Manager
print("\n[2] Initializing Validity Mask Manager...")
mask_manager = ValidityMaskManager()
try:
print("\n[3] Streaming frames for Visualization...")
print(" -> OpenCV visualization active. Press 'q' or ESC to switch to Rerun.")
cv2.namedWindow("Masked RGB", cv2.WINDOW_NORMAL)
cv2.namedWindow("Dense Depth", cv2.WINDOW_NORMAL)
rerun_mode = False
rerun_initialized = False
# The generator yields synchronized frames indefinitely
for frame_data in generator:
if frame_data is None:
continue
# Handle both single frames and multi-frame objects
frames = []
if hasattr(frame_data, "left") or hasattr(frame_data, "right") or hasattr(frame_data, "center"):
if getattr(frame_data, "left", None): frames.append(frame_data.left)
if getattr(frame_data, "right", None): frames.append(frame_data.right)
if getattr(frame_data, "center", None): frames.append(frame_data.center)
else:
frames.append(frame_data)
# For visualization stacking
all_rgb_masked = []
all_depth_vis = []
for frame in frames:
# 4. Access Lazy Properties
rgb_image = frame.image
depth_image = frame.depth_image
# 5. Access Calibration Data
cam_matrix = frame.camera_matrix
dist_coeffs = frame.distortion_coefficients
T_base_to_cam = frame.T_base_to_cam
# 6. Apply Validity Masks
c_name = frame.camera_type
lidar_str = frame.lidars_used if frame.lidars_used else "no_lidar"
vig_mask, lidar_mask = mask_manager.get_masks(c_name, lidar_str, rgb_image.shape)
dense_depth_validity_mask = vig_mask & lidar_mask
# Apply the vignetting mask to remove invalid fisheye edges from the RGB image
masked_rgb = rgb_image.copy()
masked_rgb[~vig_mask] = 0
# 7. Generate Dense Depth Map
dense_processor = DenseDepthImage(rgb_image, depth_image, apply_validity_mask=False)
dense_depth = dense_processor.compute_dense_depth()
# Before creating a point cloud, apply the eroded combined mask to drop unstable boundary pixels
dense_depth[~dense_depth_validity_mask] = 0
# 8. Create Colored Point Cloud
pts_cam, colors = create_point_cloud_from_depth(
dense_depth, masked_rgb, cam_matrix, dist_coeffs
)
# Transform points to the robot's base coordinate frame for correct upright 3D viewing
pts_cam_homog = np.hstack((pts_cam, np.ones((pts_cam.shape[0], 1))))
T_cam_to_base = np.linalg.inv(T_base_to_cam)
pts_base = (T_cam_to_base @ pts_cam_homog.T).T[:, :3]
# 9. Visualization
if not rerun_mode:
# 9a. OpenCV Visualization
max_depth = 5.0
depth_vis = np.clip(dense_depth, 0, max_depth) / max_depth
depth_vis = (depth_vis * 255).astype(np.uint8)
depth_colormap = cv2.applyColorMap(depth_vis, cv2.COLORMAP_JET)
depth_colormap[dense_depth == 0] = [0, 0, 0]
all_rgb_masked.append(masked_rgb)
all_depth_vis.append(depth_colormap)
else:
# 9b. Rerun Visualization
if not rerun_initialized:
try:
import rerun as rr
import rerun.blueprint as rrb
print(" -> Spawning Rerun viewer...")
rr.init("api_example_visualization", spawn=True)
blueprint = rrb.Blueprint(
rrb.Horizontal(
rrb.Spatial3DView(name="Sparse Point Cloud", origin="sparse_view"),
rrb.Spatial3DView(name="Dense Point Cloud", origin="dense_view"),
rrb.Spatial2DView(name="Layered RGB-D", origin="camera"),
),
rrb.BlueprintPanel(expanded=False),
rrb.SelectionPanel(expanded=True),
rrb.TimePanel(expanded=False, play_state="following"),
)
rr.send_blueprint(blueprint)
rerun_initialized = True
except ImportError:
print(" -> Rerun is not installed. Exiting.")
break
# Prefix paths with camera name for multi-camera support in Rerun
prefix = f"camera/{c_name}/"
rr.log(prefix + "rgb", rr.Image(masked_rgb[:, :, ::-1]))
rr.log(prefix + "dense_depth", rr.DepthImage(dense_depth, meter=1.0, depth_range=[0.0, config.RERUN_COLOR_MAX_DEPTH_M]))
rr.log(prefix + "sparse_depth", rr.DepthImage(depth_image, meter=1.0, depth_range=[0.0, config.RERUN_COLOR_MAX_DEPTH_M]))
rr.log(
f"sparse_view/{c_name}/point_cloud",
rr.Points3D(frame.point_cloud_base, colors=frame.point_colors, radii=[0.01])
)
rr.log(
f"dense_view/{c_name}/point_cloud",
rr.Points3D(pts_base, colors=colors[:, ::-1], radii=[0.01])
)
if not rerun_mode and all_rgb_masked:
# Tile images if there are multiple
stacked_rgb = np.hstack(all_rgb_masked)
stacked_depth = np.hstack(all_depth_vis)
cv2.imshow("Masked RGB", stacked_rgb)
cv2.imshow("Dense Depth", stacked_depth)
key = cv2.waitKey(1) & 0xFF
if key == ord('q') or key == 27:
rerun_mode = True
cv2.destroyAllWindows()
print("\n[9b] Switching to Rerun Visualization (press Ctrl+C to exit)...")
finally:
print("\nStopping streamer...")
streamer.stop()
if __name__ == "__main__":
main()