Skip to content

Commit

Permalink
Fixed C++ and python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nohayassin committed May 5, 2021
1 parent 26fa554 commit b5fbe30
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 68 deletions.
1 change: 1 addition & 0 deletions examples/align/rs-align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <librealsense2/rs.hpp>
#include "example-imgui.hpp"
#include "../examples/example-utils.hpp"

/*
This example introduces the concept of spatial stream alignment.
Expand Down
1 change: 1 addition & 0 deletions examples/ar-advanced/rs-ar-advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fstream>
#include <vector>
#include "example.hpp"
#include "../examples/example-utils.hpp"

struct point3d {
float f[3];
Expand Down
73 changes: 73 additions & 0 deletions examples/example-utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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>

//////////////////////////////
// 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;
}
61 changes: 0 additions & 61 deletions examples/example.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,64 +1070,3 @@ void register_glfw_callbacks(window& app, glfw_state& app_state)
}
};
}

// 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";
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/measure/rs-measure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <thread>
#include <atomic>
#include <mutex>
#include "../examples/example-utils.hpp"

using pixel = std::pair<int, int>;

Expand Down
2 changes: 1 addition & 1 deletion examples/pose-and-image/rs-pose-and-image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <chrono>
#include <thread>
#include <mutex>
#include "../example.hpp"
#include "../examples/example-utils.hpp"

int main(int argc, char * argv[]) try
{
Expand Down
2 changes: 1 addition & 1 deletion examples/pose-predict/rs-pose-predict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

inline rs2_quaternion quaternion_exp(rs2_vector v)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/pose/rs-pose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <librealsense2/rs.hpp>
#include <iostream>
#include <iomanip>
#include "../example.hpp"
#include "../examples/example-utils.hpp"

int main(int argc, char * argv[]) try
{
Expand Down
1 change: 1 addition & 0 deletions examples/trajectory/rs-trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <librealsense2/rs.hpp>
#include "example.hpp" // Include short list of convenience functions for rendering
#include <cstring>
#include "../examples/example-utils.hpp"

struct short3
{
Expand Down
7 changes: 7 additions & 0 deletions 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 "../examples/example-utils.hpp"

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

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

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

Expand Down Expand Up @@ -236,6 +241,8 @@ 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);
auto prof = p.start(cfg);
Expand Down
1 change: 0 additions & 1 deletion wrappers/python/examples/align-depth2color.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# Import OpenCV for easy image rendering
import cv2

context = rs.context()
# Create a pipeline
pipeline = rs.pipeline()
# Create a config and configure the pipeline to stream
Expand Down
1 change: 0 additions & 1 deletion wrappers/python/examples/opencv_pointcloud_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import cv2
import numpy as np
import pyrealsense2 as rs
context = rs.context()

class AppState:

Expand Down
1 change: 0 additions & 1 deletion wrappers/python/examples/opencv_viewer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
context = rs.context()

# Get device product line for setting a supporting resolution
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
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 connected device does not support RGB stream")
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

0 comments on commit b5fbe30

Please sign in to comment.