Correct openCV pipeline for CSI RPi camera v2 on Jetson Nano

Hi all!

I want to use my raspberry v2 camera with jetson nano, in order to take a picture via openCV and store it. I am working on a headless system.

This command on the terminal:

gst-launch-1.0 nvarguscamerasrc num-buffers=1 ! ‘video/x-raw(memory:NVMM), width=1920, height=1080, format=NV12’ ! nvjpegenc ! filesink location=test.jpg

works fine. However, I cannot properly convert it to an openCV pipeline in python code.

I have tried:

cam = cv2.VideoCapture("/dev/video0") 
cam = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw,format=RG10,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink")
cam = cv2.VideoCapture("nvarguscamerasrc sensor-id=0 ! nvoverlaysink")
cam = cv2.VideoCapture(0)

and also
cam = cv2.VideoCapture(gst_cap, cv2.CAP_GSTREAMER)

with

gst_cap

being any of the above.

I always get the following error:

WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (1757) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (886) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

or:

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (1757) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (886) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

I understand that the errors above mean that openCV does not have a picture to work with. In other words, I cannot get an image from my camera.

Also v4l2-ctl -d /dev/video0 --list-formats-ext returns:

ioctl: VIDIOC_ENUM_FMT
Index : 0
Type : Video Capture
Pixel Format: ‘RG10’
Name : 10-bit Bayer RGRG/GBGB
Size: Discrete 3264x2464
Interval: Discrete 0.048s (21.000 fps)
Size: Discrete 3264x1848
Interval: Discrete 0.036s (28.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.017s (60.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.017s (60.000 fps)

Any suggestions?

Hi,
Please try to run this sample:
OpenCV Video Capture with GStreamer doesn't work on ROS-melodic - #3 by DaneLLL

If you can run it successfully, you can encode to JPEG by calling cv2.imwrite().