Cannot Capture Video Stream with OpenCV

Hello, I am new to using the Jetson TX2 and I have been trying for a long time to obtain a stream from the onboard camera using OpenCV but I have been running into countless issues. The code I used to attempt this is below.

#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
// open the default camera using default API
// cap.open(0);
// OR advance usage: select any API backend
int deviceID = 1; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID, apiID);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
// show live and wait for a key with timeout long enough to show images
imshow("Live", frame);
if (waitKey(5) >= 0)
break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}

The error I am receiving is

GStreamer-CRITICAL **: 15:52:08.328:
Trying to dispose element pipeline0, but it is in PAUSED instead of the NULL state.
You need to explicitly set elements to the NULL state before
dropping the final reference, to allow them to clean up.
This problem may also be caused by a refcounting bug in the
application or some element.

[ WARN:0] global /home/nvidia/OpenCV/workspace/opencv-4.5.0/modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline

(MC:12407): GStreamer-CRITICAL **: 15:52:08.328: Trying to dispose element videoconvert0, but it is in PLAYING instead of the NULL state. You need to explicitly set elements to the NULL state before dropping the final reference, to allow them to clean up. This problem may also be caused by a refcounting bug in the application or some element.

[ WARN:0] global /home/nvidia/OpenCV/workspace/opencv-4.5.0/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

(MC:12407): GStreamer-CRITICAL **: 15:52:08.328: Trying to dispose element appsink0, but it is in READY instead of the NULL state. You need to explicitly set elements to the NULL state before dropping the final reference, to allow them to clean up. This problem may also be caused by a refcounting bug in the application or some element.

[ WARN:0] global /home/nvidia/OpenCV/workspace/opencv-4.5.0/modules/videoio/src/cap_v4l.cpp (893) open VIDEOIO(V4L2:/dev/video1): can't open camera by index ERROR! Unable to open camera

(MC:12407): GStreamer-CRITICAL **: 15:52:08.336: gst_element_post_message: assertion 'GST_IS_ELEMENT (element)' failed

Any assistance in figuring out this problem would be appreciated.

If you are using a CSI camera as the RPI v2 IMX219 camera, then it is a bayer sensor and you cannot access it as a UVC camera. You need to debayer it, this can be done by Argus library, and more specifically with gstreamer plugin nvcamerasrc.

So you wouldn’t try to open your camera from V4L, but from gstreamer backend with such pipeline:

cap.open("nvarguscamerasrc ! video/x-raw(memory:NVMM),height=640,width=480,framerate=30/1 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! queue ! appsink drop=1 ", cv::CAP_GSTREAMER);  

Thank you. This solved the issue I was having.

On another note, I have an issue trying to get an external UVC camera to work with the board. Should I create another forum post?

For your UVC cam, you would use v4l2src plugin.
You may try try this and report info for better advice.