install OpenCV for python3 in Jetson Nano

The default python version in Jetson Nano is 2.7, but the official installation instruction for tensorflow is python 3. when I write the following code in python 3:

import cv2

It throws error:

ModuleNotFoundError: No module named 'cv2'

so I install opencv using pip or pip3:

pip install opencv-python

I got the following error:

Collecting opencv-python
  ERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none)
ERROR: No matching distribution found for opencv-python

How to install opencv in python 3?

2 Likes

Hi,

OpenCV for both python2 and python3 is pre-installed on Nano.
Please remember that python3 is executed with $ python3 ā€¦ .

nvidia@nano:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.1'

Thanks.

1 Like

sorry, in my jetson nano:

alex@alex-jetson-nano:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: numpy.core.multiarray failed to import

I also create a virtual python environment tensorflow-gpu, when import cv2:

alex@alex-jetson-nano:~$ workon tensorflow-gpu
(tensorflow-gpu) alex@alex-jetson-nano:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

I solved it by:

sudo apt-get install python3-opencv
2 Likes

I already had OpenCV installed in the default image. However within a virtualenv ( created via virtualenv -p python3 env --system-site-packages), it was unable to import cv2 from the default image.

Doing sudo apt-get install python3-opencv made cv2 available within the virtualenv, but it downgraded the openCV version as shown below.

This downgraded OpenCV version for me

jithu@nano:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.1'

xxx@nano:$ sudo apt-get install python3-opencv
>>> 
jithu@nano:~$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>>

I believe doing so will lose the H/W acceleration capabilities.

Hi moderators how to make cv2 module (from the default image) available within the virtualenv environment ? (created with --system-site-packages) option

3 Likes

how to exclude opencv during the time of installation of nano OS with sdkmanager?
To make cv2 module available within virtual environment you could use symlink probably and create cv2.so file within the virtual environment folder. However, since you are using the default system wide installation that is coherent and supports both python2 and python3 with opencv you may go on with it, and otherwise you might manage to get the cv2.so mapping to work on itself. Though I would do a separate installation with cmake explicitly configured for use of a specific virtual environment.
Smth. like below might work, in my opinion:

cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF  -D OPENCV_EXTRA_MODULES_PATH=/home/nvidia/opencv_contrib-3.4.6/modules  -D PYTHON3_EXECUTABLE=/home/nvidia/venv/bin/python PYTHON3_NUMPY_INCLUDE_DIRS=/home/nvidia/venv/lib/python3.6/site-packages/numpy/core/include/ BUILD_opencv_python3=yes -D PYTHON_PACKAGES_PATH=~/home/nvidia/venv/lib/python3.6/site-packages/ -D PYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so  ..
1 Like

C

No way, in my case there is still the same error

Python 3.7.1 | packaged by conda-forge | (default, Feb 26 2019, 04:21:53) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

Even I tried installing both versions for python3 and python. Can any one help? How to deal with this?

@davronsherbaev

It looks like youā€™re using Anaconda. Ananconda doesnā€™t store itā€™s packages in the same location as the system python which you can modify with ā€œapt-get install python-somethingā€ or ā€œsudo pip install somethingā€.

Tbh I donā€™t use Anaconda much, but I believe you add and remove packages to Anaconda using the conda install command.

ā€¦ however there is probably not a version of OpenCV with cuda support built in that works on Nano. You would have to build it yourself by specifying the location to the anaconda python path when you build OpenCV.

Really, if you want to use python and accelerated OpenCV on nano, the easiest thing to do is to either use OpenCV that the SDK Manager installs for you (which I belive has CUDA support built in), or to build it yourself using one of these scripts:

4.3.0:

4.0.1:
https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.0.0_Nano.sh

Yeah, thatā€™s a problem. Easily fixed though with:

sudo apt-get remove python3-opencv
1 Like

Mine got no cv2 after ā€˜sudo apt-get install python3-opencvā€™ is done.
Why?

======
$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type ā€œhelpā€, ā€œcopyrightā€, ā€œcreditsā€ or ā€œlicenseā€ for more information.

import cv2
Traceback (most recent call last):
File ā€œā€, line 1, in
ModuleNotFoundError: No module named ā€˜cv2ā€™

The above cv2 module not found is in virtualenvwrapper environment.
cv2 module exists in non-virtualenv environment.
Why?
How to solve cv2 module not found in virtualenv environment?

sudo apt-get install python3-opencv fixed the cv2 not found.
Two things, 1.-why would it brake if it was working all right?

2.-
from jetcam.usb_camera import USBCamera
camera = USBCamera(width=224, height=224, capture_device=0)

gives me ā€œCould not initialize cameraā€

But Cheese can open the cameraā€¦

How to install OpenCV 4.1 in Jetson Nano.

sudo apt-get install python3-opencv does not install latest version.

That command will install the version built and bundled by Canonical, which is not the latest version and does not have CUDA support. 3.x-4.x can be installed with this script:

3 Likes

I made an article that explains how to compile the latest version of opencv on jetson nano

1 Like

Awesome!!! Very thorough and useful. Thanks a lot for sharing!!!

One final noteā€¦ Followed all the procedure stated here:

https://pythops.com/post/compile-deeplearning-libraries-for-jetson-nano

However, the library is not correctly linked.

As per

https://docs.donkeycar.com/guide/robot_sbc/setup_jetson_nano/

it is necessary to create a link to it

# Go to the folder where OpenCV's native library is built
cd /usr/local/lib/python3.6/site-packages/cv2/python-3.6
# Rename
mv cv2.cpython-36m-xxx-linux-gnu.so cv2.so
# Go to your virtual environments site-packages folder if previously set
cd ~/env/lib/python3.6/site-packages/
# Or just go to your home folder if not set a venv site-packages folder
cd ~
# Symlink the native library
ln -s /usr/local/lib/python3.6/site-packages/cv2/python-3.6/cv2.so cv2.so

NOTE that it is almost mandatory to create a virtual environment in order to properly install tensorflow, scipy and keras, and always a best practice.

1 Like

@emiliano

Iā€™m not sure that you need add these modifications. Something is wrong with your system.
Did you try simply to recreate the ld.so.cache cache by running ldconfig command ?

I am using venv wrapper and this solved my problem. @pythops your article is good for non-venv/native but does not work without linking with venvs. Thanks everyoneā€¦ Oh, by the way this breaks non-venv cv2 and makes it throw ā€œImportError: ./cv2.so: undefined symbol: PyUnicode_FromStringā€. It would be nice to explain how to fix the native environment.

I used your script for installing OpenCV 4.3.0 and Iā€™m glad to say that it worked first time!

I had tried several times before with various installation scripts but always ran out of memory at 100% of build. I donā€™t know why yours was so effective but apart from it running all night I had no problems.

Thanks for your effort.

1 Like