RM新时代网站

C++ 多設備示例代碼-Multi Device

# 多設備示例-MultiDevice

 

功能描述:本示例主要演示了對多設備進(jìn)行操作。

> 本示例基于C++ High Level API進(jìn)行演示

 

首先需要創(chuàng )建一個(gè)Context,獲取設備信息列表用于創(chuàng )建設備及Pipeline

ob::Context ctx;
//查詢(xún)已經(jīng)接入設備的列表
auto devList = ctx.queryDeviceList();
//獲取接入設備的數量
int devCount = devList->deviceCount();

創(chuàng )建設備,并通過(guò)設備創(chuàng )建Pipeline

//遍歷設備列表并創(chuàng )建設備
std::vector<std::shared_ptr<ob::Device>> devices;
for (int i = 0; i < devCount; i++)
{
    //獲取設備并創(chuàng )建pipeline
    auto dev  = devList->getDevice( i );
    auto pipe = std::make_shared< ob::Pipeline >( dev );
    pipes.push_back( pipe );

 

打開(kāi)多個(gè)設備的深度和彩色流

void StartStream(std::vector<std::shared_ptr<ob::Pipeline>> pipes) {
    int i = 0;
    for(auto &&pipe: pipes) {
        std::shared_ptr<ob::Config> config = std::make_shared<ob::Config>();
        // Get the depth camera configuration list
        auto                                    depthProfileList = pipe->getStreamProfileList(OB_SENSOR_DEPTH);
        std::shared_ptr<ob::VideoStreamProfile> depthProfile     = nullptr;
        if(depthProfileList) {
            // Open the default profile of Depth Sensor, which can be configured through the configuration file
            depthProfile = std::const_pointer_cast<ob::StreamProfile>(depthProfileList->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
        }
        config->enableStream(depthProfile);

        // Get the color camera configuration list
        try {
            auto                                    colorProfileList = pipe->getStreamProfileList(OB_SENSOR_COLOR);
            std::shared_ptr<ob::VideoStreamProfile> colorProfile     = nullptr;
            if(colorProfileList) {
                // Open the default profile of Color Sensor, which can be configured through the configuration file
                colorProfile = std::const_pointer_cast<ob::StreamProfile>(colorProfileList->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
            }
            config->enableStream(colorProfile);
        }
        catch(ob::Error &e) {
            std::cerr << "Current device is not support color sensor!" << std::endl;
        }

        // Start the pipeline and pass in the configuration
        pipe->start(config, [i](std::shared_ptr<ob::FrameSet> frameSet) {
            std::lock_guard<std::mutex> lock(frameMutex);
            if(frameSet->colorFrame()) {
                colorFrames[i] = frameSet->colorFrame();
            }
            if(frameSet->depthFrame()) {
                depthFrames[i] = frameSet->depthFrame();
            }
        });
        i++;
    }
}

 

停止所有設備已打開(kāi)的流

void StopStream( std::vector< std::shared_ptr< ob::Pipeline > > pipes) {
    int i = 0;
    for ( auto&& pipe : pipes ) {
        if(colorFrames[i])
            colorFrames->reset();
        if(depthFrames[i])
            depthFrames->reset();
        if(irFrames[i])
            irFrames->reset();
        //停止pipeline
        pipe->stop();
        i++;
    }
}

 

程序正常退出之后資源將會(huì )自動(dòng)釋放

預期輸出:

image.png 


RM新时代网站