Using Visionworks Api on Jetson TX1 is there a way to get frames from two USB cameras at the same time with same context?

Using VisionWorks Api on Jetson TX1 is there a way to get frames from two USB cameras at the same time with same context?

for example will this code work and will it actually get frames from two cameras in real time and update the frame status:

std::string sourceLeftUri = "device:///v4l2?index=0";
std::string sourceRightUri = "device:///v4l2?index=1";

nvxio::ContextGuard context;
std::unique_ptr<nvxio::FrameSource> sourceLeft(nvxio::createDefaultFrameSource(context, sourceLeftUri));
std::unique_ptr<nvxio::FrameSource> sourceRight(nvxio::createDefaultFrameSource(context, sourceRightUri));

sourceLeft->open();
sourceRight->open();

vx_image frameLeft = vxCreateImage(context, frameConfigLeft.frameWidth, frameConfigLeft.frameHeight, VX_DF_IMAGE_RGBX);
vx_image frameRight = vxCreateImage(context, frameConfigRight.frameWidth, frameConfigRight.frameHeight, VX_DF_IMAGE_RGBX);

// ...
// more super cuda magic
// ...

//then goes while loop that fetches the frames from two cameras
while (true) 
{
        nvxio::FrameSource::FrameStatus statusLeft = sourceLeft->fetch(frameLeft);
        nvxio::FrameSource::FrameStatus statusRight = sourceRight->fetch(frameRight);

        // do something with frameLeft and frameRight, for example get stereo diparity with SMGB
        // ...
}

so is this code gonna work? no limitatins for context to have more than one FrameSource?

Thank you for your help in advance!

Hi,

It works.
I write a sample to open two cameras and display frames in turn (change to another camera every 120 frames).

Thanks.

#include <NVXIO/Application.hpp>
#include <NVXIO/ConfigParser.hpp>
#include <NVXIO/FrameSource.hpp>
#include <NVXIO/Render.hpp>
#include <NVXIO/SyncTimer.hpp>
#include <NVXIO/Utility.hpp>

#include <iostream>


int main(int argc, char* argv[]) {
    std::string sourceURI1 = "device:///v4l2?index=1";
    std::string sourceURI2 = "device:///v4l2?index=2";

    nvxio::Application &app = nvxio::Application::get();
    app.init(argc, argv);

    nvxio::ContextGuard context;
    vxDirective(context, VX_DIRECTIVE_ENABLE_PERFORMANCE);

    std::unique_ptr<nvxio::FrameSource> cam1(nvxio::createDefaultFrameSource(context, sourceURI1));
    std::unique_ptr<nvxio::FrameSource> cam2(nvxio::createDefaultFrameSource(context, sourceURI2));

    nvxio::FrameSource::Parameters config;
    config.frameWidth = 1280;
    config.frameHeight = 720;
    cam1->setConfiguration(config);
    cam2->setConfiguration(config);
    if (!cam1 || !cam1->open())
    {
        std::cerr << "Error: Can't open source URI " << sourceURI1 << std::endl;
        return nvxio::Application::APP_EXIT_CODE_NO_RESOURCE;
    }

    if (!cam2 || !cam2->open())
    {
        std::cerr << "Error: Can't open source URI " << sourceURI2 << std::endl;
        return nvxio::Application::APP_EXIT_CODE_NO_RESOURCE;
    }

    nvxio::FrameSource::Parameters cam1Params = cam1->getConfiguration();
    nvxio::FrameSource::Parameters cam2Params = cam2->getConfiguration();

    std::cout << "CAM1: " << cam1Params.frameWidth << " " << cam1Params.frameHeight << std::endl;
    std::cout << "CAM2: " << cam2Params.frameWidth << " " << cam2Params.frameHeight << std::endl;

    std::unique_ptr<nvxio::Render> renderer(nvxio::createDefaultRender(context, "Demo", config.frameWidth, config.frameHeight));
    vx_image frame_cam1 = vxCreateImage(context, config.frameWidth, config.frameHeight, VX_DF_IMAGE_RGBX);
    vx_image frame_cam2 = vxCreateImage(context, config.frameWidth, config.frameHeight, VX_DF_IMAGE_RGBX);

    int numFrame = 0;
    while(true){
        numFrame++;
        cam1->fetch(frame_cam1);
        cam2->fetch(frame_cam2);

        if( (numFrame/120)%2==0 ) renderer->putImage(frame_cam1);
        else renderer->putImage(frame_cam2);
        renderer->flush();
    }


    std::cout << "All goods" << std::endl;
    return 0;
}
nvcc topic_1017188.cpp -std=c++11 -lvisionworks -lnvxio -o test && ./test