Onboard camera not working with OpenCV

I’m trying to read from the onboard camera with opencv and even after following many other solutions online, can not get it to work. When I try with code like:

cam = cv2.VideoCapture(“nvarguscamersrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=2 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink”)
_, img = cam.read()
cv2.imshow(‘input’, img)

I get the following error on imshow:

cv2.error: /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/highgui/src/window.cpp:304: error: (-215) size.width>0 && size.height>0 in function imshow

Which I believe means the camera isn’t starting.

I can start the camera from command line with gst-launch-1.0 and v4l2-ctl --list-devices shows the following:

vi-output, ov5693 2-0036 (platform:15700000.vi:2):

I have also tried the code from Capture and display video from either IP CAM, USB webcam, or the Tegra X2/X1 onboard camera. · GitHub and it fails to open the camera as well.

My cv2.version is 3.2.0
Any idea to what my problem could possibly be?

This means you are trying to display a void frame.

Main cause may be a typo in your pipeline: nvarguscamerasrc, so the pipeline failed to start.
You would test if the videoCapture is opened before reading from it (cam.isOpened()) and also check return code from cap.read() before using frame.

Sorry for the typo in my original post, but I double checked the code and I do have nvarguscamerasrc (don’t know how that happened).

When I print the value of cam.read() I get (False, none).

I’ve tried a number of variations on the input for cv2.VideoCapture and nothing has worked, so I’m unable to tell what is wrong other than the fact that the camera is not starting. Also, I tried that code I linked and it didn’t work either, which seems strange.

Sorry I missed that from your code, but output format is NV12 for nvarguscamerasrc, it was I420 in older releases with nvcamerasrc.

import cv2

cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! queue ! appsink drop=1", cv2.CAP_GSTREAMER)
if not cap.isOpened():
   print('Failed to open camera')

cv2.namedWindow('Demo', cv2.WINDOW_AUTOSIZE)

# Run for 300 frames
for i in range(300):
        ret_val, img = cap.read();
        if not ret_val:

        cv2.imshow('Demo', img);


Thanks for the reply! I ran your code in a brand new program, and it printed the ‘Failed to open camera’ message.

So the pipeline failed to start. Is your camera a RPi V2 cam ? Or do you have another camera ?
Try to display camera from a terminal with gst-launch:

gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1' ! nvvidconv ! xvimagesink

The camera is the onboard camera, and I have no other camera’s attached.

The camera DOES launch successfully from terminal, also using the command you provided. Do you need the output from that?

I missed you are using a TX2… If you have the onboard module, it is ok and the pipeline runs fine from gst-launch.

I’m wondering if your opencv version has gstreamer support. What gives:

import cv2

and check for GSTREAMER support in Video I/O section:

GStreamer:                   YES (1.14.5)

If not available, you would reconfigure a new opencv build enabling it with -D WITH_GSTREAMER=ON

This must be the problem: GStreamer is set to no.
I don’t remember building OpenCV from source on my Jetson, will that now be necessary? Do you have any recommendations for the TX2 specifically? I’ve run into a lot of issues building other applications from source on the Jetson, so any advice here would be appreciated.

Try on of these scripts:

1 Like