OpenCV error

Hello, I am using raspberry pi camera with Jetson nano, while I am trying the following code an error occur and could not find the exact problem, my opencv version is 4.5.1

import cv2
print(cv2.__version__)
dispW=640
dispH=480
flip=4
camSet='nvarguscamerasrc !  video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1 ! nvvidconv flip-method='+str(flip)+' ! video/x-raw, width='+str(dispW)+', height='+str(dispH)+', format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink'
  1. cam= cv2.VideoCapture(camSet)
    
  2. print(cam.isOpened())
    
  3. while(True):
    
  4.     # Capture frame-by-frame
    
  5.     ret,frame = cam.read()
    
  6.     # Display the resulting frame
    
  7.     cv2.imshow('frame',frame)
    
  8.     if cv2.waitKey(1) == ord('q'):
    
  9.         break
    
  10. # When everything done, release the capture
    
  11. cam.release()
    
  12. cv2.destroyAllWindows() 
    

this is the error :

4.5.1
[ERROR:0] global /tmp/pip-req-build-zuuo394f/opencv/modules/videoio/src/cap.cpp (140) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.5.1) /tmp/pip-req-build-zuuo394f/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=3264, height=2464, format=NV12, framerate=21/1 ! nvvidconv flip-method=4 ! video/x-raw, width=640, height=480, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink in function ‘icvExtractPattern’

False
Traceback (most recent call last):
File “testcamera.py”, line 20, in
cv2.imshow(‘frame’,frame)
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-zuuo394f/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

Hi,
Please check if you can run the command and see camera preview:

$ export DISPLAY=:0
$ gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1' ! nvegltransform ! nveglglessink

And then try

$ gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1' ! nvvidconv flip-method=4 ! video/x-raw, width=640, height=480, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! fakesink

Seems the videoCapture used the wrong backend (CV_IMAGES).
You would try to specify gstreamer backend with:

cam= cv2.VideoCapture(camSet, cv2.CAP_GSTREAMER)

If this doesn’t work, check that your opencv build has gstreamer support with:

print(cv2.getBuildInformation())

I executed this code in the terminal and the camera opened successfully:

$ export DISPLAY=:0
$ gst-launch-1.0 nvarguscamerasrc ! ‘video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1’ ! nvegltransform ! nveglglessink

but this code didnot open the camera:

> $ gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1' ! nvvidconv flip-method=4 ! video/x-raw, width=640, height=480, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! fakesink
> ```

it kept showing these lines:
> 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: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
> 
> GST_ARGUS: 3264 x 1848 FR = 28.000001 fps Duration = 35714284 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
> 
> GST_ARGUS: 1920 x 1080 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
> 
> GST_ARGUS: 1640 x 1232 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
> 
> GST_ARGUS: 1280 x 720 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
> 
> GST_ARGUS: 1280 x 720 FR = 120.000005 fps Duration = 8333333 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
> 
> GST_ARGUS: Running with following settings:
>    Camera index = 0 
>    Camera mode  = 0 
>    Output Stream W = 3264 H = 2464 
>    seconds to Run    = 0 
>    Frame Rate = 21.000000 
> GST_ARGUS: Setup Complete, Starting captures for 0 seconds
> GST_ARGUS: Starting repeat capture requests.
> CONSUMER: Producer has connected; continuing.

First of all thanks for your help, I tried it but nothing changed, and this is the result of printing the build information:

4.5.1
False

General configuration for OpenCV 4.5.1 =====================================
Version control: 4.5.1-dirty

Platform:
Timestamp: 2021-01-02T12:47:39Z
Host: Linux 5.4.0-54-generic aarch64
CMake: 3.18.4
CMake generator: Unix Makefiles
CMake build tool: /bin/gmake
Configuration: Release

CPU/HW features:
Baseline: NEON FP16

C/C++:
Built as dynamic libs?: NO
C++ standard: 11
C++ Compiler: /opt/rh/devtoolset-9/root/usr/bin/c++ (ver 9.3.1)
C++ flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /opt/rh/devtoolset-9/root/usr/bin/cc
C flags (Release): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -Wl,-strip-all -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -L/root/ffmpeg_build/lib -Wl,–gc-sections -Wl,–as-needed
Linker flags (Debug): -L/root/ffmpeg_build/lib -Wl,–gc-sections -Wl,–as-needed
ccache: YES
Precompiled headers: NO
Extra dependencies: ade Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent /lib64/libpng.so /lib64/libz.so dl m pthread rt
3rdparty dependencies: ittnotify libprotobuf libjpeg-turbo libwebp libtiff libopenjp2 IlmImf quirc tegra_hal

OpenCV modules:
To be built: calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
Disabled: world
Disabled by dependency: -
Unavailable: java python2 ts
Applications: -
Documentation: NO
Non-free algorithms: NO

GUI:
QT: YES (ver 5.15.0)
QT OpenGL support: NO
GTK+: NO
VTK support: NO

Media I/O:
ZLib: /lib64/libz.so (ver 1.2.7)
JPEG: libjpeg-turbo (ver 2.0.6-62)
WEBP: build (ver encoder: 0x020f)
PNG: /lib64/libpng.so (ver 1.5.13)
TIFF: build (ver 42 - 4.0.10)
JPEG 2000: build (ver 2.3.1)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.112.103)
avformat: YES (58.64.100)
avutil: YES (56.60.100)
swscale: YES (5.8.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)

Parallel framework: pthreads

Trace: YES (with Intel ITT)

Other third-party libraries:
Lapack: NO
Eigen: NO
Custom HAL: YES (carotene (ver 0.0.1))
Protobuf: build (3.5.1)

OpenCL: YES (no extra features)
Include path: /tmp/pip-req-build-zuuo394f/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python 3:
Interpreter: /opt/python/cp36-cp36m/bin/python (ver 3.6.12)
Libraries: libpython3.6m.a (ver 3.6.12)
numpy: /tmp/pip-build-env-c03ewnak/overlay/lib/python3.6/site-packages/numpy/core/include (ver 1.19.3)
install path: python

Python (for build): /bin/python2.7

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Install to: /tmp/pip-req-build-zuuo394f/_skbuild/linux-aarch64-3.6/cmake-install

Traceback (most recent call last):
File “testcamera.py”, line 20, in
cv2.imshow(‘frame’,frame)
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-zuuo394f/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

It looks it did successfully open the camera. Obviously, as there is no display (fakesink) you cannot see it.
You may further check with:

gst-launch-1.0 nvarguscamerasrc ! 'video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1' ! nvvidconv flip-method=4 ! video/x-raw, width=640, height=480, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! videoconvert ! xvimagesink

This may be the root cause of your problem. You would rebuild opencv with gstreamer support.
I’d suggest to use the following script:

1 Like

sorry for the inconvenience, I am newbie to the jetson nano.
Just want to make sure of steps before doing it, should I download the opencv.sh file and run the 2 codes mentioned in the page?

Second question is , in the following code it is written “4.4.0”, my opencv version is 4.5.1, should I change the number or it will cause failure in the script?

./build_opencv.sh 4.4.0

Yes, if you want to build opencv 4.5.1, just run the script with:

./build_opencv.sh 4.5.1

I run the script but unfortunately it did not work:

4.4.0
[ WARN:0] global /tmp/build_opencv/opencv/modules/videoio/src/cap_gstreamer.cpp (803) open OpenCV | GStreamer warning: cannot find appsink in manual pipeline
[ WARN:0] global /tmp/build_opencv/opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
False

General configuration for OpenCV 4.4.0 =====================================
Version control: 4.4.0

Extra modules:
Location (extra): /tmp/build_opencv/opencv_contrib/modules
Version control (extra): 4.4.0

Platform:
Timestamp: 2021-05-21T13:12:45Z
Host: Linux 4.9.201-tegra aarch64
CMake: 3.10.2
CMake generator: Unix Makefiles
CMake build tool: /usr/bin/make
Configuration: RELEASE

CPU/HW features:
Baseline: NEON FP16
required: NEON
disabled: VFPV3

C/C++:
Built as dynamic libs?: YES
C++ standard: 11
C++ Compiler: /usr/bin/c++ (ver 7.5.0)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
C Compiler: /usr/bin/cc
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,–gc-sections -Wl,–as-needed
Linker flags (Debug): -Wl,–gc-sections -Wl,–as-needed
ccache: NO
Precompiled headers: NO
Extra dependencies: m pthread cudart_static -lpthread dl rt nppc nppial nppicc nppicom nppidei nppif nppig nppim nppist nppisu nppitc npps cublas cudnn cufft -L/usr/local/cuda/lib64 -L/usr/lib/aarch64-linux-gnu
3rdparty dependencies:

OpenCV modules:
To be built: alphamat aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
Disabled: world
Disabled by dependency: -
Unavailable: cnn_3dobj cvv java js julia matlab ovis sfm ts viz
Applications: apps
Documentation: NO
Non-free algorithms: YES

GUI:
GTK+: YES (ver 3.22.30)
GThread : YES (ver 2.56.4)
GtkGlExt: NO
OpenGL support: NO
VTK support: NO

Media I/O:
ZLib: /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
JPEG: /usr/lib/aarch64-linux-gnu/libjpeg.so (ver 80)
WEBP: build (ver encoder: 0x020f)
PNG: /usr/lib/aarch64-linux-gnu/libpng.so (ver 1.6.34)
TIFF: /usr/lib/aarch64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
JPEG 2000: build Jasper (ver 1.900.1)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: YES (2.2.5)
FFMPEG: YES
avcodec: YES (57.107.100)
avformat: YES (57.83.100)
avutil: YES (55.78.100)
swscale: YES (4.8.100)
avresample: YES (3.7.0)
GStreamer: YES (1.14.5)
v4l/v4l2: YES (linux/videodev2.h)

Parallel framework: pthreads

Trace: YES (with Intel ITT)

Other third-party libraries:
Lapack: YES (/usr/lib/aarch64-linux-gnu/liblapack.so /usr/lib/aarch64-linux-gnu/libcblas.so /usr/lib/aarch64-linux-gnu/libatlas.so)
Eigen: YES (ver 3.3.4)
Custom HAL: YES (carotene (ver 0.0.1))
Protobuf: build (3.5.1)

NVIDIA CUDA: YES (ver 10.2, CUFFT CUBLAS FAST_MATH)
NVIDIA GPU arch: 53 62 72
NVIDIA PTX archs:

cuDNN: YES (ver 8.0)

OpenCL: YES (no extra features)
Include path: /tmp/build_opencv/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python 2:
Interpreter: /usr/bin/python2.7 (ver 2.7.17)
Libraries: /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.17)
numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
install path: lib/python2.7/dist-packages/cv2/python-2.7

Python 3:
Interpreter: /usr/bin/python3 (ver 3.6.9)
Libraries: /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.9)
numpy: /usr/local/lib/python3.6/dist-packages/numpy/core/include (ver 1.19.4)
install path: lib/python3.6/dist-packages/cv2/python-3.6

Python (for build): /usr/bin/python2.7

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Install to: /usr/local

Traceback (most recent call last):
File “trycamera.py”, line 19, in
cv2.imshow(‘frame’,frame)
cv2.error: OpenCV(4.4.0) /tmp/build_opencv/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

The pipeline string was probably not correct, as gstreamer failed to find appsink that should be the sink of the pipeline.
As a result, the videoCapture failed to open, and you are reading empty frames, that cannot be displayed as say the last messages.

So are there any solutions could I try?

Check your pipeline string. You may post it here.

Hi,
The log in the comment looks good. Do you check the video output?

And is your Jetson Nano with emmc or sdcard slot? The emmc storage is 16GB and is not enough for rebuilding OpenCV. The default OpenCV 4.1.1 has gstreamer enabled and should work fine.

Could you please explain more, how should I check it?
P.S Sorry for the late reply

My jetson nano is with sd card slot

First try this pipeline that should allow to read the CSI camera into opencv:

camSet = "nvarguscamerasrc ! video/x-raw(memory:NVMM),format=NV12,width=640,height=480,framerate=30/1 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1 "

and try to change from this.
If you get stcuk, post your pipeline and error messages here.

Either your opencv build did not install into python3, or you have a customized python3 installation and it has been installed in a different directory than the actually looked one.

I have poor experience with python setup and may not be able to help much further.
I can just share my own case:

# Get your python3 version
$ python3 --version
Python 3.6.9

# Find the lib that provides python bindings for opencv in your python 3.6 dist-packages.
# Note that depending on your setup you it may not be in /usr/lib/ but may also be in /usr/local/lib/
$find /usr/lib/python3.6/dist-packages/ -name 'cv2*.so' 
/usr/lib/python3.6/dist-packages/cv2.old/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so
1 Like

The problem has been solved by reformatting the sd card and installing a new image. Also, before deleting the old image I noticed that a lot of OpenCV packages were installed outside python 3.6 or python 2 folders, not sure if that was the problem.
Thanks all for helping me out, specially @Honey_Patouceul much appreciate your help.

2 Likes