How to Open e-con Camera using C++?

Hi, this time I’ve successfully open my e-con camera using gstreamer pipeline on terminal using this command :

gst-launch-1.0 v4l2src device="/dev/video1" ! "video/x-raw,width=1920,height=1080,format=UYVY" ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12' ! nvoverlaysink

Now I want to open my camera using C++ code with OpenCV but I keep failing to do it, here’s my code:

#include "opencv2/opencv.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
 
using namespace std;
using namespace cv;
 
int main(){
 
  // Create a VideoCapture object and open the input file
  // If the input is the web camera, pass 0 instead of the video file name
  const char* gst =  "v4l2src device='/dev/video1' ! 'video/x-raw,width=1920,height=1080,format=UYVY' ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12' ! appsink";

  VideoCapture cap(gst); 
    
  // Check if camera opened successfully
  if(!cap.isOpened()){
    cout << "Error opening video stream or file" << endl;
    return -1;
  }
     
  while(1){
 
    Mat frame;
    // Capture frame-by-frame
    cap >> frame;
  
    // If the frame is empty, break immediately
    if (frame.empty())
      break;
 
    // Display the resulting frame
    imshow( "Frame", frame );
 
    // Press  ESC on keyboard to exit
    char c=(char)waitKey(25);
    if(c==27)
      break;
  }
  
  // When everything done, release the video capture object
  cap.release();
 
  // Closes all the frames
  destroyAllWindows();
     
  return 0;
}

I compile with

g++ camtest.cpp -o output `pkg-config --cflags --libs opencv`

and produces this error

VIDEOIO ERROR: V4L: device v4l2src device='/dev/video1' ! 'video/x-raw,width=1920,height=1080,format=UYVY' ! nvvidconv ! 'video/x-raw(memory:NVMM),format=NV12' ! appsink: Unable to query number of channels

(output:13844): GStreamer-CRITICAL **: 11:22:32.713: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed

(output:13844): GStreamer-CRITICAL **: 11:22:32.761: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed

(output:13844): GLib-GObject-WARNING **: 11:22:32.762: invalid cast from 'GstAppSink' to 'GstBin'

(output:13844): GStreamer-CRITICAL **: 11:22:32.763: gst_bin_iterate_elements: assertion 'GST_IS_BIN (bin)' failed

(output:13844): GStreamer-CRITICAL **: 11:22:32.763: gst_iterator_next: assertion 'it != NULL' failed

(output:13844): GStreamer-CRITICAL **: 11:22:32.763: gst_iterator_free: assertion 'it != NULL' failed
OpenCV Error: Unspecified error (GStreamer: cannot find appsink in manual pipeline
) in cvCaptureFromCAM_GStreamer, file /home/nvidia/src/opencv-3.4.0/modules/videoio/src/cap_gstreamer.cpp, line 805
VIDEOIO(cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename)): raised OpenCV exception:

/home/nvidia/src/opencv-3.4.0/modules/videoio/src/cap_gstreamer.cpp:805: error: (-2) GStreamer: cannot find appsink in manual pipeline
 in function cvCaptureFromCAM_GStreamer

Any help will be appreciated thanks

I solved it by changing the gst string to

const char* gst =  "v4l2src device=/dev/video1 ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)UYVY ! videoconvert ! appsink";

But strangely on the terminal it shows this error first, then it will open my camera successfully, I wonder why

VIDEOIO ERROR: V4L: device v4l2src device=/dev/video1 ! video/x-raw, width=(int)1920, height=(int)1080, format=(string)UYVY ! videoconvert ! appsink: Unable to query number of channels

Hi Ivan,
This error message looks like its related to opencv and its build flags.

Kindly have a look at e-con’s blog on using opencv with e-con’s cameras in the below link, and see if that helps in resolving the error:

https://www.e-consystems.com/Articles/Camera/accessing_cameras_in_opencv_with_high_performance.asp