Process to install OpenCV 4-1 on Nano

Dear all,

I did some tests to install OpenCV-4.1 on the Jetson Nano, and this is the process to achieve the installation:

1- Updating the packages:

sudo apt update
sudo apt install -y build-essential cmake git libgtk2.0-dev pkg-config  libswscale-dev libtbb2 libtbb-dev
sudo apt install -y python-dev python3-dev python-numpy python3-numpy
sudo apt install -y curl

2- Install video & image formats:

sudo apt install -y  libjpeg-dev libpng-dev libtiff-dev libjasper-dev 
sudo apt install -y libavcodec-dev libavformat-dev
sudo apt install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt install -y libv4l-dev v4l-utils qv4l2 v4l2ucp libdc1394-22-dev

3- Download OpenCV & Contribs Modules:

curl -L https://github.com/opencv/opencv/archive/4.1.0.zip -o opencv-4.1.0.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.zip -o opencv_contrib-4.1.0.zip

4- Unzipping packages:

unzip opencv-4.1.0.zip 
unzip opencv_contrib-4.1.0.zip 
cd opencv-4.1.0/

5- Create directory:

mkdir release
cd release/

6- Build OpenCV using Cmake:

cmake -D WITH_CUDA=ON \
    	-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules \
    	-D WITH_GSTREAMER=ON \
    	-D WITH_LIBV4L=ON \
    	-D BUILD_opencv_python2=ON \
    	-D BUILD_opencv_python3=ON \
    	-D BUILD_TESTS=OFF \
    	-D BUILD_PERF_TESTS=OFF \
    	-D BUILD_EXAMPLES=OFF \
    	-D CMAKE_BUILD_TYPE=RELEASE \
    	-D CMAKE_INSTALL_PREFIX=/usr/local ..

7- Compile the OpenCV with Contribs Modules

make -j4
sudo make install

It may take a while…more than one hour, but the job is done !

Best regards,

Christophe

Minor suggestions: Make -j4 failed a lot in my tests. I tried in my own script. It uses too much memory, especially towards the end when it links everything together.

-D CUDA_ON enables (partially broken) CUDA support. I believe it may have to be explicitly specified to be enabled since its a non free component. Can you post the results of a print(cv2.getBuildInformation) pls?

Lastly, There are a few build scripts you can modify or contribute to if you prefer not to write from scratch:

Any recent version / branch:

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

Hello Mdegans,

Thank you so much for your kind reply and informations.

According to your process, I recommend you to build a swap file first to avoid memory leak.
For me, there is no problem to build and compile OpenCV on the Jetson Nano.
Just a good fan to cool the CPU/GPU, and that’s it. ;-)

Warm regards,

A swap file will prevent a crash caused by running out of memory, but you may lose the speed you gain
from multiple jobs becausee it’s swapping from a Micro SD card. Swapping from a Micro SD card regularly will also greatly reduced it’s lifespan.

print(cv2.getBuildInformation)
where I write this?
for what is this and what you mean with partial broken?

sudo apt install -y build-essential cmake git libgtk2.0-dev pkg-config libswscale-dev libtbb2 libtbb-dev

When iam trying this im getting an error saying:

E: Failed to fetch http://ports.ubuntu.com/ubuntu-ports/pool/main/c/cmake/cmake-data_3.10.2-1ubuntu2.18.04.1_all.deb Hash Sum mismatch
Hashes of expected file:
- SHA256:011ab89d7d0a1f87d401abee744eb6709818bfb45b89e6631f166b1cfcd954a8
- SHA1:54fd07faf1358234b7fee04155905b419e36d363 [weak]
- MD5Sum:d1eab09950727c8426752a493adea033 [weak]
- Filesize:1331524 [weak]
Hashes of received file:
- SHA256:9a23c3747def4d63a6d934809cb91f310cdbe7f4a88b43a27b2f43f5415be3ac
- SHA1:b623064bf46264f9525c670b6bd2ed00e6ddeb1c [weak]
- MD5Sum:178f44b39edf9a972ed0896bf237269c [weak]
- Filesize:1331524 [weak]
Last modification reported: Fri, 26 Jul 2019 20:28:24 +0000
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

How to fix this???
i have tried these steps to avoid:

  1. sudo apt-get clean
    sudo apt-get update

  2. sudo rm /var/lib/apt/lists/* -vf
    sudo apt-get update

Looks like a corrupted download. How reliable is your internet connection? Please retry and post the error, if any.

I have installed those(updating packages) seperately…by using

sudo apt install -y build-essential
sudo apt install -y git
sudo apt install -y libgtk2.0-dev…etc

But im getting this error only when im trying to install cmake
sudo apt install -y cmake

Rest all steps worked very fine…

When you re-run the cmake install, are the hashes of the the received file the same across errors? I am trying to determine whether the error is random or not. This will help determine the cause. Please try to install cmake again and post the error.

I made a post that explains how to compile the latest version of opencv on jetson nano

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

Get:1 http://ports.ubuntu.com/ubuntu-ports bionic-updates/main arm64 cmake-data all 3.10.2-1ubuntu2.18.04.1 [1,332 kB]
Err:1 http://ports.ubuntu.com/ubuntu-ports bionic-updates/main arm64 cmake-data all 3.10.2-1ubuntu2.18.04.1
Hash Sum mismatch
Hashes of expected file:

  • SHA256:011ab89d7d0a1f87d401abee744eb6709818bfb45b89e6631f166b1cfcd954a8
  • SHA1:54fd07faf1358234b7fee04155905b419e36d363 [weak]
  • MD5Sum:d1eab09950727c8426752a493adea033 [weak]
  • Filesize:1331524 [weak]
    Hashes of received file:
  • SHA256:9a23c3747def4d63a6d934809cb91f310cdbe7f4a88b43a27b2f43f5415be3ac
  • SHA1:b623064bf46264f9525c670b6bd2ed00e6ddeb1c [weak]
  • MD5Sum:178f44b39edf9a972ed0896bf237269c [weak]
  • Filesize:1331524 [weak]
    Last modification reported: Fri, 26 Jul 2019 20:28:24 +0000
    Fetched 1,332 kB in 13s (99.8 kB/s)
    E: Failed to fetch http://ports.ubuntu.com/ubuntu-ports/pool/main/c/cmake/cmake-data_3.10.2-1ubuntu2.18.04.1_all.deb Hash Sum mismatch
    Hashes of expected file:
    - SHA256:011ab89d7d0a1f87d401abee744eb6709818bfb45b89e6631f166b1cfcd954a8
    - SHA1:54fd07faf1358234b7fee04155905b419e36d363 [weak]
    - MD5Sum:d1eab09950727c8426752a493adea033 [weak]
    - Filesize:1331524 [weak]
    Hashes of received file:
    - SHA256:9a23c3747def4d63a6d934809cb91f310cdbe7f4a88b43a27b2f43f5415be3ac
    - SHA1:b623064bf46264f9525c670b6bd2ed00e6ddeb1c [weak]
    - MD5Sum:178f44b39edf9a972ed0896bf237269c [weak]
    - Filesize:1331524 [weak]
    Last modification reported: Fri, 26 Jul 2019 20:28:24 +0000
    E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

This is the error I got when I tried to install it again…
which is same as the previous error…

That’s strange. Can you post the contents of your /etc/apt/sources.list? I will try to install the package in question later and see if I get the same error.

So I installed cmake on my xavier and on a nano chroot and it works fine. I will try it on an actual nano on monday. I would check /etc/apt/sources.list, your network configuration, and if everyting seems fine after that, backup what you need and reflash. If it keeps happening, you might consider trying from another network.

from the nano chroot:

root@hostname:/# apt install -y cmake
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  cmake-data libcurl4 libjsoncpp1 librhash0 libuv1
Suggested packages:
  cmake-doc ninja-build
The following NEW packages will be installed:
  cmake cmake-data libcurl4 libjsoncpp1 librhash0 libuv1
0 upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,678 kB of archives.
After this operation, 24.3 MB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports bionic-updates/main arm64 cmake-data all 3.10.2-1ubuntu2.18.04.1 [1,332 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports bionic-updates/main arm64 libcurl4 arm64 7.58.0-2ubuntu3.8 [180 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports bionic/main arm64 libjsoncpp1 arm64 1.7.4-3 [69.2 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports bionic/main arm64 librhash0 arm64 1.3.6-2 [71.8 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports bionic/main arm64 libuv1 arm64 1.18.0-3 [53.7 kB]
Get:6 http://ports.ubuntu.com/ubuntu-ports bionic-updates/main arm64 cmake arm64 3.10.2-1ubuntu2.18.04.1 [2,971 kB]
Fetched 4,678 kB in 2s (2,988 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
E: Can not write log (Is /dev/pts mounted?) - posix_openpt (19: No such device)
Selecting previously unselected package cmake-data.
(Reading database ... 119123 files and directories currently installed.)
Preparing to unpack .../0-cmake-data_3.10.2-1ubuntu2.18.04.1_all.deb ...
Unpacking cmake-data (3.10.2-1ubuntu2.18.04.1) ...
Selecting previously unselected package libcurl4:arm64.
Preparing to unpack .../1-libcurl4_7.58.0-2ubuntu3.8_arm64.deb ...
Unpacking libcurl4:arm64 (7.58.0-2ubuntu3.8) ...
Selecting previously unselected package libjsoncpp1:arm64.
Preparing to unpack .../2-libjsoncpp1_1.7.4-3_arm64.deb ...
Unpacking libjsoncpp1:arm64 (1.7.4-3) ...
Selecting previously unselected package librhash0:arm64.
Preparing to unpack .../3-librhash0_1.3.6-2_arm64.deb ...
Unpacking librhash0:arm64 (1.3.6-2) ...
Selecting previously unselected package libuv1:arm64.
Preparing to unpack .../4-libuv1_1.18.0-3_arm64.deb ...
Unpacking libuv1:arm64 (1.18.0-3) ...
Selecting previously unselected package cmake.
Preparing to unpack .../5-cmake_3.10.2-1ubuntu2.18.04.1_arm64.deb ...
Unpacking cmake (3.10.2-1ubuntu2.18.04.1) ...
Setting up libuv1:arm64 (1.18.0-3) ...
Setting up libcurl4:arm64 (7.58.0-2ubuntu3.8) ...
Setting up cmake-data (3.10.2-1ubuntu2.18.04.1) ...
Setting up librhash0:arm64 (1.3.6-2) ...
Setting up libjsoncpp1:arm64 (1.7.4-3) ...
Setting up cmake (3.10.2-1ubuntu2.18.04.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...

Thanks it is working now…I just changed the network configuration and it is working fine

Glad that fixed it. Some ISPs set up caching, and sometimes the cache can be out of date. You may wish to set up your gateway and/or nanos themselves to use google/cloudflare dns instead of that of your ISP. That may help prevent this from happening again and is often just a good idea.

Thanks for this information…

Another issue is at “make -j4”

After this step it took nearly an hour time but it is not done completely…at 98% it is popping up a notification saying:

“Low memory warning:
Memory available for new process : *** MB
free *** MB , buffer/cache *** MB”

and also system became slow…cursor and keyboard were too slow while this happend
I even have the extra swap memory of 4GB along with Memory 4GB

System was fine when i restarted…but even after trying this step for 3-4 times…same issue is happening again

I tried to clean it using “sudo apt-get clean” didnt work out…

I tried these commands to clear cache:

sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches' #for page cache sudo sh -c ‘echo 2 >/proc/sys/vm/drop_caches’ #for dentries and inodes
$ sudo sh -c ‘echo 3 >/proc/sys/vm/drop_caches’ #for both page cache, dentries and inodes

(these are the commands used if we are trying to use them in sudo)

Even then it is showing the same popup warning…

Can u help me fix this…

You have 3 options:

  1. Add swap (recommended and easy to do)

  2. Stop some services to get more memory

  3. Use a minimalist image https://pythops.com/post/create-your-own-image-for-jetson-nano-board

Running out of memory at the end of the build is a known issue. Adding more swap temporarily will help, or…

My usual fix is to turn off the GUI temporarily (systemctl isolate multi-user.target). You must have an HDMI monitor connected, last I checked, or ssh set up. Display Port will result in a black screen unless this has been fixed since I last tried.

Then re-run the build. It should be faster with more free memory anyway.

Hi, thanks very much for the instructions. However I ran into an issue where the OpenCV4.1 only affected Python2.7 and Python3.6 remained unaffected (still has opencv3). Can someone help me out on this? Thank you very much!