How to get camera screen with python

I’m using OpenCV 3.2.0 and when I print out “cv2.version” in python, it prints 3.2.0 correctly.

When I use

cap = cv2.VideoCapture("nvcamerasrc ! 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")

This code to open the camera, it fails with this error message.

GStreamer: Error opening bin: no element "nvcamerasrc"

When I use this command in command line, it works perfectly.

gst-launch-1.0 nvcamerasrc fpsRange="30.0 30.0" ! 'video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)I420, framerate=(fraction)30/1' ! \
nvvidconv flip-method=2 ! 'video/x-raw(memory:NVMM), format=(string)I420' ! nvoverlaysink -e

Can you help me fix this? Thank you.


Thanks for your question.

Do you enable gstreamer component when building opencv?


Please help us check the cmake summary report first.
Make sure all the gstreamer components are enabled.

--   Video I/O:
--     GStreamer:                   
--       base:                      YES (ver 1.8.3)
--       video:                     YES (ver 1.8.3)
--       app:                       YES (ver 1.8.3)
--       riff:                      YES (ver 1.8.3)
--       pbutils:                   YES (ver 1.8.3)

I didn’t compile with gstreamer option. I actually followed even though I installed newer version, and that may made this error. I’ll try to build again with that cmake command. Thank you!

--     GStreamer:                   
--       base:                      YES (ver 0.10.36)
--       video:                     YES (ver 0.10.36)
--       app:                       YES (ver 0.10.36)
--       riff:                      YES (ver 0.10.36)
--       pbutils:                   YES (ver 0.10.36)

GStreamer was on, but the same error occurred again. I think it’s because of the old version of gstreamer (libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev)

So I uninstalled them and reinstalled gstremer 1.0

sudo apt purge libgstreamer*
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

Now, I get the correct version of gstreamer at cmake output. I’m building OpenCV again and hope it works now.

It worked! Thank you so much!