PiCam in OpenCV nano_built_opencv error

Hello, Developers, please help. I want to run Pi camera on my Jetson nano, but without success. I installed the file from mdegans and nano_build_opencv I also installed packages and read this: CSI-Camera Raspberry Pi v2 not work on Jetson Nano
but it still not working. GStreamer in buildinformation always stay =NO.
What could I do more, please?
in the terminal the camera work well, also when I run it with jetson.utils.videoSourse. But runnig with openCV nothing.

Information:

Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import cv2
print(cv2.getBuildInformation())

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

Platform:
Timestamp: 2021-11-20T08:59:00Z
Host: Linux 4.9.140-tegra aarch64
CMake: 3.22.0
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-10/root/usr/bin/c++ (ver 10.2.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-10/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: /lib64/libopenblas.so Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent /lib64/libpng.so /lib64/libz.so dl m pthread rt
3rdparty dependencies: libprotobuf ade ittnotify 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: QT5
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.1.0-62)
WEBP: build (ver encoder: 0x020f)
PNG: /lib64/libpng.so (ver 1.5.13)
TIFF: build (ver 42 - 4.2.0)
JPEG 2000: build (ver 2.4.0)
OpenEXR: build (ver 2.3.0)
HDR: YES
SUNRASTER: YES
PXM: YES
PFM: YES

Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.91.100)
avformat: YES (58.45.100)
avutil: YES (56.51.100)
swscale: YES (5.7.100)
avresample: NO
GStreamer: NO
v4l/v4l2: YES (linux/videodev2.h)

Parallel framework: pthreads

Trace: YES (with Intel ITT)

Other third-party libraries:
Lapack: YES (/lib64/libopenblas.so)
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-bbq4c33o/opencv/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python 3:
Interpreter: /opt/_internal/cpython-3.6.15/bin/python (ver 3.6.15)
Libraries: libpython3.6m.a (ver 3.6.15)
numpy: /tmp/pip-build-env-g91cluvq/overlay/lib/python3.6/site-packages/numpy/core/include (ver 1.19.3)
install path: python/cv2/python-3.6

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

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

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

this is the string:
gst-launch-1.0 nvarguscamerasrc ! ‘video/x-raw(memory:NVMM),width=3264, height=2464, framerate=21/1, format=NV12’ ! nvvidconv flip-method=2 ! ‘video/x-raw,width=960, height=616’ ! nvvidconv ! nvegltransform ! nveglglessink -e

this is the code:
import cv2
import jetson.inference
import jetson.utils
dispW=1280 # 1280
dispH=960 # 960
flip=2

#vid= ‘/home/nanorobo/Desktop/Python_projects/video.mp4’
#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’

#cam=jetson.utils.gstCamera(dispW,dispH,’/dev/video0’)
#camSet=“nvarguscamerasrc sensor_id=0 ! video/x-raw(memory:NVMM),width=3280, height=2464, framerate=21/1, format=NV12’ ! nvvidconv flip-method=0 ! ‘video/x-raw,width=960, height=720’ ! nvvidconv ! nvegltransform ! nveglglessink -e”
#camSet=" nvarguscamerasrc ! ‘video/x-raw(memory:NVMM),width=1280, height=720, framerate=60/1, format=NV12’ ! nvvidconv flip-method=0 ! ‘video/x-raw, width=1280, height=720, format=BGRx’ ! videoconvert ! ‘video/x-raw, format=BGR’ ! identity silent=false ! fakesink -e"
#camSet= " nvarguscamerasrc ! ‘video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)NV12, framerate=(fraction)30/1’ ! nvvidconv videoconvert ! appsink"

#camSet=“nvarguscamerasrc ! ‘video/x-raw(memory:NVMM),width=3264, height=2464, framerate=21/1, format=NV12’ ! nvvidconv flip-method=2 ! ‘video/x-raw,width=960, height=616’ ! nvvidconv ! nvegltransform ! nveglglessink -e”

camSet=“nvarguscamerasrc ! ‘video/x-raw(memory:NVMM),width=3264, height=2464, framerate=21/1, format=NV12’ ! nvvidconv flip-method=2 ! ‘video/x-raw,width=960, height=616’ ! nvvidconv ! nvegltransform ! nveglglessink -e”
#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’
cam=cv2.VideoCapture(camSet, cv2.CAP_GSTREAMER)

#cam=cv2.VideoCapture(“nvarguscamerasrc ! nvvidconv ! video/x-raw, format=BGRx’ ! videoconvert ! video/x-raw, format=BGR ! appsink”, cv2.CAP_GSTREAMER)

#cam = jetson.utils.videoSource(“csi://0”)

#cam=jetson.utils.gstCamera(dispW,dispH,“csi://0”)
#cam=cv2.VideoCapture(“csi://0”,cv2.CAP_GSTREAMER)
#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

#cam=cv2.VideoCapture(’/dev/video0’)
while True:
#img, width, height= cam.CaptureRGBA()
ret_val, frame= cam.read()
cv2.imshow(‘piCam’,frame)
if cv2.waitKey(1)==ord(‘q’):
break
cam.release()
cv2.destroyAllWindows()

this is the error:

File “/home/nanorobo/Desktop/Python_projects/object detection/opiti_picam.py”, line 38, in
cv2.imshow(‘piCam’,frame)
cv2.error: OpenCV(4.5.4) /tmp/pip-req-build-bbq4c33o/opencv/modules/highgui/src/window.cpp:1006: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

Hi,
Please check if you can run this gst-launch-1.0 command and see camera preview:

$ gst-launch-1.0 nvarguscamerasrc ! nvoverlaysink

If above command can be run successfully, please try this python sample:
OpenCV Video Capture with GStreamer doesn't work on ROS-melodic - #3 by DaneLLL

Hello, DaneLLL. Thank you for your answer! When I put the text in the terminal it is ok, but the code in VSCode isn’t, It gives me this error:

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

OpenCV(4.5.4) /tmp/pip-req-build-bbq4c33o/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’

camera open failed

Hi,
From the error it looks like the OpenCV does not enable gstreamer. Probably it loads another version of OpenCV in the environment. Would suggest check this.

Hello how to check this? I read this article too Problems with OpenCV - #15 by notprime and made the installation with AastaNV · GitHub and now is GStremer=ON, but it still make error:

the code:

import cv2
import numpy as np
import os
dispW=1280 # 1280
dispH=960 # 960
flip=2

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’
#camSet=‘nvarguscamerasrc ! video/x-raw(memory:NVMM), width=1920, height=1080, format=NV12, framerate=29/1 ! nvvidconv flip-method=’+str(flip)+’ ! video/x-raw, width=’+str(dispW)+’, height=’+str(dispH)+’, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink’

cam=cv2.VideoCapture(camSet)

while True:
ret, frame=cam.read()
cv2.imshow(‘piCam’,frame)
if cv2.waitKey(1)==ord(‘q’):
break
cam.release()
cv2.destroyAllWindows()

the error:

(Argus) Error Timeout: (propagating from src/rpc/socket/client/SocketClientDispatch.cpp, function openSocketConnection(), line 219)
(Argus) Error Timeout: Cannot create camera provider (in src/rpc/socket/client/SocketClientDispatch.cpp, function createCameraProvider(), line 106)
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:720 Failed to create CameraProvider
[ WARN:0] global /home/nanorobo/open_Aasta/JEP/script/workspace/opencv-4.5.0/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Traceback (most recent call last):
File “/home/nanorobo/Desktop/Python_projects/pi_cam/pi_cam.py”, line 18, in
cv2.imshow(‘piCam’,frame)
cv2.error: OpenCV(4.5.0) /home/nanorobo/open_Aasta/JEP/script/workspace/opencv-4.5.0/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

Hi @user50863 ,

when passing the parameters dispW, dishH and flip, try not to u se the str method.
So, trey to use the following pipeline:

camSet = 'nvarguscamerasrc !
         "video/x-raw(memory:NVMM), width=(int)3264, height=(int)2464, format=(string)NV12, "
         "framerate=(fraction)21/1 ! "
         "nvvidconv flip-method=flip ! "
         "video/x-raw, width=(int)dispW, height=(int)dispH, format=(string)BGRx ! "
         "videoconvert ! "
         "video/x-raw, format=(string)BGR ! appsink"

Let us know what happens!

Hello! Thank you very much for your answer! Today I just tried again, without any changes in code or install from last time and got new errors. Very stranges.

The error:

Traceback (most recent call last):
File “/home/nanorobo/Desktop/Python_projects/pi_cam/pi_cam.py”, line 1, in
import cv2
File “/usr/local/lib/python3.6/dist-packages/cv2/init.py”, line 96, in
bootstrap()
File “/usr/local/lib/python3.6/dist-packages/cv2/init.py”, line 86, in bootstrap
import cv2
ImportError: /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so: undefined symbol: _ZTIN2cv3dnn14dnn4_v202009085LayerE

Hello notprime again, i solved last error just with upgrade, but now in information for cv2 in cv2.getBuildInformation() , Gstreamer=NO again, and again the old error:

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

OpenCV(4.5.4) /tmp/pip-req-build-bbq4c33o/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=1920, height=1080, format=NV12, framerate=20/1 ! nvvidconv flip-method=2 ! video/x-raw, width=1280, height=960, format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink in function ‘icvExtractPattern’

Traceback (most recent call last):
File “/home/nanorobo/Desktop/Python_projects/pi_cam/pi_cam.py”, line 22, in
cv2.imshow(‘piCam’,frame)
cv2.error: OpenCV(4.5.4) /tmp/pip-req-build-bbq4c33o/opencv/modules/highgui/src/window.cpp:1006: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’

I cant understand what happen,something between GStreamer, openCV 4.5.4 and JetPack