8 import pyrealsense2
as rs
13 | | | | ___ | | _ __ ___ _ __ | ___|_ _ _ __ ___ | |_ (_) ___ _ __ ___ 14 | |_| | / _ \| || '_ \ / _ \| '__| | |_ | | | || '_ \ / __|| __|| | / _ \ | '_ \ / __| 15 | _ || __/| || |_) || __/| | | _| | |_| || | | || (__ | |_ | || (_) || | | |\__ \ 16 |_| |_| \___||_|| .__/ \___||_| |_| \__,_||_| |_| \___| \__||_| \___/ |_| |_||___/ 22 def __init__(self, pipeline, pipeline_profile):
29 Enumerate the connected Intel RealSense devices 33 context : rs.context() 34 The context created for using the realsense library 38 connect_device : array 39 Array of enumerated devices which are connected to the PC 43 for d
in context.devices:
44 if d.get_info(rs.camera_info.name).lower() !=
'platform camera':
45 connect_device.append(d.get_info(rs.camera_info.serial_number))
50 spatial_smooth_delta=20, temporal_smooth_alpha=0.4, temporal_smooth_delta=20):
52 Filter the depth frame acquired using the Intel RealSense device 56 depth_frame : rs.frame() 57 The depth frame to be post-processed 58 decimation_magnitude : double 59 The magnitude of the decimation filter 60 spatial_magnitude : double 61 The magnitude of the spatial filter 62 spatial_smooth_alpha : double 63 The alpha value for spatial filter based smoothening 64 spatial_smooth_delta : double 65 The delta value for spatial filter based smoothening 66 temporal_smooth_alpha: double 67 The alpha value for temporal filter based smoothening 68 temporal_smooth_delta: double 69 The delta value for temporal filter based smoothening 73 filtered_frame : rs.frame() 74 The post-processed depth frame 78 assert (depth_frame.is_depth_frame())
81 decimation_filter = rs.decimation_filter()
82 spatial_filter = rs.spatial_filter()
83 temporal_filter = rs.temporal_filter()
85 filter_magnitude = rs.option.filter_magnitude
86 filter_smooth_alpha = rs.option.filter_smooth_alpha
87 filter_smooth_delta = rs.option.filter_smooth_delta
90 decimation_filter.set_option(filter_magnitude, decimation_magnitude)
91 spatial_filter.set_option(filter_magnitude, spatial_magnitude)
92 spatial_filter.set_option(filter_smooth_alpha, spatial_smooth_alpha)
93 spatial_filter.set_option(filter_smooth_delta, spatial_smooth_delta)
94 temporal_filter.set_option(filter_smooth_alpha, temporal_smooth_alpha)
95 temporal_filter.set_option(filter_smooth_delta, temporal_smooth_delta)
98 filtered_frame = decimation_filter.process(depth_frame)
99 filtered_frame = spatial_filter.process(filtered_frame)
100 filtered_frame = temporal_filter.process(filtered_frame)
102 return filtered_frame
107 | \/ | __ _ (_) _ __ / ___| ___ _ __ | |_ ___ _ __ | |_ 108 | |\/| | / _` || || '_ \ | | / _ \ | '_ \ | __|/ _ \| '_ \ | __| 109 | | | || (_| || || | | | | |___| (_) || | | || |_| __/| | | || |_ 110 |_| |_| \__,_||_||_| |_| \____|\___/ |_| |_| \__|\___||_| |_| \__| 116 def __init__(self, context, pipeline_configuration):
118 Class to manage the Intel RealSense devices 122 context : rs.context() 123 The context created for using the realsense library 124 pipeline_configuration : rs.config() 125 The realsense library configuration to be used for the application 128 assert isinstance(context, type(rs.context()))
129 assert isinstance(pipeline_configuration, type(rs.config()))
138 Enable an Intel RealSense Device 142 device_serial : string 143 Serial number of the realsense device 144 enable_ir_emitter : bool 145 Enable/Disable the IR-Emitter of the device 148 pipeline = rs.pipeline()
151 self._config.enable_device(device_serial)
152 pipeline_profile = pipeline.start(self.
_config)
155 sensor = pipeline_profile.get_device().first_depth_sensor()
156 sensor.set_option(rs.option.emitter_enabled, 1
if enable_ir_emitter
else 0)
161 Enable all the Intel RealSense Devices which are connected to the PC 171 Enable/Disable the emitter of the intel realsense device 174 for (device_serial, device)
in self._enabled_devices.items():
176 sensor = device.pipeline_profile.get_device().first_depth_sensor()
177 sensor.set_option(rs.option.emitter_enabled, 1
if enable_ir_emitter
else 0)
178 if enable_ir_emitter:
179 sensor.set_option(rs.option.laser_power, 330)
183 Load the settings stored in the JSON file 187 with open(path_to_settings_file,
'r') as file: 188 json_text = file.read().strip() 190 for (device_serial, device)
in self._enabled_devices.items():
192 device = device.pipeline_profile.get_device()
193 advanced_mode = rs.rs400_advanced_mode(device)
194 advanced_mode.load_json(json_text)
198 Poll for frames from the enabled Intel RealSense devices. This will return at least one frame from each device. 199 If temporal post processing is enabled, the depth stream is averaged over a certain amount of frames 205 while len(frames) < len(self._enabled_devices.items()) :
206 for (serial, device)
in self._enabled_devices.items():
207 streams = device.pipeline_profile.get_streams()
208 frameset = device.pipeline.poll_for_frames()
209 if frameset.size() == len(streams):
211 for stream
in streams:
212 if (rs.stream.infrared == stream.stream_type()):
213 frame = frameset.get_infrared_frame(stream.stream_index())
214 key_ = (stream.stream_type(), stream.stream_index())
216 frame = frameset.first_or_default(stream.stream_type())
217 key_ = stream.stream_type()
218 frames[serial][key_] = frame
224 Retruns width and height of the depth stream for one arbitrary device 233 for (serial, device)
in self._enabled_devices.items():
234 for stream
in device.pipeline_profile.get_streams():
235 if (rs.stream.depth == stream.stream_type()):
236 width = stream.as_video_stream_profile().width()
237 height = stream.as_video_stream_profile().height()
242 Get the intrinsics of the imager using its frame delivered by the realsense device 247 The frame grabbed from the imager inside the Intel RealSense for which the intrinsic is needed 251 device_intrinsics : dict 253 Serial number of the device 255 Intrinsics of the corresponding device 257 device_intrinsics = {}
258 for (serial, frameset)
in frames.items():
259 device_intrinsics[serial] = {}
260 for key, value
in frameset.items():
261 device_intrinsics[serial][key] = value.get_profile().as_video_stream_profile().get_intrinsics()
262 return device_intrinsics
266 Get the extrinsics between the depth imager 1 and the color imager using its frame delivered by the realsense device 271 The frame grabbed from the imager inside the Intel RealSense for which the intrinsic is needed 275 device_intrinsics : dict 277 Serial number of the device 279 Extrinsics of the corresponding device 281 device_extrinsics = {}
282 for (serial, frameset)
in frames.items():
283 device_extrinsics[serial] = frameset[
284 rs.stream.depth].get_profile().as_video_stream_profile().get_extrinsics_to(
285 frameset[rs.stream.color].get_profile())
286 return device_extrinsics
289 self._config.disable_all_streams()
294 |_ _|___ ___ | |_ (_) _ __ __ _ 295 | | / _ \/ __|| __|| || '_ \ / _` | 296 | || __/\__ \| |_ | || | | || (_| | 297 |_| \___||___/ \__||_||_| |_| \__, | 301 if __name__ ==
"__main__":
304 c.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 6)
305 c.enable_stream(rs.stream.infrared, 1, 1280, 720, rs.format.y8, 6)
306 c.enable_stream(rs.stream.infrared, 2, 1280, 720, rs.format.y8, 6)
307 c.enable_stream(rs.stream.color, 1280, 720, rs.format.rgb8, 6)
309 device_manager.enable_all_devices()
311 frames = device_manager.poll_frames()
312 device_manager.enable_emitter(
True)
313 device_extrinsics = device_manager.get_depth_to_color_extrinsics(frames)
315 device_manager.disable_streams()
def __init__(self, pipeline, pipeline_profile)
def get_depth_to_color_extrinsics(self, frames)
def get_depth_shape(self)
def enable_emitter(self, enable_ir_emitter=True)
def enumerate_connected_devices(context)
def load_settings_json(self, path_to_settings_file)
def disable_streams(self)
def get_device_intrinsics(self, frames)
static std::string print(const transformation &tf)
def enable_all_devices(self, enable_ir_emitter=False)
def __init__(self, context, pipeline_configuration)
def enable_device(self, device_serial, enable_ir_emitter)
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)