Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust python examples to support L515 device #8351

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions wrappers/python/examples/align-depth2color.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,22 @@
# Create a pipeline
pipeline = rs.pipeline()

#Create a config and configure the pipeline to stream
# Create a config and configure the pipeline to stream
# different resolutions of color and depth streams
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

if device_product_line == 'L500':
config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
else:
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

# Start streaming
profile = pipeline.start(config)
Expand Down Expand Up @@ -66,10 +77,13 @@
depth_image_3d = np.dstack((depth_image,depth_image,depth_image)) #depth image is 1 channel, color is 3 channels
bg_removed = np.where((depth_image_3d > clipping_distance) | (depth_image_3d <= 0), grey_color, color_image)

# Render images
# Render images:
# depth align to color on left
# depth on right
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
images = np.hstack((bg_removed, depth_colormap))
cv2.namedWindow('Align Example', cv2.WINDOW_AUTOSIZE)

cv2.namedWindow('Align Example', cv2.WINDOW_NORMAL)
cv2.imshow('Align Example', images)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
Expand Down
12 changes: 12 additions & 0 deletions wrappers/python/examples/depth_auto_calibration_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def main(argv):
pipeline = rs.pipeline()
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()

auto_calibrated_device = rs.auto_calibrated_device(device)

if not auto_calibrated_device:
print("The connected device does not support auto calibration")
return


config.enable_stream(rs.stream.depth, 256, 144, rs.format.z16, 90)
conf = pipeline.start(config)
calib_dev = rs.auto_calibrated_device(conf.get_device())
Expand Down
2 changes: 1 addition & 1 deletion wrappers/python/examples/frame_queue_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def slower_processing(frame):
# 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, 640, 360, rs.format.z16, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

# Start streaming to the slow processing function.
# This stream will lag, causing the occasional frame drop.
Expand Down
5 changes: 3 additions & 2 deletions wrappers/python/examples/opencv_pointcloud_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ def pivot(self):
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

config.enable_stream(rs.stream.depth, rs.format.z16, 30)
config.enable_stream(rs.stream.color, rs.format.bgr8, 30)

# Start streaming
pipeline.start(config)
Expand Down
24 changes: 21 additions & 3 deletions wrappers/python/examples/opencv_viewer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

if device_product_line == 'L500':
config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
else:
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)

# Start streaming
pipeline.start(config)
Expand All @@ -35,8 +46,15 @@
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)

# Stack both images horizontally
images = np.hstack((color_image, depth_colormap))
depth_colormap_dim = depth_colormap.shape
color_colormap_dim = color_image.shape

# If depth and color resolutions are different, resize color image to match depth image for display
if depth_colormap_dim != color_colormap_dim:
resized_color_image = cv2.resize(color_image, dsize=(depth_colormap_dim[1], depth_colormap_dim[0]), interpolation=cv2.INTER_AREA)
images = np.hstack((resized_color_image, depth_colormap))
else:
images = np.hstack((color_image, depth_colormap))

# Show images
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
Expand Down
33 changes: 23 additions & 10 deletions wrappers/python/examples/pyglet_pointcloud_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,15 @@ def rotation(self):
# Configure streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
# other_stream, other_format = rs.stream.infrared, rs.format.y8

config.enable_stream(rs.stream.depth, rs.format.z16, 30)
other_stream, other_format = rs.stream.color, rs.format.rgb8
config.enable_stream(other_stream, 640, 480, other_format, 30)
config.enable_stream(other_stream, other_format, 30)

# Start streaming
pipeline.start(config)
profile = pipeline.get_active_profile()

depth_sensor = profile.get_device().first_depth_sensor()
depth_scale = depth_sensor.get_depth_scale()

depth_profile = rs.video_stream_profile(profile.get_stream(rs.stream.depth))
depth_intrinsics = depth_profile.get_intrinsics()
w, h = depth_intrinsics.width, depth_intrinsics.height
Expand Down Expand Up @@ -145,8 +142,16 @@ def convert_fmt(fmt):
w * h, 'v3f/stream', 't2f/stream', 'n3f/stream')
# Create and allocate memory for our color data
other_profile = rs.video_stream_profile(profile.get_stream(other_stream))
image_data = pyglet.image.ImageData(w, h, convert_fmt(
other_profile.format()), (gl.GLubyte * (w * h * 3))())

image_w, image_h = w, h
color_intrinsics = other_profile.get_intrinsics()
color_w, color_h = color_intrinsics.width, color_intrinsics.height

if state.color:
image_w, image_h = color_w, color_h

image_data = pyglet.image.ImageData(image_w, image_h, convert_fmt(
other_profile.format()), (gl.GLubyte * (image_w * image_h * 3))())

if (pyglet.version.startswith('1.') and not pyglet.version.startswith('1.4')):
# pyglet.clock.ClockDisplay has be removed in 1.4
Expand Down Expand Up @@ -422,9 +427,17 @@ def run(dt):
# handle color source or size change
fmt = convert_fmt(mapped_frame.profile.format())
global image_data

if (image_data.format, image_data.pitch) != (fmt, color_source.strides[0]):
empty = (gl.GLubyte * (w * h * 3))()
image_data = pyglet.image.ImageData(w, h, fmt, empty)
if state.color:
global color_w, color_h
maloel marked this conversation as resolved.
Show resolved Hide resolved
image_w, image_h = color_w, color_h
else:
image_w, image_h = w, h

empty = (gl.GLubyte * (image_w * image_h * 3))()
image_data = pyglet.image.ImageData(image_w, image_h, fmt, empty)

# copy image data to pyglet
image_data.set_data(fmt, color_source.strides[0], color_source.ctypes.data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def find_device_that_supports_advanced_mode() :
if dev.supports(rs.camera_info.name):
print("Found device that supports advanced mode:", dev.get_info(rs.camera_info.name))
return dev
raise Exception("No device that supports advanced mode was found")
raise Exception("No D400 product line device that supports advanced mode was found")

try:
dev = find_device_that_supports_advanced_mode()
Expand Down
8 changes: 7 additions & 1 deletion wrappers/python/examples/python-tutorial-1-depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
try:
# Create a context object. This object owns the handles to all connected realsense devices
pipeline = rs.pipeline()
pipeline.start()

# Configure streams
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)

# Start streaming
pipeline.start(config)

while True:
# This call waits until a new coherent set of frames is available on a device
Expand Down
9 changes: 6 additions & 3 deletions wrappers/python/examples/read_bag_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# Create object for parsing command-line options
parser = argparse.ArgumentParser(description="Read recorded bag file and display depth stream in jet colormap.\
Remember to change the stream resolution, fps and format to match the recorded.")
Remember to change the stream fps and format to match the recorded.")
# Add argument which takes path to a bag file as an input
parser.add_argument("-i", "--input", type=str, help="Path to the bag file")
# Parse the command line arguments to an object
Expand All @@ -37,10 +37,13 @@

# Create a config object
config = rs.config()
# Tell config that we will use a recorded device from filem to be used by the pipeline through playback.

# Tell config that we will use a recorded device from file to be used by the pipeline through playback.
rs.config.enable_device_from_file(config, args.input)

# Configure the pipeline to stream the depth stream
config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)
# Change this parameters according to the recorded bag file resolution
config.enable_stream(rs.stream.depth, rs.format.z16, 30)

# Start streaming from file
pipeline.start(config)
Expand Down
3 changes: 2 additions & 1 deletion wrappers/python/pyrs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ void init_device(py::module &m) {
"like versions of various internal components", "info"_a)
.def("hardware_reset", &rs2::device::hardware_reset, "Send hardware reset request to the device")
.def(py::init<>())
.def("__nonzero__", &rs2::device::operator bool)
.def("__nonzero__", &rs2::device::operator bool) // Called to implement truth value testing in Python 2
.def("__bool__", &rs2::device::operator bool) // Called to implement truth value testing in Python 3
.def(BIND_DOWNCAST(device, debug_protocol))
.def(BIND_DOWNCAST(device, playback))
.def(BIND_DOWNCAST(device, recorder))
Expand Down
7 changes: 5 additions & 2 deletions wrappers/python/pyrs_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ void init_frame(py::module &m) {
.def("stream_name", &rs2::stream_profile::stream_name, "The stream's human-readable name.")
.def("is_default", &rs2::stream_profile::is_default, "Checks if the stream profile is marked/assigned as default, "
"meaning that the profile will be selected when the user requests stream configuration using wildcards.")
.def("__nonzero__", &rs2::stream_profile::operator bool, "Checks if the profile is valid")
.def("__nonzero__", &rs2::stream_profile::operator bool, "Checks if the profile is valid") // Called to implement truth value testing in Python 2
.def("__bool__", &rs2::stream_profile::operator bool, "Checks if the profile is valid") // Called to implement truth value testing in Python 3

.def("get_extrinsics_to", &rs2::stream_profile::get_extrinsics_to, "Get the extrinsic transformation between two profiles (representing physical sensors)", "to"_a)
.def("register_extrinsics_to", &rs2::stream_profile::register_extrinsics_to, "Assign extrinsic transformation parameters "
"to a specific profile (sensor). The extrinsic information is generally available as part of the camera calibration, "
Expand Down Expand Up @@ -109,7 +111,8 @@ void init_frame(py::module &m) {
// .def(py::self = py::self) // can't overload assignment in python
.def(py::init<rs2::frame>())
.def("swap", &rs2::frame::swap, "Swap the internal frame handle with the one in parameter", "other"_a)
.def("__nonzero__", &rs2::frame::operator bool, "check if internal frame handle is valid")
.def("__nonzero__", &rs2::frame::operator bool, "check if internal frame handle is valid") // Called to implement truth value testing in Python 2
.def("__bool__", &rs2::frame::operator bool, "check if internal frame handle is valid") // Called to implement truth value testing in Python 3
.def("get_timestamp", &rs2::frame::get_timestamp, "Retrieve the time at which the frame was captured")
.def_property_readonly("timestamp", &rs2::frame::get_timestamp, "Time at which the frame was captured. Identical to calling get_timestamp.")
.def("get_frame_timestamp_domain", &rs2::frame::get_frame_timestamp_domain, "Retrieve the timestamp domain.")
Expand Down
3 changes: 2 additions & 1 deletion wrappers/python/pyrs_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ void init_processing(py::module &m) {
.def(BIND_DOWNCAST(filter, depth_huffman_decoder))
.def(BIND_DOWNCAST(filter, hdr_merge))
.def(BIND_DOWNCAST(filter, sequence_id_filter))
.def("__nonzero__", &rs2::filter::operator bool); // No docstring in C++
.def("__nonzero__", &rs2::filter::operator bool) // Called to implement truth value testing in Python 2
.def("__bool__", &rs2::filter::operator bool); // Called to implement truth value testing in Python 3
// get_queue?
// is/as?

Expand Down
Loading