├── .gitignore ├── LICENSE ├── README.md └── build_opencv.sh /.gitignore: -------------------------------------------------------------------------------- 1 | configure.log 2 | build.log 3 | test.log 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2019 Michael de Gans 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenCV build script for Tegra 2 | 3 | This script builds OpenCV from source on Tegra (Nano, NX, AGX, etc.). 4 | 5 | Related thread on Nvidia developer forum 6 | [here](https://devtalk.nvidia.com/default/topic/1051133/jetson-nano/opencv-build-script/). 7 | 8 | [How it Works](https://wiki.debian.org/QemuUserEmulation) 9 | 10 | ## Usage: 11 | ```shell 12 | ./build_opencv.sh 13 | ``` 14 | 15 | ## Specifying an OpenCV version (git branch) 16 | ```shell 17 | ./build_opencv.sh 4.4.0 18 | ``` 19 | 20 | Where `4.4.0` is any version of openCV from 2.2 to 4.4.0 21 | (any valid OpenCV git branch or tag will also attempt to work, however the very old versions have not been tested to build and may require spript modifications.). 22 | 23 | **JetPack 4.4 NOTE:** the minimum version that will build correctly on JetPack 4.4 GA is 4.4.0. Prior versions of JetPack may need the CUDNN version adjusted (the `-D CUDNN_VERSION='8.0'` line can simply be removed). 24 | -------------------------------------------------------------------------------- /build_opencv.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 2019 Michael de Gans 3 | 4 | set -e 5 | 6 | # change default constants here: 7 | readonly PREFIX=/usr/local # install prefix, (can be ~/.local for a user install) 8 | readonly DEFAULT_VERSION=4.4.0 # controls the default version (gets reset by the first argument) 9 | readonly CPUS=$(nproc) # controls the number of jobs 10 | 11 | # better board detection. if it has 6 or more cpus, it probably has a ton of ram too 12 | if [[ $CPUS -gt 5 ]]; then 13 | # something with a ton of ram 14 | JOBS=$CPUS 15 | else 16 | JOBS=1 # you can set this to 4 if you have a swap file 17 | # otherwise a Nano will choke towards the end of the build 18 | fi 19 | 20 | cleanup () { 21 | # https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script 22 | while true ; do 23 | echo "Do you wish to remove temporary build files in /tmp/build_opencv ? " 24 | if ! [[ "$1" -eq "--test-warning" ]] ; then 25 | echo "(Doing so may make running tests on the build later impossible)" 26 | fi 27 | read -p "Y/N " yn 28 | case ${yn} in 29 | [Yy]* ) rm -rf /tmp/build_opencv ; break;; 30 | [Nn]* ) exit ;; 31 | * ) echo "Please answer yes or no." ;; 32 | esac 33 | done 34 | } 35 | 36 | setup () { 37 | cd /tmp 38 | if [[ -d "build_opencv" ]] ; then 39 | echo "It appears an existing build exists in /tmp/build_opencv" 40 | cleanup 41 | fi 42 | mkdir build_opencv 43 | cd build_opencv 44 | } 45 | 46 | git_source () { 47 | echo "Getting version '$1' of OpenCV" 48 | git clone --depth 1 --branch "$1" https://github.com/opencv/opencv.git 49 | git clone --depth 1 --branch "$1" https://github.com/opencv/opencv_contrib.git 50 | } 51 | 52 | install_dependencies () { 53 | # open-cv has a lot of dependencies, but most can be found in the default 54 | # package repository or should already be installed (eg. CUDA). 55 | echo "Installing build dependencies." 56 | sudo apt-get update 57 | sudo apt-get dist-upgrade -y --autoremove 58 | sudo apt-get install -y \ 59 | build-essential \ 60 | cmake \ 61 | git \ 62 | gfortran \ 63 | libatlas-base-dev \ 64 | libavcodec-dev \ 65 | libavformat-dev \ 66 | libavresample-dev \ 67 | libcanberra-gtk3-module \ 68 | libdc1394-22-dev \ 69 | libeigen3-dev \ 70 | libglew-dev \ 71 | libgstreamer-plugins-base1.0-dev \ 72 | libgstreamer-plugins-good1.0-dev \ 73 | libgstreamer1.0-dev \ 74 | libgtk-3-dev \ 75 | libjpeg-dev \ 76 | libjpeg8-dev \ 77 | libjpeg-turbo8-dev \ 78 | liblapack-dev \ 79 | liblapacke-dev \ 80 | libopenblas-dev \ 81 | libpng-dev \ 82 | libpostproc-dev \ 83 | libswscale-dev \ 84 | libtbb-dev \ 85 | libtbb2 \ 86 | libtesseract-dev \ 87 | libtiff-dev \ 88 | libv4l-dev \ 89 | libxine2-dev \ 90 | libxvidcore-dev \ 91 | libx264-dev \ 92 | pkg-config \ 93 | python-dev \ 94 | python-numpy \ 95 | python3-dev \ 96 | python3-numpy \ 97 | python3-matplotlib \ 98 | qv4l2 \ 99 | v4l-utils \ 100 | zlib1g-dev 101 | } 102 | 103 | configure () { 104 | local CMAKEFLAGS=" 105 | -D BUILD_EXAMPLES=OFF 106 | -D BUILD_opencv_python2=ON 107 | -D BUILD_opencv_python3=ON 108 | -D CMAKE_BUILD_TYPE=RELEASE 109 | -D CMAKE_INSTALL_PREFIX=${PREFIX} 110 | -D CUDA_ARCH_BIN=5.3,6.2,7.2,8.7 111 | -D CUDA_ARCH_PTX= 112 | -D CUDA_FAST_MATH=ON 113 | -D CUDNN_VERSION='8.0' 114 | -D EIGEN_INCLUDE_PATH=/usr/include/eigen3 115 | -D ENABLE_NEON=ON 116 | -D OPENCV_DNN_CUDA=ON 117 | -D OPENCV_ENABLE_NONFREE=ON 118 | -D OPENCV_EXTRA_MODULES_PATH=/tmp/build_opencv/opencv_contrib/modules 119 | -D OPENCV_GENERATE_PKGCONFIG=ON 120 | -D WITH_CUBLAS=ON 121 | -D WITH_CUDA=ON 122 | -D WITH_CUDNN=ON 123 | -D WITH_GSTREAMER=ON 124 | -D WITH_LIBV4L=ON 125 | -D WITH_OPENGL=ON" 126 | 127 | if [[ "$1" != "test" ]] ; then 128 | CMAKEFLAGS=" 129 | ${CMAKEFLAGS} 130 | -D BUILD_PERF_TESTS=OFF 131 | -D BUILD_TESTS=OFF" 132 | fi 133 | 134 | echo "cmake flags: ${CMAKEFLAGS}" 135 | 136 | cd opencv 137 | mkdir build 138 | cd build 139 | cmake ${CMAKEFLAGS} .. 2>&1 | tee -a configure.log 140 | } 141 | 142 | main () { 143 | 144 | local VER=${DEFAULT_VERSION} 145 | 146 | # parse arguments 147 | if [[ "$#" -gt 0 ]] ; then 148 | VER="$1" # override the version 149 | fi 150 | 151 | if [[ "$#" -gt 1 ]] && [[ "$2" == "test" ]] ; then 152 | DO_TEST=1 153 | fi 154 | 155 | # prepare for the build: 156 | setup 157 | install_dependencies 158 | git_source ${VER} 159 | 160 | if [[ ${DO_TEST} ]] ; then 161 | configure test 162 | else 163 | configure 164 | fi 165 | 166 | # start the build 167 | make -j${JOBS} 2>&1 | tee -a build.log 168 | 169 | if [[ ${DO_TEST} ]] ; then 170 | make test 2>&1 | tee -a test.log 171 | fi 172 | 173 | # avoid a sudo make install (and root owned files in ~) if $PREFIX is writable 174 | if [[ -w ${PREFIX} ]] ; then 175 | make install 2>&1 | tee -a install.log 176 | else 177 | sudo make install 2>&1 | tee -a install.log 178 | fi 179 | 180 | cleanup --test-warning 181 | 182 | } 183 | 184 | main "$@" 185 | --------------------------------------------------------------------------------