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

Enabling LRS examples/tutorials on SR306 #8749

Merged
merged 15 commits into from
May 13, 2021
6 changes: 6 additions & 0 deletions examples/align/rs-align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ void render_slider(rect location, float* alpha, direction* dir);

int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_COLOR,RS2_STREAM_DEPTH }, serial))
return EXIT_SUCCESS;

// Create and initialize GUI related objects
window app(1280, 720, "RealSense Align Example"); // Simple window handling
ImGui_ImplGlfw_Init(app, false); // ImGui library intializition
Expand All @@ -42,6 +46,8 @@ int main(int argc, char * argv[]) try
// Create a pipeline to easily configure and start the camera
rs2::pipeline pipe;
rs2::config cfg;
if (!serial.empty())
cfg.enable_device(serial);
cfg.enable_stream(RS2_STREAM_DEPTH);
cfg.enable_stream(RS2_STREAM_COLOR);
pipe.start(cfg);
Expand Down
6 changes: 6 additions & 0 deletions examples/ar-advanced/rs-ar-advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ std::vector<uint8_t> bytes_from_raw_file(const std::string& filename);

int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_POSE,RS2_STREAM_FISHEYE }, serial))
return EXIT_SUCCESS;

std::string out_map_filepath, in_map_filepath, default_filepath = "map.raw";
for (int c = 1; c < argc; ++c) {
if (!std::strcmp(argv[c], "-m") || !std::strcmp(argv[c], "--load_map")) {
Expand All @@ -76,6 +80,8 @@ int main(int argc, char * argv[]) try
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
if (!serial.empty())
cfg.enable_device(serial);
// Enable fisheye and pose streams
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
cfg.enable_stream(RS2_STREAM_FISHEYE, 1);
Expand Down
74 changes: 74 additions & 0 deletions examples/example-utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once
#include <iostream>
#include <string>
#include <map>
#include <librealsense2/rs.hpp>
#include <algorithm>

//////////////////////////////
// Demos Helpers //
//////////////////////////////

// Find devices with specified streams
bool device_with_streams(std::vector <rs2_stream> stream_requests, std::string& out_serial)
{
rs2::context ctx;
auto devs = ctx.query_devices();
std::vector <rs2_stream> unavailable_streams = stream_requests;
for (auto dev : devs)
{
std::map<rs2_stream, bool> found_streams;
for (auto& type : stream_requests)
{
found_streams[type] = false;
for (auto& sensor : dev.query_sensors())
{
for (auto& profile : sensor.get_stream_profiles())
{
if (profile.stream_type() == type)
{
found_streams[type] = true;
unavailable_streams.erase(std::remove(unavailable_streams.begin(), unavailable_streams.end(), type), unavailable_streams.end());
if (dev.supports(RS2_CAMERA_INFO_SERIAL_NUMBER))
out_serial = dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
}
}
}
}
// Check if all streams are found in current device
bool found_all_streams = true;
for (auto& stream : found_streams)
{
if (!stream.second)
{
found_all_streams = false;
break;
}
}
if (found_all_streams)
return true;
}
// After scanning all devices, not all requested streams were found
for (auto& type : unavailable_streams)
{
switch (type)
{
case RS2_STREAM_POSE:
case RS2_STREAM_FISHEYE:
std::cerr << "Connect T26X and rerun the demo" << std::endl;
break;
case RS2_STREAM_DEPTH:
std::cerr << "The demo requires Realsense camera with DEPTH sensor" << std::endl;
break;
case RS2_STREAM_COLOR:
std::cerr << "The demo requires Realsense camera with RGB sensor" << std::endl;
break;
default:
throw std::runtime_error("The requested stream: " + std::to_string(type) + ", for the demo is not supported by connected devices!"); // stream type
}
}
return false;
}
1 change: 1 addition & 0 deletions examples/example.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <functional>

#include "../third-party/stb_easy_font.h"
#include "example-utils.hpp"

#ifndef PI
#define PI 3.14159265358979323846
Expand Down
6 changes: 6 additions & 0 deletions examples/measure/rs-measure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ void render_simple_distance(const rs2::depth_frame& depth,

int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_COLOR,RS2_STREAM_DEPTH }, serial))
return EXIT_SUCCESS;

// OpenGL textures for the color and depth frames
texture depth_image, color_image;

Expand Down Expand Up @@ -133,6 +137,8 @@ int main(int argc, char * argv[]) try
rs2::pipeline pipe;

rs2::config cfg;
if (!serial.empty())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the Pose/FE demos needs to be retested with T265

cfg.enable_device(serial);
cfg.enable_stream(RS2_STREAM_DEPTH); // Enable default depth
// For the color stream, set format to RGBA
// To allow blending of the color frame on top of the depth frame
Expand Down
1 change: 1 addition & 0 deletions examples/pose-and-image/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ endif()
if(BUILD_EXAMPLES)
add_executable(rs-pose-and-image rs-pose-and-image.cpp)
target_link_libraries(rs-pose-and-image ${DEPENDENCIES})
include_directories(../../examples)
set_target_properties (rs-pose-and-image PROPERTIES FOLDER Examples)
install(TARGETS rs-pose-and-image RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
7 changes: 7 additions & 0 deletions examples/pose-and-image/rs-pose-and-image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
#include <chrono>
#include <thread>
#include <mutex>
#include "example-utils.hpp"

int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_POSE }, serial))
return EXIT_SUCCESS;

// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
if (!serial.empty())
cfg.enable_device(serial);
// Add pose stream
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
// Enable both image streams
Expand Down
1 change: 1 addition & 0 deletions examples/pose-predict/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if(BUILD_EXAMPLES)
add_executable(rs-pose-predict rs-pose-predict.cpp)
set_property(TARGET rs-pose-predict PROPERTY CXX_STANDARD 11)
target_link_libraries(rs-pose-predict ${DEPENDENCIES})
include_directories(../../examples)
set_target_properties (rs-pose-predict PROPERTIES FOLDER Examples)
install(TARGETS rs-pose-predict RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
7 changes: 7 additions & 0 deletions examples/pose-predict/rs-pose-predict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <math.h>
#include <float.h>
#include "example-utils.hpp"

inline rs2_quaternion quaternion_exp(rs2_vector v)
{
Expand Down Expand Up @@ -46,10 +47,16 @@ rs2_pose predict_pose(rs2_pose & pose, float dt_s)

int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_POSE }, serial))
return EXIT_SUCCESS;

// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
if (!serial.empty())
cfg.enable_device(serial);
// Add pose stream
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);

Expand Down
1 change: 1 addition & 0 deletions examples/pose/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ if(BUILD_EXAMPLES)
add_executable(rs-pose rs-pose.cpp)
set_property(TARGET rs-pose PROPERTY CXX_STANDARD 11)
target_link_libraries(rs-pose ${DEPENDENCIES})
include_directories(../../examples)
set_target_properties (rs-pose PROPERTIES FOLDER Examples)
install(TARGETS rs-pose RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
7 changes: 7 additions & 0 deletions examples/pose/rs-pose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
#include <librealsense2/rs.hpp>
#include <iostream>
#include <iomanip>
#include "example-utils.hpp"

int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_POSE}, serial))
return EXIT_SUCCESS;

// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
if (!serial.empty())
cfg.enable_device(serial);
// Add pose stream
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);
// Start pipeline with chosen configuration
Expand Down
6 changes: 6 additions & 0 deletions examples/trajectory/rs-trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ class split_screen_renderer
// In this example, we show how to track the camera's motion using a T265 device
int main(int argc, char * argv[]) try
{
std::string serial;
if (!device_with_streams({ RS2_STREAM_POSE }, serial))
return EXIT_SUCCESS;

// Initialize window for rendering
window app(1280, 720, "RealSense Trajectory Example");

Expand All @@ -456,6 +460,8 @@ int main(int argc, char * argv[]) try
rs2::pipeline pipe;
// Create a configuration for configuring the pipeline with a non default profile
rs2::config cfg;
if (!serial.empty())
cfg.enable_device(serial);
// Add pose stream
cfg.enable_stream(RS2_STREAM_POSE, RS2_FORMAT_6DOF);

Expand Down
2 changes: 1 addition & 1 deletion tools/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ endif()
if(BUILD_GRAPHICAL_EXAMPLES)
add_executable(rs-benchmark rs-benchmark.cpp ../../third-party/glad/glad.c)
target_link_libraries(rs-benchmark ${DEPENDENCIES} realsense2-gl)
include_directories(../../third-party/tclap/include ../../third-party/glad)
include_directories(../../third-party/tclap/include ../../third-party/glad ../../examples)
set_target_properties (rs-benchmark PROPERTIES
FOLDER Tools
)
Expand Down
23 changes: 22 additions & 1 deletion tools/benchmark/rs-benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <fstream>

#include "tclap/CmdLine.h"
#include "example-utils.hpp"

using namespace std;
using namespace chrono;
Expand Down Expand Up @@ -206,6 +207,21 @@ class gl_blocks : public suite

int main(int argc, char** argv) try
{
std::string serial;
rs2_stream second_stream;
if (!device_with_streams({ RS2_STREAM_DEPTH }, serial))
return EXIT_SUCCESS;

if (device_with_streams({ RS2_STREAM_COLOR }, serial))
second_stream = RS2_STREAM_COLOR;
else if (device_with_streams({ RS2_STREAM_INFRARED }, serial))
second_stream = RS2_STREAM_INFRARED;
else
{
std::cout<< " Connect a Depth Camera that supports either RGB or Infrared streams." <<std::endl;
return EXIT_SUCCESS;
}

CmdLine cmd("librealsense rs-benchmark tool", ' ', RS2_API_VERSION_STR);
cmd.parse(argc, argv);

Expand Down Expand Up @@ -236,8 +252,13 @@ int main(int argc, char** argv) try

pipeline p;
config cfg;
if (!serial.empty())
cfg.enable_device(serial);
cfg.enable_stream(RS2_STREAM_DEPTH);
cfg.enable_stream(RS2_STREAM_COLOR, RS2_FORMAT_YUYV, 30);
if(second_stream == RS2_STREAM_COLOR)
cfg.enable_stream(RS2_STREAM_COLOR, RS2_FORMAT_YUYV, 30);
else
cfg.enable_stream(RS2_STREAM_INFRARED);
auto prof = p.start(cfg);
auto dev = prof.get_device();
auto name = dev.get_info(RS2_CAMERA_INFO_NAME);
Expand Down
9 changes: 9 additions & 0 deletions wrappers/python/examples/align-depth2color.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

found_rgb = False
for s in device.sensors:
if s.get_info(rs.camera_info.name) == 'RGB Camera':
found_rgb = True
break
if not found_rgb:
print("The demo requires Depth camera with Color sensor")
exit(0)

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

if device_product_line == 'L500':
Expand Down
14 changes: 13 additions & 1 deletion wrappers/python/examples/opencv_pointcloud_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import numpy as np
import pyrealsense2 as rs


class AppState:

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -67,6 +66,19 @@ def pivot(self):
pipeline = rs.pipeline()
config = rs.config()

pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()

found_rgb = False
for s in device.sensors:
if s.get_info(rs.camera_info.name) == 'RGB Camera':
found_rgb = True
break
if not found_rgb:
print("The demo requires Depth camera with Color sensor")
exit(0)

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

Expand Down
9 changes: 9 additions & 0 deletions wrappers/python/examples/opencv_viewer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))

found_rgb = False
for s in device.sensors:
if s.get_info(rs.camera_info.name) == 'RGB Camera':
found_rgb = True
break
if not found_rgb:
print("The demo requires Depth camera with Color sensor")
exit(0)

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

if device_product_line == 'L500':
Expand Down
14 changes: 13 additions & 1 deletion wrappers/python/examples/pyglet_pointcloud_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,25 @@ def rotation(self):
Ry = rotation_matrix((0, 1, 0), math.radians(-self.yaw))
return np.dot(Ry, Rx).astype(np.float32)


state = AppState()

# Configure streams
pipeline = rs.pipeline()
config = rs.config()

pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()

found_rgb = False
for s in device.sensors:
if s.get_info(rs.camera_info.name) == 'RGB Camera':
found_rgb = True
break
if not found_rgb:
print("The demo requires Depth camera with Color sensor")
exit(0)

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, other_format, 30)
Expand Down