librealsense icon indicating copy to clipboard operation
librealsense copied to clipboard

High density depth image

Open Panepo opened this issue 9 years ago • 7 comments

Required Info
Camera Model D410
Firmware Version 05.06.09.00
Operating System & Version Win10 (Build 16299)
Kernel Version (Linux Only)

Hi there,

There is an option in RS viewer named "Visual Preset"; When set this option to "High Density", the RS outputs a high density depth image. How can I get this high density depth image in python?

Thanks

Panepo avatar Nov 28 '17 02:11 Panepo

Hi @Panepo One way to achieve this would be to export your desired settings from the viewer into a JSON file and later load it from python. This should copy over all relevant depth settings including the preset. Another approach would be to control the RS2_OPTION_VISUAL_PRESET option from python, but I can't find an existing code sample for that. Let us know if this helps, and we will add more example code if needed.

dorodnic avatar Nov 28 '17 06:11 dorodnic

@Panepo Do you have Creators Update? If so, how did you get librealsense to work? I can get a RGB stream, but no depth stream.

danielw256 avatar Nov 29 '17 00:11 danielw256

Hi @dorodnic

I have found the settings of the visual preset of viewer at here, and load it from python. but the output of the depth image is the same as before, is there something wrong on my code? It looks like the device uses default settings, how to set the advance mode to RS correctly?

Thanks

Panepo avatar Nov 29 '17 01:11 Panepo

@danielw256 I have Creators Update and I follow this installation guide. However my device is RS410, which doesn't have a RGB camera, but IR and depth stream works correctly.

Panepo avatar Nov 29 '17 01:11 Panepo

Hi @Panepo I have modified opencv_viewer_example.py to cycle through presets (similar to what the Viewer is doing):

## License: Apache 2.0. See LICENSE file in root directory.
## Copyright(c) 2015-2017 Intel Corporation. All Rights Reserved.

###############################################
##      Open CV and Numpy integration        ##
###############################################

import pyrealsense2 as rs
import numpy as np
import cv2

# 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)

# Start streaming
cfg = pipeline.start(config)
dev = cfg.get_device()
depth_sensor = dev.first_depth_sensor()
depth_sensor.set_option(rs.option.visual_preset, 4)

iteration = 0;
preset = 0;
preset_name = '';

try:
    while True:

        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()
        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()
        
        iteration = iteration + 1
        if iteration > 100:
            preset = preset + 1
            iteration = 0
            range = depth_sensor.get_option_range(rs.option.visual_preset)
            preset = preset % range.max
            depth_sensor.set_option(rs.option.visual_preset, preset)
            preset_name = depth_sensor.get_option_value_description(rs.option.visual_preset, preset)
        
        # Convert images to numpy arrays
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        # Apply colormap on depth image (image must be converted to 8-bit per pixel first)
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, None, 0.5, 0), cv2.COLORMAP_JET)

        # Stack both images horizontally
        images = np.hstack((color_image, depth_colormap))
        
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(images, preset_name,(60,80), font, 4,(255,255,255),2,cv2.LINE_AA)

        # Show images
        cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense', images)
        cv2.waitKey(1)

finally:

    # Stop streaming
    pipeline.stop()

In addition, load_json should also work. After reviewing your code, I had two ideas:

  1. Instead of using find_device_that_supports_advanced_mode, get the device from pipeline configuration after start:
cfg = pipeline.start(config)
dev = cfg.get_device()
  1. Perform load_json after you start streaming

dorodnic avatar Nov 29 '17 11:11 dorodnic

Hi @dorodnic Thanks a lot, it's works.

Panepo avatar Nov 30 '17 05:11 Panepo

I just solved this as well and I thought of giving my code to the community. Note that the json file saved with the rs-depth-quality does not save the info relative to resolution and fps. However, you can change them manually (they are just at the bottom) and then, in the script, read them from the json file and change it when calling the "config.enable_stream" function, as usual.

`

	class RealSenseInterfaceAdvanced():
def __init__(self, jsonFile):
	self.DS5_product_ids = ["0AD1", "0AD2", "0AD3", "0AD4", "0AD5", "0AF6", "0AFE", "0AFF", "0B00", "0B01", "0B03", "0B07"]
            self.jsonFile = jsonFile;
	# Configure depth and color streams
	self.pipeline = rs.pipeline()
	self.config = rs.config()

	jsonObj = json.load(open(self.jsonFile))
	self.json_string = str(jsonObj).replace("'", '\"')

	print("Configuration " + self.jsonFile + " loaded");
	self.enablePipeline(w=int(jsonObj['stream-width']), h=int(jsonObj['stream-height']), freq=int(jsonObj['stream-fps']));


def startStreaming(self):
	self.cfg = self.pipeline.start(self.config)
	cv2.waitKey(500)
	self.loadConfiguration();



def loadConfiguration(self):
	self.dev = self.cfg.get_device()
	self.advnc_mode = rs.rs400_advanced_mode(self.dev)

	print("Advanced mode is", "enabled" if self.advnc_mode.is_enabled() else "disabled")
	self.advnc_mode.load_json(self.json_string)
def autocycleStream(self):
	while (True):
		frames = self.pipeline.wait_for_frames()
		depth_frame = frames.get_depth_frame()
		# color_frame = frames.get_color_frame()
		if not depth_frame:
			continue

		# Convert images to numpy arrays
		depth_image = np.asanyarray(depth_frame.get_data())
		depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
		cv2.imshow('autocycleStream',depth_colormap)
		cv2.waitKey(0)

`

Then call

camera=RealSenseInterfaceAdvanced(jsonFile='configCamera.json');

camera.startStreaming() camera.autocycleStream()

ValerioB88 avatar Apr 05 '18 13:04 ValerioB88