OpenCV VideoCapture is not wokring

When I try to use python code as followings, I got errors belows.

cv2.VideoCapture(0)
[ WARN:0@13.146] global cap_v4l.cpp:1119 tryIoctl VIDEOIO(V4L2:/dev/video0): select() timeout.

Also, this command works as well.

$ gst-launch-1.0 nvarguscamerasrc sensor-id=0 ! ‘video/x-raw(memory:NVMM),height=640,width=480,framerate=30/1’ ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! queue ! appsink drop=1
Setting pipeline to PAUSED …
Pipeline is live and does not need PREROLL …
Setting pipeline to PLAYING …
New clock: GstSystemClock
GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected…
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 1920 x 1536 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.122018, max 6.309574; Exposure Range min 16000, max 660000000;

GST_ARGUS: Running with following settings:
Camera index = 0
Camera mode = 0
Output Stream W = 1920 H = 1536
seconds to Run = 0
Frame Rate = 29.999999
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.

But, the following Python code is not working.

cv2.VideoCapture(“gst-launch-1.0 nvarguscamerasrc ! ‘video/x-raw(memory:NVMM), width=3840, height=2162, framerate=30/1, format=NV12’ ! nvvidconv flip-method=0 ! ‘video/x-raw,width=1920, height=1080’ ! nvvidconv ! nvegltransform ! nveglglessink -e”, cv2.CAP_GSTREAMER)

How could I modify it?

This is my CSI camera information.

$ v4l2-ctl --all
Driver Info:
Driver name : tegra-video
Card type : vi-output, imx623 32-0044
Bus info : platform:tegra-capture-vi:4
Driver version : 5.10.104
Capabilities : 0x84200001
Video Capture
Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04200001
Video Capture
Streaming
Extended Pix Format
Media Driver Info:
Driver name : tegra-camrtc-ca
Model : NVIDIA Tegra Video Input Device
Serial :
Bus info :
Media version : 5.10.104
Hardware revision: 0x00000003 (3)
Driver version : 5.10.104
Interface Info:
ID : 0x03000017
Type : V4L Video
Entity Info:
ID : 0x00000015 (21)
Name : vi-output, imx623 32-0044
Function : V4L2 I/O
Pad 0x01000016 : 0: Sink
Link 0x0200001b: from remote pad 0x100000f of entity ‘13e40000.host1x:nvcsi@15a00000-’: Data, Enabled
Priority: 2
Video input : 0 (Camera 4: no power)
Format Video Capture:
Width/Height : 1920/1536
Pixel Format : ‘YUYV’
Field : None
Bytes per Line : 3840
Size Image : 5898240
Colorspace : sRGB
Transfer Function : Default (maps to sRGB)
YCbCr/HSV Encoding: Default (maps to ITU-R 601)
Quantization : Default (maps to Limited Range)
Flags :

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

Thanks for your answer, however, vc.isOpened() got False after it.

vc = 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 ! appsink”, cv2.CAP_GSTREAMER)

Hi,
It looks like gstreamer is not enabled in the OpenCv package. Do you use OpenCV package installed through SDKManager? Or you manually build the packge?

I use OpenCv through SDKManager.

Hi,
It seems like you don’t run the python sample. We would suggest try to run it directly. It is supposed to work well if camera source is valid.

This is my Python code.

    self.vc = 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 !  appsink", cv2.CAP_GSTREAMER)
    if self.vc.isOpened():
        print(self.index)
        self.terminated = False
        while True:
            rval, frame = self.vc.read()
            print(rval)
            if rval is False:
                break
            print(frame)
            image = QImage(frame, frame.shape[1], frame.shape[0], frame.shape[1] * 3, QImage.Format_RGB888)
            self.frame_signal.emit(self.index, image)
            key = cv2.waitKey(30)
            if self.terminated:
                break
    else:
        **print("Release")**
        self.vc.release()

And, its result is the following:

$ python Diagnosis.py
Release

Python Console:

$ python
Python 3.8.10 (default, May 26 2023, 14:05:08)
[GCC 9.4.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import cv2
vc = 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 ! appsink”, cv2.CAP_GSTREAMER)
print(vc.isOpened())
False

Hi,
Does it work if you remove cv2.CAP_GSTREAMER in cv2.VideoCapture()? Not sure if it is required in python.

I got the following error.

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 ! appsink”)
[ERROR:0@1522.631] global cap.cpp:164 open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.8.0) /io/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): 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 ! appsink in function ‘icvExtractPattern’

< cv2.VideoCapture 0xffffa5ed6130>

Hi,
Your OpenCV is 4.8.0 and gstreamer is disabled. If you use the package installed through SDKManager, it it this version:

nvidia@tegra-ubuntu:~$ python3
Python 3.8.10 (default, May 26 2023, 14:05:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.5.4'
>>>

I reinstall opencv 4.5.4, but result is same.
Should I install it from SDKManager? Also, is there another way to enable?

cv2.version
‘4.5.4’
self.vc = 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 ! appsink”)
[ERROR:0] global /tmp/pip-req-build-frgo6bcn/opencv/modules/videoio/src/cap.cpp (164) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.4) /tmp/pip-req-build-frgo6bcn/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): 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 ! appsink in function ‘icvExtractPattern’

Hi,
Please run

$ sudo apt purge opencv

And install SDK Components through SDKManager. This should reset to use default package.

Working log will be like:

$ python3 ./ocv_nvarguscamerasrc.py
GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 3840 x 2160 FR = 59.999999 fps Duration = 16666667 ; Analog Gain rang
e min 1.000000, max 44.400002; Exposure Range min 44000, max 478696000;

GST_ARGUS: 1920 x 1080 FR = 59.999999 fps Duration = 16666667 ; Analog Gain rang
e min 1.000000, max 177.000000; Exposure Range min 58000, max 184611000;

GST_ARGUS: 3840 x 2160 FR = 29.999999 fps Duration = 33333334 ; Analog Gain rang
e min 1.000000, max 30.000000; Exposure Range min 864000, max 20480000;

GST_ARGUS: 1920 x 1080 FR = 59.999999 fps Duration = 16666667 ; Analog Gain rang
e min 1.000000, max 177.000000; Exposure Range min 859000, max 15649000;

GST_ARGUS: Running with following settings:
   Camera index = 0
   Camera mode  = 3
   Output Stream W = 1920 H = 1080
   seconds to Run    = 0
   Frame Rate = 59.999999
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
[ WARN:0] global /home/ubuntu/build_opencv/opencv/modules/videoio/src/cap_gstrea
mer.cpp (1100) open OpenCV | GStreamer warning: Cannot query video position: sta
tus=0, value=-1, duration=-1
Gtk-Message: 09:05:08.652: Failed to load module "canberra-gtk-module"

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.