Problems with OpenCV

For this you should be able to avoid the crash by increasing swap memory. JetsonHacks has a tutorial and script too to follow where he increases the swap memory using a script he made.

But I think we should figure out what happened with your OpenCV pip installation because it might be annoying with multiple installs. Just wanted to let you know why it crashed in case you try again in the future.

1 Like

How about removing old OpenCV first?

sudo apt-get purge *libopencv*

Then try the installation again.

1 Like

Re-reading this, I think that your problem is that the video capture failed to open and your are thus getting a void frame that cannot be encoded into jpeg.

Check that capture is successfully opened with CAM1.isOpened() before trying to read from it.

1 Like

When I first got the problem, I tried to uninstall OpenCV through pip uninstall opencv-python: what’s the difference between the two command lines?

After I remove libopencv, what shoud I run to install it again? Just to be sure!

I was also thinking that OpenCV wasn’t the problem.
I will try tomorrow to write a little script, initializing the 2 cameras with the lines I wrote yesterday:

CAM1 = cv2.VideoCapture(gstreamer_pipeline(sensor_id=0, exposuretime_low=16000000, exposuretime_high=16000000), cv2.CAP_GSTREAMER)
CAM2 = cv2.VideoCapture(gstreamer_pipeline(sensor_id=1, exposuretime_low=20000000, exposuretime_high=20000000), cv2.CAP_GSTREAMER)

If the video capture will fail to open, what should I do?
As I said before, if I run the script made by JetsonHacksNano, the cameras work fine!

Thank you for the reply. I did launch the script again, but it got stuck at 96% again, so I shut down the jetson to avoid those multiple installs, as you said.
Regarding my OpenCV installation, I don’t really know what might have happened, I’m really clueless

So, I wrote this little script to test the two cameras:

import cv2
import numpy as np

def gstreamer_pipeline(
    sensor_id=0,
    capture_width=1280,
    capture_height=720,
    display_width=1280,
    display_height=720,
    framerate=30,
    flip_method=0,
    exposuretime_low=16000000,
    exposuretime_high=16000000,
):
    return (
        'nvarguscamerasrc sensor-id=%d wbmode=0 awblock=true gainrange="4 4" ispdigitalgainrange="2 2" exposuretimerange="%d %d" aelock=true ! '
        "video/x-raw(memory:NVMM), width=(int)%d, height=(int)%d, format=(string)NV12, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            sensor_id,
            exposuretime_low,
            exposuretime_high,
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )



cam1 = cv2.VideoCapture(gstreamer_pipeline(sensor_id=0, exposuretime_low=16000000, exposuretime_high=16000000), cv2.CAP_GSTREAMER)
cam2 = cv2.VideoCapture(gstreamer_pipeline(sensor_id=1, exposuretime_low=20000000, exposuretime_high=20000000), cv2.CAP_GSTREAMER)

print("CAM1: ", cam1.isOpened())
print("CAM2: ", cam2.isOpened())

and the result was the following output:

$ python camera_test.py
('CAM1: ', False)
('CAM2: ', False)

Moreover, I tried to import cv2 through from cv2 import cv2, but nothing changed, getting the error

$ python camera_test.py 
Traceback (most recent call last):
  File "prova_camere.py", line 1, in <module>
    from cv2 import cv2
ImportError: cannot import name cv2

I also got the error Module 'cv2' has no 'VideoCapture' memberwhile coding.
At the very end, I tried to run the following two command lines from terminal, getting an error both times:
Code 1:

$ gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! nvoverlaysink
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:568 Failed to create CaptureSession
Got EOS from element "pipeline0".
Execution ended after 0:00:00.004857453
Setting pipeline to PAUSED ...
Setting pipeline to READY ...
Setting pipeline to NULL ...
Freeing pipeline ...

Code 2:

$ gst-launch-1.0 nvarguscamerasrmode=0 awblock=true gainrange="8 8" ispdigitalgainrange="4 4" exposuretimerange="10000000 10000000" aelock=true sensor-id=1 sensor-mode=3 ! nvvidconv ! ximagesink
WARNING: erroneous pipeline: syntax error
  1. cv2.VideoCapture
    The error message is caused by OpenCV installation.
    Make sure that following returns as you installed.

import cv2
print (cv2.__version__)

  1. CSI-Camera
    on the Jetson Nano B01 4GB model, I tested CSI-Camera from here:
    To run dual_camera.py example, I have to modify sensor_mode=2 instead of 3.
    sensor_mode=3 causes system reboot.
    As I remember dual_camera.py works very well without any changes on the Xavier NX developer kit,
    very weird it is.
1 Like

So, I printed OpenCV2 version, and what happened is quite funny:
I basically coded that print(cv2.__version__) into my code you can find in my previous comment (before opening cam1 and cam2.
From terminal, when I run python camera_test.py the output is: 3.2.0. When I run python3 camera_test.py the output is 4.5.1 instead. I’m pretty sure I did something very wrong with OpenCV, which is causing all those problems.

I also tried to set sensor_mode = 2 in dual_camera.py, obtaining the same error when I run the code through python dual_camera.py or python3 dual_camera.py:

Unable to open any cameras
Traceback (most recent call last):
  File "dual_camera.py", line 193, in <module>
    start_cameras()
  File "dual_camera.py", line 174, in start_cameras
    _ , left_image=left_camera.read()
  File "dual_camera.py", line 88, in read
    frame = self.frame.copy()
AttributeError: 'NoneType' object has no attribute 'copy'

I think you did not remove the old version, 3.2.0 before installation of new one, 4.5.1.
OpenCV may lots of dependencies to other libraries, it’s hard to figure out what’s wrong.

I would uninstall the new one(4.5.1) first and in this case, remove 3.2.0.
Then build to install 4.5.1 again.

G’luck!

1 Like

Before I do any more damage, this is what I’d like to do:
I read that to uninstall opencv already installed in the Nano you have to run in terminal sudo apt-get purge *libopencv*.
So, my idea is to run in terminal pip uninstall opencv-python to uninstall 4.5.1: I’d like to see what happens if I just uninstall this specific version, maybe the older one (3.2.0) will work again. But I don’t know if this will cause problems to the version 3.2.0.
Should I run pip3 uninstall opencv-python or pip/pip3 uninstall opencv-python==4.5.1 in order not to cause any corruption to the older version?

Firstly, review what packages are installed on your Jetson Nano.
pip3 list

And if you install OpenCV 4.5.1 from scratch, opencv-python may not be found in the lists.
IF YES, you should run

sudo make uninstall

at the build directory.

Being a newbie with ubuntu, could you be a bit more specific on that?
If I run pip3 list I should get a list with all the packages installed, right?

What does the following mean?

I installed OpenCV 4.5.1 through pip install opencv-python==4.5.1.
If I find 4.5.1 in the list, I should go at the build directory and run sudo make uninstall opencv-python?

If I run pip3 list I should get a list with all the packages installed, right?

If you install something from the source-build, it will not.
For example, OpenCV from source, not listed.

You installed OpenCV with pip, you can uninstall it as you know. NOT at the build directory. Misunderstood!
pip uninstall opencv-python==4.5.1

1 Like

So, I ran pip uninstall opencv-python==4.5.1.
Then, I ran my previous code through python camera_test.py and the OpenCV version displayed was 3.2.0.
After, I ran in terminal pip list, but there was no opencv version: I looked for opencv and libopencv aswell: nothing.
There is definitely something wrong D:

Another question:
I’m pretty sure I used to run one of the code I needed through python3 code.py because of some packages not installed in python 2. Could it be that on my jetson nano there was the same version of Opencv installed for both python2 and 3, which was not causing any problem at all? And that when I updated the version everything stopped working?

I was reading this post, and I found this comment by dgarba:

I have the same problem. If I run a simple pipeline in the terminal the cameras works fine (even if after some tries the pipeline breaks itself and I can’t open the cameras anymore, don’t know why this happens), but if I use cv2.VideoCapture I have problems.
So, I tried to run print cv2.getBuildInformation(), and this is the output I received:

General configuration for OpenCV 3.2.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /build/opencv-XDqSFW/opencv-3.2.0+dfsg/contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2018-09-20T09:28:13Z
    Host:                        Linux 4.4.0-135-generic aarch64
    CMake:                       3.10.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                /usr/bin/c++  (ver 7.3.0)
    C++ flags (Release):         -g -O2 -fdebug-prefix-map=/build/opencv-XDqSFW/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2   -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g -O2 -fdebug-prefix-map=/build/opencv-XDqSFW/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG
    C++ flags (Debug):           -g -O2 -fdebug-prefix-map=/build/opencv-XDqSFW/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2   -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 -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/bin/cc
    C flags (Release):           -g -O2 -fdebug-prefix-map=/build/opencv-XDqSFW/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fvisibility=hidden -g -O2 -fdebug-prefix-map=/build/opencv-XDqSFW/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG
    C flags (Debug):             -g -O2 -fdebug-prefix-map=/build/opencv-XDqSFW/opencv-3.2.0+dfsg=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2   -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
    Linker flags (Debug):        -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          /usr/lib/aarch64-linux-gnu/libwebp.so gdcmMSFF /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so /usr/lib/libgdal.so gtk-3 gdk-3 pangocairo-1.0 pango-1.0 atk-1.0 cairo-gobject cairo gdk_pixbuf-2.0 gio-2.0 gobject-2.0 glib-2.0 gthread-2.0 dc1394 avcodec avformat avutil swscale avresample gphoto2 gphoto2_port /usr/lib/aarch64-linux-gnu/hdf5/serial/libhdf5.so /usr/lib/aarch64-linux-gnu/libpthread.so /usr/lib/aarch64-linux-gnu/libsz.so /usr/lib/aarch64-linux-gnu/libdl.so /usr/lib/aarch64-linux-gnu/libm.so vtkRenderingOpenGL vtkImagingHybrid vtkIOImage vtkCommonDataModel vtkCommonMath vtkCommonCore vtksys vtkCommonMisc vtkCommonSystem vtkCommonTransforms vtkCommonExecutionModel vtkDICOMParser vtkIOCore /usr/lib/aarch64-linux-gnu/libz.so vtkmetaio /usr/lib/aarch64-linux-gnu/libjpeg.so /usr/lib/aarch64-linux-gnu/libpng.so /usr/lib/aarch64-linux-gnu/libtiff.so vtkImagingCore vtkRenderingCore vtkCommonColor vtkFiltersExtraction vtkFiltersCore vtkFiltersGeneral vtkCommonComputationalGeometry vtkFiltersStatistics vtkImagingFourier vtkalglib vtkFiltersGeometry vtkFiltersSources vtkInteractionStyle vtkRenderingLOD vtkFiltersModeling vtkIOPLY vtkIOGeometry vtkFiltersTexture vtkRenderingFreeType /usr/lib/aarch64-linux-gnu/libfreetype.so vtkftgl vtkIOExport vtkRenderingAnnotation vtkImagingColor vtkRenderingContext2D vtkRenderingGL2PS vtkRenderingContextOpenGL /usr/lib/aarch64-linux-gnu/libgl2ps.so vtkRenderingLabel tesseract lept dl m pthread rt tbb
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 core flann hdf imgproc ml photo reg surface_matching video viz freetype fuzzy imgcodecs shape videoio highgui objdetect plot superres ts xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo stitching videostab ximgproc aruco optflow phase_unwrapping structured_light java python2 python3
    Disabled:                    world contrib_world
    Disabled by dependency:      tracking
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cnn_3dobj cvv dnn matlab sfm

  GUI: 
    QT:                          NO
    GTK+ 3.x:                    YES (ver 3.22.30)
    GThread :                    YES (ver 2.56.2)
    GtkGlExt:                    NO
    OpenGL support:              NO
    VTK support:                 YES (ver 6.3.0)

  Media I/O: 
    ZLib:                        /usr/lib/aarch64-linux-gnu/libz.so (ver 1.2.11)
    JPEG:                        /usr/lib/aarch64-linux-gnu/libjpeg.so (ver )
    WEBP:                        /usr/lib/aarch64-linux-gnu/libwebp.so (ver encoder: 0x020e)
    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:                   NO
    OpenEXR:                     /usr/lib/aarch64-linux-gnu/libImath.so /usr/lib/aarch64-linux-gnu/libIlmImf.so /usr/lib/aarch64-linux-gnu/libIex.so /usr/lib/aarch64-linux-gnu/libHalf.so /usr/lib/aarch64-linux-gnu/libIlmThread.so (ver 2.2.0)
    GDAL:                        /usr/lib/libgdal.so
    GDCM:                        YES (ver 2.8.4)

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  YES (ver 2.2.5)
    FFMPEG:                      YES
      avcodec:                   YES (ver 57.107.100)
      avformat:                  YES (ver 57.83.100)
      avutil:                    YES (ver 55.78.100)
      swscale:                   YES (ver 4.8.100)
      avresample:                YES (ver 3.7.0)
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    Aravis SDK:                  NO
    UniCap:                      NO
    UniCap ucil:                 NO
    V4L/V4L2:                    NO/YES
    XIMEA:                       NO
    Xine:                        NO
    gPhoto2:                     YES

  Parallel framework:            TBB (ver 2017.0 interface 9107)

  Other third-party libraries:
    Use IPP:                     NO
    Use VA:                      NO
    Use Intel VA-API/OpenCL:     NO
    Use Lapack:                  NO
    Use Eigen:                   YES (ver 3.3.4)
    Use Cuda:                    NO
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  OpenCL:                        <Dynamic loading of OpenCL library>
    Include path:                /usr/include/CL
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

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

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.6.5)
    Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.5)
    numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3)
    packages path:               lib/python3.6/dist-packages

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

  Java:
    ant:                         /usr/bin/ant (ver 1.10.3)
    JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
    Java wrappers:               YES
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Documentation:
    Doxygen:                     /usr/bin/doxygen (ver 1.8.13)

  Tests and samples:
    Tests:                       NO
    Performance tests:           YES
    C/C++ Examples:              YES

If you look carefully, you’ll notice under Video I/O:
GStreamer: NO
I also printed gstreamer version through gst-inspect-1.0 version 1.14.5, and the output was:
GStreamer 1.14.5
https://launchpad.net/distros/ubuntu/+source/gstreamer1.0

Right now I’m really confused and I don’t know what to do anymore.

pip/pip3 list only shows packages installed through pip/pip3.
Would you run
$ dpkg -l | grep opencv

Jetson has OpenCV by default, and you can install and uninstall it as you want.
But, as you install/update OpenCV through apt or pip/pip3, it will use pre-built version from the repositories.

It might be built without WITH_GSTREAMER=ON.

Anyway, you can

  1. backup what you did on your Jetson Nano, then make fresh sd image.
  2. uninstall whole OpenCV and install 4.5.1 again as the link AastaNV/JEP.

I solved the problem.
This is what I did:

I first installed the Swapfile made by JetsonHacksNano.
After that, I ran the shell script install_opencv4.5.0_Jetson.sh made by AastaNV to install correctly OpenCV with gstreamer support ON.

That solved all of my problems.
Thanks to everyone for your replies!