0% found this document useful (0 votes)
341 views

The openCV Installed With The Jetpack Does Not Have CUDA Supported PDF

Uploaded by

seul alone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
341 views

The openCV Installed With The Jetpack Does Not Have CUDA Supported PDF

Uploaded by

seul alone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

The openCV installed with the Jetpack does not have CUDA supported.

To use CUDA,
we have to download openCV and build from source
see https://devtalk.nvidia.com/default/topic/1058768/jetson-nano/default-opencv-
included-in-jetpack-has-no-header-files-related-to-cuda/

Install libraries
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev liblapack-dev libblas-dev
gfortran

sudo apt-get install libcanberra-gtk-module

You have to install Glu to run a CUDA GPU examples


sudo apt-get install libglew-dev

Step0 delete previous installation of opencv


sudo apt-get purge libopencv*
Step 1 increase swap size
By default the Ubuntu 18.04 distribution of Jetson Nano comes with 2 gb of Swap
memory.

To increase it we need to open the terminal and type the line:


sudo apt-get install zram-config
The zram module, on the Jetson nano allocates by default 2gb of Swap memory, so now
we’re going to extend the size to 4gb by changing the configuration file.

Just type on the terminal:


sudo gedit /usr/bin/init-zram-swapping
Replace the line:
mem=$(((totalmem / 2 / ${NRDEVICES}) * 1024))

with this line:


mem=$(((totalmem / ${NRDEVICES}) * 1024))

And then reboot.

Step 2 download and execute the Sh file that contains all


instructions to install OpenCV
1- execute this command in the terminal to clone the git repository that contains
instructions to install opencv on nano

git clone
https://github.com/JetsonHacksNano/buildOpenCV
2- enter to buildOpenCV folder from the terminal

cd Downloads/buildOpenCV-master
3- enter to the buildOpenCV folder from the explorer and open the Sh file or use
the commade sudo gedit buildOpenCV.sh

then change NUM_JOBS=$(nproc) to NUM_JOBS=1

this avoids the nano to hang when building the opencv from the sd card

the sh file contains this instructions, you can install the latest version by changing the
fifth line in this file to the desired version OPENCV_VERSION=4.x.x

the source of the sh file can be found here

https://www.jetsonhacks.com/2019/11/22/opencv-4-cuda-on-jetson-nano/

also change the Cmake section see below in yelow

4- in the terminal execute the Sh file and send the results of the build to the file
openCV_build.log using this command
./buildOpenCV.sh |& tee openCV_build.log

the build will take between 2 hours to 4 hours depends on internet speed and build
time

sudo ldconfig -v

5- You have to install Glu to run a CUDA GPU examples

sudo apt-get install libglew-dev

check opecnv build file in terminal type


python
import cv2
print(cv2.getBuildInformation())

#!/bin/bash
# License: MIT. See license file in root directory
# Copyright(c) JetsonHacks (2017-2019)

OPENCV_VERSION=4.3.0
# Jetson Nano
ARCH_BIN=5.3
INSTALL_DIR=/usr/local
# Download the opencv_extras repository
# If you are installing the opencv testdata, ie
# OPENCV_TEST_DATA_PATH=../opencv_extra/testdata
# Make sure that you set this to YES
# Value should be YES or NO
DOWNLOAD_OPENCV_EXTRAS=NO
# Source code directory
OPENCV_SOURCE_DIR=$HOME
WHEREAMI=$PWD
# NUM_JOBS is the number of jobs to run simultaneously when using make
# This will default to the number of CPU cores (on the Nano, that's 4)
# If you are using a SD card, you may want to change this
# to 1. Also, you may want to increase the size of your swap file
NUM_JOBS=$(nproc)

CLEANUP=true
PACKAGE_OPENCV="-D CPACK_BINARY_DEB=ON"

function usage
{
echo "usage: ./buildOpenCV.sh [[-s sourcedir ] | [-h]]"
echo "-s | --sourcedir Directory in which to place the opencv sources (default $HOME)"
echo "-i | --installdir Directory in which to install opencv libraries (default /usr/local)"
echo "--no_package Do not package OpenCV as .deb file (default is true)"
echo "-h | --help This message"
}

# Iterate through command line inputs


while [ "$1" != "" ]; do
case $1 in
-s | --sourcedir ) shift
OPENCV_SOURCE_DIR=$1
;;
-i | --installdir ) shift
INSTALL_DIR=$1
;;
--no_package ) PACKAGE_OPENCV=""
;;
-h | --help ) usage
exit
;;
*) usage
exit 1
esac
shift
done

CMAKE_INSTALL_PREFIX=$INSTALL_DIR

# Print out the current configuration


echo "Build configuration: "
echo " NVIDIA Jetson Nano"
echo " OpenCV binaries will be installed in: $CMAKE_INSTALL_PREFIX"
echo " OpenCV Source will be installed in: $OPENCV_SOURCE_DIR"
if [ "$PACKAGE_OPENCV" = "" ] ; then
echo " NOT Packaging OpenCV"
else
echo " Packaging OpenCV"
fi

if [ $DOWNLOAD_OPENCV_EXTRAS == "YES" ] ; then


echo "Also downloading opencv_extras"
fi
# Repository setup
sudo apt-add-repository universe
sudo apt-get update

# Download dependencies for the desired configuration


cd $WHEREAMI
sudo apt-get install -y \
build-essential \
cmake \
libavcodec-dev \
libavformat-dev \
libavutil-dev \
libeigen3-dev \
libglew-dev \
libgtk2.0-dev \
libgtk-3-dev \
libjpeg-dev \
libpng-dev \
libpostproc-dev \
libswscale-dev \
libtbb-dev \
libtiff5-dev \
libv4l-dev \
libxvidcore-dev \
libx264-dev \
qt5-default \
zlib1g-dev \
pkg-config

# We will be supporting OpenGL, we need a little magic to help


# https://devtalk.nvidia.com/default/topic/1007290/jetson-tx2/building-opencv-with-opengl-support-
/post/5141945/#5141945
cd /usr/local/cuda/include
sudo patch -N cuda_gl_interop.h $WHEREAMI'/patches/OpenGLHeader.patch'

# Python 2.7
sudo apt-get install -y python-dev python-numpy python-py python-pytest
# Python 3.6
sudo apt-get install -y python3-dev python3-numpy python3-py python3-pytest

# GStreamer support
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

cd $OPENCV_SOURCE_DIR
git clone --branch "$OPENCV_VERSION" https://github.com/opencv/opencv.git
git clone --branch "$OPENCV_VERSION" https://github.com/opencv/opencv_contrib.git
if [ $DOWNLOAD_OPENCV_EXTRAS == "YES" ] ; then
echo "Installing opencv_extras"
# This is for the test data
cd $OPENCV_SOURCE_DIR
git clone https://github.com/opencv/opencv_extra.git
cd opencv_extra
git checkout -b v${OPENCV_VERSION} ${OPENCV_VERSION}
fi

# Patch the Eigen library issue ...


cd $OPENCV_SOURCE_DIR/opencv
sed -i 's/include <Eigen\/Core>/include <eigen3\/Eigen\/Core>/g'
modules/core/include/opencv2/core/private.hpp

# Create the build directory and start cmake


cd $OPENCV_SOURCE_DIR/opencv
mkdir build
cd build

# Here are some options to install source examples and tests


# -D INSTALL_TESTS=ON \
# -D OPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
# -D INSTALL_C_EXAMPLES=ON \
# -D INSTALL_PYTHON_EXAMPLES=ON \

# If you are compiling the opencv_contrib modules:


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

# There are also switches which tell CMAKE to build the samples and tests
# Check OpenCV documentation for details
# -D WITH_QT=ON \

echo $PWD
time cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} \
-D WITH_CUDA=ON \
-D CUDA_ARCH_BIN=${ARCH_BIN} \
-D CUDA_ARCH_PTX="" \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_LIBV4L=ON \
-D WITH_V4L=ON \
-D WITH_GSTREAMER=ON \
-D WITH_GSTREAMER_0_10=OFF \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D BUILD_opencv_python2=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=ON \
-D INSTALL_TESTS=ON \
-D OPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
$"PACKAGE_OPENCV" \
../

if [ $? -eq 0 ] ; then
echo "CMake configuration make successful"
else
# Try to make again
echo "CMake issues " >&2
echo "Please check the configuration being used"
exit 1
fi

# Consider the MAXN performance mode if using a barrel jack on the Nano
time make -j$NUM_JOBS
if [ $? -eq 0 ] ; then
echo "OpenCV make successful"
else
# Try to make again; Sometimes there are issues with the build
# because of lack of resources or concurrency issues
echo "Make did not build " >&2
echo "Retrying ... "
# Single thread this time
make
if [ $? -eq 0 ] ; then
echo "OpenCV make successful"
else
# Try to make again
echo "Make did not successfully build" >&2
echo "Please fix issues and retry build"
exit 1
fi
fi

echo "Installing ... "


sudo make install
sudo ldconfig
if [ $? -eq 0 ] ; then
echo "OpenCV installed in: $CMAKE_INSTALL_PREFIX"
else
echo "There was an issue with the final installation"
exit 1
fi

# If PACKAGE_OPENCV is on, pack 'er up and get ready to go!


# We should still be in the build directory ...
if [ "$PACKAGE_OPENCV" != "" ] ; then
echo "Starting Packaging"
sudo ldconfig
time sudo make package -j$NUM_JOBS
if [ $? -eq 0 ] ; then
echo "OpenCV make package successful"
else
# Try to make again; Sometimes there are issues with the build
# because of lack of resources or concurrency issues
echo "Make package did not build " >&2
echo "Retrying ... "
# Single thread this time
sudo make package
if [ $? -eq 0 ] ; then
echo "OpenCV make package successful"
else
# Try to make again
echo "Make package did not successfully build" >&2
echo "Please fix issues and retry build"
exit 1
fi
fi
fi

# check installation
IMPORT_CHECK="$(python -c "import cv2 ; print cv2.__version__")"
if [[ $IMPORT_CHECK != *$OPENCV_VERSION* ]]; then
echo "There was an error loading OpenCV in the Python sanity test."
echo "The loaded version does not match the version built here."
echo "Please check the installation."
echo "The first check should be the PYTHONPATH environment variable."
fi

To install IDE (from AI on the jetson nano lesson 8)

in terminal do:

1/ install Curl (it will help us to install visual studio code code-oss)
sudo apt-get install curl

2/ download code-oss

curl -L https://github.com/toolboc/vscode/releases/download/1.32.3/code-oss_1.32.3-arm64.deb -o
code-oss_1.32.3-arm64.deb

3/ install code-oss

sudo dpkg -i code-oss_1.32.3-arm64.deb

serach code-oss in search in your computer and lunch it

select extensions and type python and install

after that type Ctrl+shift+p

write ‘select interpreter’ click on It and select python3


to add auto-completion (intellisense) option

do the following

CTRL+SHIFT+P type Preferences:Open Settings(JSON) Add these settings inside the { } :

"python.linting.pylintArgs": ["--generate-members", "--extension-pkg-whitelist=cv2"],

"python.autoComplete.extraPaths": ["/usr/lib/python3.6/dist-packages/cv2/python-3.6"]

The autoComplete path should be the folder where the cv2 .so file is. I got that location by typing dpkg
-L libopencv-python, but your package name might be different since it was compiled from source.

git clone https://github.vom/JetsonHacksNano/installSwapfile

Install libraries (from AI on the jetson nano lesson 9)


To install matplotlib library
sudo apt-get install python3-matplotlib
install librariers
sudo apt-get install libcanberra-gtk-module

sudo apt-get install libhdf5-serial-dev hdf5-tools libhdf5-dev zlib1g-dev zip libjpeg8-dev liblapack-dev libblas-dev
gfortran

sudo apt-get install python3-pip


sudo pip3 install -U pip testresources setuptools

sudo pip3 install -U numpy==1.16.1 future==0.17.1 mock==3.0.5 h5py==2.9.0 keras_preprocessing==1.0.5

keras_applications==1.0.8 gast==0.2.2 futures protobuf pybind11


Don’t forget to restore the swap file to default

You might also like