Error happened open CSI camera with cv2.VideoCapture(0)

Hi all:
I have a problem with cv2.VideoCapture(0).read by Jetson Xavier NX, as described below:
Description:
1.When i use below command “gst-launch-1…” it works well.

gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM),width=1280, height=720, framerate=21/1, format=NV12' ! nvvidconv flip-method=0 ! 'video/x-raw,width=960, height=616' ! nvvidconv ! nvegltransform ! nveglglessink -e

  1. I’m using IMX219 CSI-camera with OpenCV 4.4 and I’m getting errors from cv2.VideoCapture(0), error message is below:
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (1761) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_v4l.cpp (1004) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.
ret :  False
Traceback (most recent call last):
  File "csi_test.py", line 27, in <module>
    show_camera()
  File "csi_test.py", line 16, in show_camera
    cv2.imshow("CSI Camera", img)
cv2.error: OpenCV(4.4.0) /home/ability/test1/opencv-4.4.0/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

3.I compile this code :

import cv2
def show_camera():
    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT,720)
    cap.set(cv2.CAP_PROP_FPS, 15)

    if cap.isOpened():
        while (cap.isOpened()):
            ret_val, img = cap.read()
            print("ret : ",ret_val);
            cv2.imshow("CSI Camera", img)
            
            if cv2.waitKey(1) & 0xFF == ord('q'):
               break
        cap.release()
        cv2.destroyAllWindows()
    else:
        print("Unable to open camera")

if __name__ == "__main__":
    show_camera()

Do you have any suggestions can provide our reference,
Thanks in advance.

why don’t you break the loop if not ret_val?

Hi @crackwitz :
Thanks for reply,
If i break the loop , the error message still appear.

I’m sure that is wrong.

show exactly how you followed my advice. there’s probably another mistake.

Hi @crackwitz :
Thanks for reply,
My modifications as follow:


ret_val, img = cap.read()
print("ret : ",ret_val);
if ret_val is False:
break

please try to set the OPENCV_VIDEOIO_DEBUG=1 environment var,
then rerun your script.
it should print out info about the available video backends on your box.

based on that, play with different backends (e.g. CAP_V4L) and see, if one of them works better.

Hi @crackwitz :
Thanks for your reply,
print out info as bellow:

[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap.cpp (211) open VIDEOIO(GSTREAMER): trying capture cameraNum=0 ...
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (1761) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap.cpp (235) open VIDEOIO(GSTREAMER): can't create capture
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap.cpp (211) open VIDEOIO(MSMF): trying capture cameraNum=0 ...
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap.cpp (274) open VIDEOIO(MSMF): backend is not available (plugin is missing, or can't be loaded due dependencies or it is not compatible)
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap.cpp (211) open VIDEOIO(V4L2): trying capture cameraNum=0 ...
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap.cpp (224) open VIDEOIO(V4L2): created, isOpened=1
[ WARN:0] global /home/ability/test1/opencv-4.4.0/modules/videoio/src/cap_v4l.cpp (1004) tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.

Thanks.