Jetson Nano C++ Opencv Get Camera Frame

Hi all,
I am currently experiencing an error in obtaining the camera image through OpenCV. So seek help.
Device: Jetson Nano 4G
Code:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // Set GStreamer pipeline string
    std::string pipeline = "nvarguscamerasrc sensor-id=0 ! \"video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1\" ! nvvidconv ! videoconvert ! appsink";

    // Turn on the camera
    cv::VideoCapture cap(pipeline, cv::CAP_GSTREAMER);
    if (!cap.isOpened()) {
        std::cerr << "Can't open camera" << std::endl;
        return -1;
    }

    cv::Mat frame;
    while (true) {
        cap >> frame; 
        if (frame.empty()) {
            std::cerr << "No frames captured" << std::endl;
            break;
        }

        cv::imshow("Camera Feed", frame);

        if (cv::waitKey(30) == 'q') {
            break;
        }
    }

    cap.release(); 
    cv::destroyAllWindows();
    return 0;
}

Using OpenCV, cap. isOpened() returns True, but reports an error when retrieving frames.Information output during runtime
[ WARN:0@30.319] global cap_gstreamer.cpp:1697 open OpenCV | GStreamer warning: unable to query duration of stream

(test:28379): GStreamer-CRITICAL **: 17:48:39.602: gst_caps_get_structure: assertion ‘GST_IS_CAPS (caps)’ failed

(test:28379): GStreamer-CRITICAL **: 17:48:39.602: gst_structure_get_int: assertion ‘structure != NULL’ failed
[ WARN:0@30.319] global cap_gstreamer.cpp:1707 open OpenCV | GStreamer warning: cannot query video width/height

(test:28379): GStreamer-CRITICAL **: 17:48:39.602: gst_structure_get_fraction: assertion ‘structure != NULL’ failed
[ WARN:0@30.319] global cap_gstreamer.cpp:1713 open OpenCV | GStreamer warning: cannot query video fps
[ WARN:0@30.319] global cap_gstreamer.cpp:1728 open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

(test:28379): GStreamer-CRITICAL **: 17:49:44.603: gst_sample_get_caps: assertion ‘GST_IS_SAMPLE (sample)’ failed
[ERROR:0@95.320] global cap_gstreamer.cpp:925 retrieveVideoFrame GStreamer: gst_sample_get_caps() returns NULL

I can use the following command to open the camera screen:
gst-launch-1.0 nvarguscamerasrc sensor-id=0 ! “video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1” ! nvvidconv ! xvimagesink sync=false

Regrads

Hi,
Please try the python sample and see if it works:
OpenCV Video Capture with GStreamer doesn't work on ROS-melodic - #3 by DaneLLL

Hi DaneLLL,
Thank you very much. After changing the pipeline, I can now get the image.
Regrads

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.