-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding sensors/realsense_pcd_visualizer.py & polish documents
- Loading branch information
Showing
4 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
examples/Python/ReconstructionSystem/sensors/realsense_pcd_visualizer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# pyrealsense2 is required. | ||
# Please see instructions in https://github.com/IntelRealSense/librealsense/tree/master/wrappers/python | ||
import pyrealsense2 as rs | ||
from open3d import * | ||
import numpy as np | ||
from enum import IntEnum | ||
|
||
from datetime import datetime | ||
from open3d import * | ||
|
||
class Preset(IntEnum): | ||
Custom = 0 | ||
Default = 1 | ||
Hand = 2 | ||
HighAccuracy = 3 | ||
HighDensity = 4 | ||
MediumDensity = 5 | ||
|
||
def get_intrinsic_matrix(frame): | ||
intrinsics = frame.profile.as_video_stream_profile().intrinsics | ||
out = PinholeCameraIntrinsic(640, 480, | ||
intrinsics.fx, intrinsics.fy, intrinsics.ppx, intrinsics.ppy) | ||
return out | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
# Create a pipeline | ||
pipeline = rs.pipeline() | ||
|
||
#Create a config and configure the pipeline to stream | ||
# different resolutions of color and depth streams | ||
config = rs.config() | ||
|
||
config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30) | ||
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30) | ||
|
||
# Start streaming | ||
profile = pipeline.start(config) | ||
depth_sensor = profile.get_device().first_depth_sensor() | ||
|
||
# Using preset HighAccuracy for recording | ||
depth_sensor.set_option(rs.option.visual_preset, Preset.HighAccuracy) | ||
|
||
# Getting the depth sensor's depth scale (see rs-align example for explanation) | ||
depth_scale = depth_sensor.get_depth_scale() | ||
|
||
# We will not display the background of objects more than | ||
# clipping_distance_in_meters meters away | ||
clipping_distance_in_meters = 3 # 3 meter | ||
clipping_distance = clipping_distance_in_meters / depth_scale | ||
# print(depth_scale) | ||
|
||
# Create an align object | ||
# rs.align allows us to perform alignment of depth frames to others frames | ||
# The "align_to" is the stream type to which we plan to align depth frames. | ||
align_to = rs.stream.color | ||
align = rs.align(align_to) | ||
|
||
vis = Visualizer() | ||
vis.create_window() | ||
|
||
pcd = PointCloud() | ||
flip_transform = [[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]] | ||
|
||
# Streaming loop | ||
frame_count = 0 | ||
try: | ||
while True: | ||
|
||
dt0=datetime.now() | ||
|
||
# Get frameset of color and depth | ||
frames = pipeline.wait_for_frames() | ||
|
||
# Align the depth frame to color frame | ||
aligned_frames = align.process(frames) | ||
|
||
# Get aligned frames | ||
aligned_depth_frame = aligned_frames.get_depth_frame() | ||
color_frame = aligned_frames.get_color_frame() | ||
intrinsic = PinholeCameraIntrinsic(get_intrinsic_matrix(color_frame)) | ||
|
||
# Validate that both frames are valid | ||
if not aligned_depth_frame or not color_frame: | ||
continue | ||
|
||
depth_image = Image(np.array(aligned_depth_frame.get_data())) | ||
color_temp = np.asarray(color_frame.get_data()) | ||
color_image = Image(color_temp) | ||
|
||
rgbd_image = create_rgbd_image_from_color_and_depth( | ||
color_image, depth_image, depth_scale=1.0/depth_scale, | ||
depth_trunc=clipping_distance_in_meters, | ||
convert_rgb_to_intensity = False) | ||
temp = create_point_cloud_from_rgbd_image( | ||
rgbd_image, intrinsic) | ||
temp.transform(flip_transform) | ||
pcd.points = temp.points | ||
pcd.colors = temp.colors | ||
|
||
if frame_count == 0: | ||
vis.add_geometry(pcd) | ||
|
||
vis.update_geometry() | ||
vis.poll_events() | ||
vis.update_renderer() | ||
|
||
process_time = datetime.now() - dt0 | ||
print("FPS: "+str(1/process_time.total_seconds())) | ||
frame_count += 1 | ||
|
||
finally: | ||
pipeline.stop() | ||
vis.destroy_window() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters