Skip to content

Commit b5fbe30

Browse files
committed
Fixed C++ and python examples
1 parent 26fa554 commit b5fbe30

14 files changed

+100
-68
lines changed

examples/align/rs-align.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <librealsense2/rs.hpp>
55
#include "example-imgui.hpp"
6+
#include "../examples/example-utils.hpp"
67

78
/*
89
This example introduces the concept of spatial stream alignment.

examples/ar-advanced/rs-ar-advanced.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <fstream>
1010
#include <vector>
1111
#include "example.hpp"
12+
#include "../examples/example-utils.hpp"
1213

1314
struct point3d {
1415
float f[3];

examples/example-utils.hpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// License: Apache 2.0. See LICENSE file in root directory.
2+
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3+
4+
#pragma once
5+
#include <iostream>
6+
#include <string>
7+
#include <map>
8+
#include <librealsense2/rs.hpp>
9+
10+
//////////////////////////////
11+
// Demos Helpers //
12+
//////////////////////////////
13+
14+
// Find devices with specified streams
15+
bool device_with_streams(std::vector <rs2_stream> stream_requests, std::string& out_serial)
16+
{
17+
rs2::context ctx;
18+
auto devs = ctx.query_devices();
19+
std::vector <rs2_stream> unavailable_streams = stream_requests;
20+
for (auto& dev : devs)
21+
{
22+
std::map<rs2_stream, bool> found_streams;
23+
for (auto& type : stream_requests)
24+
{
25+
found_streams[type] = false;
26+
for (auto& sensor : dev.query_sensors())
27+
{
28+
for (auto& profile : sensor.get_stream_profiles())
29+
{
30+
if (profile.stream_type() == type)
31+
{
32+
found_streams[type] = true;
33+
unavailable_streams.erase(std::remove(unavailable_streams.begin(), unavailable_streams.end(), type), unavailable_streams.end());
34+
if (dev.supports(RS2_CAMERA_INFO_SERIAL_NUMBER))
35+
out_serial = dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
36+
}
37+
}
38+
}
39+
}
40+
// Check if all streams are found in current device
41+
bool found_all_streams = true;
42+
for (auto& stream : found_streams)
43+
{
44+
if (!stream.second)
45+
{
46+
found_all_streams = false;
47+
break;
48+
}
49+
}
50+
if (found_all_streams)
51+
return true;
52+
}
53+
// After scanning all devices, not all requested streams were found
54+
for (auto& type : unavailable_streams)
55+
{
56+
switch (type)
57+
{
58+
case RS2_STREAM_POSE:
59+
case RS2_STREAM_FISHEYE:
60+
std::cerr << "Connect T26X and rerun the demo" << std::endl;
61+
break;
62+
case RS2_STREAM_DEPTH:
63+
std::cerr << "The demo requires Realsense camera with DEPTH sensor" << std::endl;
64+
break;
65+
case RS2_STREAM_COLOR:
66+
std::cerr << "The demo requires Realsense camera with RGB sensor" << std::endl;
67+
break;
68+
default:
69+
throw std::runtime_error("The requested stream: " + std::to_string(type) + ", for the demo is not supported by connected devices!"); // stream type
70+
}
71+
}
72+
return false;
73+
}

examples/example.hpp

-61
Original file line numberDiff line numberDiff line change
@@ -1070,64 +1070,3 @@ void register_glfw_callbacks(window& app, glfw_state& app_state)
10701070
}
10711071
};
10721072
}
1073-
1074-
// Find devices with specified streams
1075-
bool device_with_streams(std::vector <rs2_stream> stream_requests, std::string& out_serial)
1076-
{
1077-
rs2::context ctx;
1078-
auto devs = ctx.query_devices();
1079-
std::vector <rs2_stream> unavailable_streams = stream_requests;
1080-
for (auto& dev : devs)
1081-
{
1082-
std::map<rs2_stream, bool> found_streams;
1083-
for (auto& type : stream_requests)
1084-
{
1085-
found_streams[type] = false;
1086-
for (auto& sensor : dev.query_sensors())
1087-
{
1088-
for (auto& profile : sensor.get_stream_profiles())
1089-
{
1090-
if (profile.stream_type() == type)
1091-
{
1092-
found_streams[type] = true;
1093-
unavailable_streams.erase(std::remove(unavailable_streams.begin(), unavailable_streams.end(), type), unavailable_streams.end());
1094-
if(dev.supports(RS2_CAMERA_INFO_SERIAL_NUMBER))
1095-
out_serial = dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER);
1096-
}
1097-
}
1098-
}
1099-
}
1100-
// Check if all streams are found in current device
1101-
bool found_all_streams = true;
1102-
for (auto& stream : found_streams)
1103-
{
1104-
if (!stream.second)
1105-
{
1106-
found_all_streams = false;
1107-
break;
1108-
}
1109-
}
1110-
if(found_all_streams)
1111-
return true;
1112-
}
1113-
// After scanning all devices, not all requested streams were found
1114-
for (auto& type : unavailable_streams)
1115-
{
1116-
switch (type)
1117-
{
1118-
case RS2_STREAM_POSE:
1119-
case RS2_STREAM_FISHEYE:
1120-
std::cerr << "Connect T26X and rerun the demo";
1121-
break;
1122-
case RS2_STREAM_DEPTH:
1123-
std::cerr << "The demo requires Realsense camera with DEPTH sensor" << std::endl;
1124-
break;
1125-
case RS2_STREAM_COLOR:
1126-
std::cerr << "The demo requires Realsense camera with RGB sensor" << std::endl;
1127-
break;
1128-
default:
1129-
throw std::runtime_error("The requested stream: " + std::to_string(type) + ", for the demo is not supported by connected devices!"); // stream type
1130-
}
1131-
}
1132-
return false;
1133-
}

examples/measure/rs-measure.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <thread>
1515
#include <atomic>
1616
#include <mutex>
17+
#include "../examples/example-utils.hpp"
1718

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

examples/pose-and-image/rs-pose-and-image.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <chrono>
77
#include <thread>
88
#include <mutex>
9-
#include "../example.hpp"
9+
#include "../examples/example-utils.hpp"
1010

1111
int main(int argc, char * argv[]) try
1212
{

examples/pose-predict/rs-pose-predict.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include <math.h>
1111
#include <float.h>
12-
#include "../example.hpp"
12+
#include "../examples/example-utils.hpp"
1313

1414
inline rs2_quaternion quaternion_exp(rs2_vector v)
1515
{

examples/pose/rs-pose.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <librealsense2/rs.hpp>
44
#include <iostream>
55
#include <iomanip>
6-
#include "../example.hpp"
6+
#include "../examples/example-utils.hpp"
77

88
int main(int argc, char * argv[]) try
99
{

examples/trajectory/rs-trajectory.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <librealsense2/rs.hpp>
55
#include "example.hpp" // Include short list of convenience functions for rendering
66
#include <cstring>
7+
#include "../examples/example-utils.hpp"
78

89
struct short3
910
{

tools/benchmark/rs-benchmark.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <fstream>
1818

1919
#include "tclap/CmdLine.h"
20+
#include "../examples/example-utils.hpp"
2021

2122
using namespace std;
2223
using namespace chrono;
@@ -206,6 +207,10 @@ class gl_blocks : public suite
206207

207208
int main(int argc, char** argv) try
208209
{
210+
std::string serial;
211+
if (!device_with_streams({ RS2_STREAM_COLOR,RS2_STREAM_DEPTH }, serial))
212+
return EXIT_SUCCESS;
213+
209214
CmdLine cmd("librealsense rs-benchmark tool", ' ', RS2_API_VERSION_STR);
210215
cmd.parse(argc, argv);
211216

@@ -236,6 +241,8 @@ int main(int argc, char** argv) try
236241

237242
pipeline p;
238243
config cfg;
244+
if (!serial.empty())
245+
cfg.enable_device(serial);
239246
cfg.enable_stream(RS2_STREAM_DEPTH);
240247
cfg.enable_stream(RS2_STREAM_COLOR, RS2_FORMAT_YUYV, 30);
241248
auto prof = p.start(cfg);

wrappers/python/examples/align-depth2color.py

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# Import OpenCV for easy image rendering
1313
import cv2
1414

15-
context = rs.context()
1615
# Create a pipeline
1716
pipeline = rs.pipeline()
1817
# Create a config and configure the pipeline to stream

wrappers/python/examples/opencv_pointcloud_viewer.py

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import cv2
3131
import numpy as np
3232
import pyrealsense2 as rs
33-
context = rs.context()
3433

3534
class AppState:
3635

wrappers/python/examples/opencv_viewer_example.py

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# Configure depth and color streams
1313
pipeline = rs.pipeline()
1414
config = rs.config()
15-
context = rs.context()
1615

1716
# Get device product line for setting a supporting resolution
1817
pipeline_wrapper = rs.pipeline_wrapper(pipeline)

wrappers/python/examples/pyglet_pointcloud_viewer.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,25 @@ def rotation(self):
8484
Ry = rotation_matrix((0, 1, 0), math.radians(-self.yaw))
8585
return np.dot(Ry, Rx).astype(np.float32)
8686

87-
8887
state = AppState()
8988

9089
# Configure streams
9190
pipeline = rs.pipeline()
9291
config = rs.config()
9392

93+
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
94+
pipeline_profile = config.resolve(pipeline_wrapper)
95+
device = pipeline_profile.get_device()
96+
97+
found_rgb = False
98+
for s in device.sensors:
99+
if s.get_info(rs.camera_info.name) == 'RGB Camera':
100+
found_rgb = True
101+
break
102+
if not found_rgb:
103+
print("The connected device does not support RGB stream")
104+
exit(0)
105+
94106
config.enable_stream(rs.stream.depth, rs.format.z16, 30)
95107
other_stream, other_format = rs.stream.color, rs.format.rgb8
96108
config.enable_stream(other_stream, other_format, 30)

0 commit comments

Comments
 (0)