-
Notifications
You must be signed in to change notification settings - Fork 6
/
realsense_device_manager.py
382 lines (298 loc) · 13.3 KB
/
realsense_device_manager.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
##############################################################################################################################################
## License: Apache 2.0. See LICENSE and LICENSE.librealsense files in root directory. ##
##############################################################################################################################################
## This code has been appended to and modified from the librealsense box_dimensioner_multicam example: ##
## https://github.com/IntelRealSense/librealsense/blob/master/wrappers/python/examples/box_dimensioner_multicam/realsense_device_manager.py ##
## Modified functions are denoted with *Modified* and new functions are denoted with *New* ##
##############################################################################################################################################
__doc__ = \
"""
Class for managing Intel RealSense Devices
This code has been appended to and modified from the librealsense box_dimensioner_multicam example:
https://github.com/IntelRealSense/librealsense/blob/master/wrappers/python/examples/box_dimensioner_multicam/realsense_device_manager.py
Modified functions are denoted with *Modified* and new functions are denoted with *New*
Distributed as a module of DynaMo: https://github.com/anderson-cu-bioastronautics/dynamo_realsense-capture
"""
import pyrealsense2 as rs
class Device:
def __init__(self, pipeline, pipeline_profile):
"""
Class to manage each Intel RealSense D4XX Device
Parameters
----------
pipeline : rs.pipeline() object
pipeline_profile : enabled rs.pipeline() object
"""
self.pipeline = pipeline
self.pipeline_profile = pipeline_profile
def enumerate_connected_devices(context):
"""
Enumerate the connected Intel RealSense devices
Parameters
-----------
context : rs.context()
The context created for using the realsense library
Return
-----------
connect_device : array
Array of enumerated devices which are connected to the PC
"""
connect_device = []
for d in context.devices:
if d.get_info(rs.camera_info.name).lower() != 'platform camera':
connect_device.append(d.get_info(rs.camera_info.serial_number))
return connect_device
def post_process_depth_frame(depth_frame, decimation_magnitude=1.0, spatial_magnitude=2.0, spatial_smooth_alpha=0.5, spatial_smooth_delta=20, temporal_smooth_alpha=0.4, temporal_smooth_delta=20):
"""
Filter the depth frame acquired using the Intel RealSense device
Parameters
-----------
depth_frame : rs.frame()
The depth frame to be post-processed
decimation_magnitude : double
The magnitude of the decimation filter
spatial_magnitude : double
The magnitude of the spatial filter
spatial_smooth_alpha : double
The alpha value for spatial filter based smoothening
spatial_smooth_delta : double
The delta value for spatial filter based smoothening
temporal_smooth_alpha : double
The alpha value for temporal filter based smoothening
temporal_smooth_delta : double
The delta value for temporal filter based smoothening
Return:
----------
filtered_frame : rs.frame()
The post-processed depth frame
"""
# Post processing possible only on the depth_frame
assert (depth_frame.is_depth_frame())
# Available filters and control options for the filters
decimation_filter = rs.decimation_filter()
spatial_filter = rs.spatial_filter()
temporal_filter = rs.temporal_filter()
filter_magnitude = rs.option.filter_magnitude
filter_smooth_alpha = rs.option.filter_smooth_alpha
filter_smooth_delta = rs.option.filter_smooth_delta
# Apply the control parameters for the filter
decimation_filter.set_option(filter_magnitude, decimation_magnitude)
spatial_filter.set_option(filter_magnitude, spatial_magnitude)
spatial_filter.set_option(filter_smooth_alpha, spatial_smooth_alpha)
spatial_filter.set_option(filter_smooth_delta, spatial_smooth_delta)
temporal_filter.set_option(filter_smooth_alpha, temporal_smooth_alpha)
temporal_filter.set_option(filter_smooth_delta, temporal_smooth_delta)
# Apply the filters
filtered_frame = decimation_filter.process(depth_frame)
filtered_frame = spatial_filter.process(filtered_frame)
filtered_frame = temporal_filter.process(filtered_frame)
return filtered_frame
class DeviceManager:
def __init__(self, context, pipeline_configuration):
"""
Class to manage all connected Intel RealSense devices
Parameters
-----------
context : rs.context()
The context created for using the realsense library
pipeline_configuration : rs.config()
The realsense library configuration to be used for the application
"""
assert isinstance(context, type(rs.context()))
assert isinstance(pipeline_configuration, type(rs.config()))
self._context = context
self._available_devices = enumerate_connected_devices(context)
self._enabled_devices = {}
self._config = pipeline_configuration
self._frame_counter = 0
def enable_device(self, device_serial, enable_ir_emitter):
"""
Enable an Intel RealSense Device
Parameters
-----------
device_serial : string
Serial number of the realsense device
enable_ir_emitter : bool
Enable/Disable the IR-Emitter of the device
"""
pipeline = rs.pipeline()
# Enable the device
self._config.enable_device(device_serial)
pipeline_profile = pipeline.start(self._config)
# Set the acquisition parameters
sensor = pipeline_profile.get_device().first_depth_sensor()
#sensor.set_option(rs.option.emitter_enabled, 1 if enable_ir_emitter else 0)
self._enabled_devices[device_serial] = (Device(pipeline, pipeline_profile))
def disable_all_devices(self):
"""
*New*
Disable all connected Intel Realsense Devices
"""
for (serial, device) in self._enabled_devices.items():
device.pipeline.stop()
self._config.disable_all_streams()
def enable_all_devices(self, enable_ir_emitter=False):
"""
Enable all the Intel RealSense Devices which are connected to the PC
Parameters
-----------
enable_ir_emitter : bool
Enable/Disable the IR-Emitter of the device
"""
print(str(len(self._available_devices)) + " devices have been found")
for serial in self._available_devices:
self.enable_device(serial, enable_ir_emitter)
def enable_device_from_file(self, file):
"""
*New*
Enable a device from a .bag file
Parameters
-----------
file : str
Filepath of .bag file from which to load a device
"""
pipeline = rs.pipeline()
# Enable the device
self._config.enable_device_from_file(file)
pipeline_profile = pipeline.start(self._config)
self._enabled_devices[file] = (Device(pipeline, pipeline_profile))
def enable_all_emitters(self):
"""
*Modified*
Enable/Disable the emitters of all the connected intel realsense device
Modified to be done before activiating device, performed using rs.context
"""
for device in self._context.devices:
# Get the active profile and enable the emitter for all the connected devices
sensor = device.first_depth_sensor()
sensor.set_option(rs.option.emitter_enabled, 1)
sensor.set_option(rs.option.laser_power, 360)
def load_settings_json(self, path_to_settings_file):
"""
*Modified*
Load the settings stored in the JSON file
This function is modified to obtain device from rs.context() and not from enabled devices
Run before enabling devices to avoid bugs
Parameters
----------
path_to_settings_file : str
Path to JSON settings file for all cameras
"""
file = open(path_to_settings_file, 'r')
json_text = file.read().strip()
file.close()
for device in self._context.devices:
# Get the active profile and load the json file which contains settings readable by the realsense
#device = device.pipeline_profile.get_device()
advanced_mode = rs.rs400_advanced_mode(device)
advanced_mode.load_json(json_text)
def poll_frames(self):
"""
*Modified*
Poll for frames from the enabled Intel RealSense devices.
This function is modified to return frame objects which are of their inherent format from the pyrealsense2 libray.
Returns
--------
frameCollection : dict
Dictionary with keys as serial numbers of all connected cameras, containing each camera's saved images for the frame
"""
frameCollection = {}
while len(frameCollection) != len(self._enabled_devices) :
for (serial, device) in self._enabled_devices.items():
if not serial in frameCollection:
#frameCollection[serial] = {}
pipeline = device.pipeline
frames = pipeline.poll_for_frames()
frames.keep()
if frames.size() != 0:
frameCollection[serial] = frames
return frameCollection
def get_depth_shape(self):
"""
Returns width and height of the depth stream for one arbitrary device
Returns
-------
width: int
height: int
"""
width = -1
height = -1
for (serial, device) in self._enabled_devices.items():
for stream in device.pipeline_profile.get_streams():
if (rs.stream.depth == stream.stream_type()):
width = stream.as_video_stream_profile().width()
height = stream.as_video_stream_profile().height()
return width, height
def get_device_intrinsics(self, frames):
"""
Get the intrinsics of the imager using its frame delivered by the realsense device
Parameters
-----------
frames : rs::frame
The frame grabbed from the imager inside the Intel RealSense for which the intrinsic is needed
Returns
------
device_intrinsics : dict
Dictionary with device_intrinsics stored as
keys : serial
Serial number of the device
values: [key]
Intrinsics of the corresponding device
"""
device_intrinsics = {}
for (serial, frameset) in frames.items():
device_intrinsics[serial] = {}
try:
device_intrinsics[serial][rs.stream.depth] = frameset.get_depth_frame().get_profile().as_video_stream_profile().get_intrinsics()
except:
pass
try:
device_intrinsics[serial][rs.stream.color] = frameset.get_color_frame().get_profile().as_video_stream_profile().get_intrinsics()
except:
pass
try:
device_intrinsics[serial][rs.stream.infrared] = frameset.get_infrared_frame(1).get_profile().as_video_stream_profile().get_intrinsics()
except:
pass
return device_intrinsics
def get_depth_to_color_extrinsics(self, frames):
"""
Get the extrinsics between the depth imager 1 and the color imager using its frame delivered by the realsense device
Parameters
-----------
frames : rs::frame
The frame grabbed from the imager inside the Intel RealSense for which the intrinsic is needed
Return
------
device_intrinsics : dict
keys : serial
Serial number of the device
values: [key]
Extrinsics of the corresponding device
"""
device_extrinsics = {}
for (serial, frameset) in frames.items():
device_extrinsics[serial] = frameset[rs.stream.depth].get_profile().as_video_stream_profile().get_extrinsics_to(
frameset[rs.stream.color].get_profile())
return device_extrinsics
def disable_streams(self):
"""
*New*
Disable all streams in device manager's rs::config
"""
self._config.disable_all_streams()
if __name__ == "__main__":
try:
c = rs.config()
c.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 6)
c.enable_stream(rs.stream.infrared, 1, 1280, 720, rs.format.y8, 6)
c.enable_stream(rs.stream.infrared, 2, 1280, 720, rs.format.y8, 6)
c.enable_stream(rs.stream.color, 1280, 720, rs.format.rgb8, 6)
device_manager = DeviceManager(rs.context(), c)
device_manager.enable_all_devices()
for k in range(150):
frames = device_manager.poll_frames()
device_manager.enable_emitter(True)
device_extrinsics = device_manager.get_depth_to_color_extrinsics(frames)
finally:
device_manager.disable_streams()