I’ve created a github repo for an OpenCV build script. The script installs build dependencies, clones a requested version of OpenCV, builds it from source, and installs it.
On a Nano itself it will likely take quite a while to run, and a large swapfile is recommended or you are likely to run out of memory during the build.
Please not that this script is currently untested and expected to break. Reporting issues here or on github is very much appreciated.
mdegans - Thanks very much for providing this - it’s really helpful. I ran into a minor issue at the end where I got the following:
“Do you wish to remove temporary build files in /tmp/build_opencv ?
(Doing so may make running tests on the build later impossible)
Y/N y
./build_opencv.sh: error reading input file: Stale file handle”
I looked in the tmp folder and there was no build_opencv folder there.
Also - it would be good to document the ‘test’ option in the README.
Hmm. Based on your paste it looks like you selected ‘y’ to delete the files, in which case it’s normal that there be nothing in /tmp/build_opencv
The stale file handle error seems to be because the script deletes /tmp/build_opencv while still inside /tmp/build_opencv. My cleanup function should cd out first. Thanks for finding that.
Documenting the test option is a good idea. I included it for the sorts of people who read the script, as normally they can take a very very long time to run.
I will update the script soon, fixing the bug you found and documenting the test option as suggested. I will probably add a warning that tests can take a very long time to run.
Thanks !!. Use Python 3.6.8
I run an application with OpenCV and the following error comes up:
ERROR: A version that meets the opencv_contrib_python requirement (versions: none) could not be found
ERROR: A matching distribution for opencv_contrib_python was not found.
Hi
I am going to be using C++ for my jetson project and as such don’t believe I will need python. Can I just remove the lines where you apt install python and then set the cmake flags to off or will this break the script?
Thanks
Sorry to bother again. I did as I mentioned above where I commented out the lines that install python and I set the python cmake flags to off. Opencv built however when I checked the version it had installed 4.1.1 and with no Cuda. I check the script to ensure that the Cuda flag was set to on and it was. Any idea what could have caused this? Also is there a log file I can find. I didn’t remove the temporary build files.
Thanks
Can you post a copy of the script? Regarding the log file, I don’t think there is one, but that’s a good suggestion and I just added it to the dev branch of the script.
From the nano_build_opencv folder you cloned, can you run
git checkout dev
git pull
and rerun the build? There should then be four logs written to /tmp/build_opencv/opencv/build as each of the build steps progress: configure.log, build.log, test.log, and install.log.
However, the most likely cause of failure while building OpenCV is that your device runs out of memory. I recommend that you either turn off x11 temporarily with sudo systemctl isolate multi-user.target, or add a larger swap file – preferably both for the fastest build – and preferably the swap on an external disk, both for speed and to reduce wear on your sd card.
To install a swap file, fallocate a file an an external disks and roughly follow these commands here. It’s probably not desirable to add it to fstab, so you can skip that part and delete the file after you’re done and turn swap back off.
Hi. Thanks for the advice. See below the script that I ran.
#!/usr/bin/env bash
# 2019 Michael de Gans
set -e
# change default constants here:
readonly PREFIX=/usr/local # install prefix, (can be ~/.local for a user install)
readonly DEFAULT_VERSION=4.2.0 # controls the default version (gets reset by the first argument)
readonly CPUS=$(nproc) # controls the number of jobs
# better board detection. if it has 6 or more cpus, it probably has a ton of ram too
if [[ $CPUS -gt 5 ]]; then
# something with a ton of ram
JOBS=$CPUS
else
JOBS=4 # you can set this to 4 if you have a swap file
# otherwise a Nano will choke towards the end of the build
fi
cleanup () {
# https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script
while true ; do
echo "Do you wish to remove temporary build files in /tmp/build_opencv ? "
if ! [[ "$1" -eq "--test-warning" ]] ; then
echo "(Doing so may make running tests on the build later impossible)"
fi
read -p "Y/N " yn
case ${yn} in
[Yy]* ) rm -rf /tmp/build_opencv ; break;;
[Nn]* ) exit ;;
* ) echo "Please answer yes or no." ;;
esac
done
}
setup () {
cd /tmp
if [[ -d "build_opencv" ]] ; then
echo "It appears an existing build exists in /tmp/build_opencv"
cleanup
fi
mkdir build_opencv
cd build_opencv
}
git_source () {
echo "Getting version '$1' of OpenCV"
git clone --branch "$1" https://github.com/opencv/opencv.git
git clone --branch "$1" https://github.com/opencv/opencv_contrib.git
}
install_dependencies () {
# open-cv has a lot of dependencies, but most can be found in the default
# package repository or should already be installed (eg. CUDA).
echo "Installing build dependencies."
sudo apt-get update
sudo apt-get dist-upgrade -y --autoremove
sudo apt-get install -y \
build-essential \
cmake \
git \
gfortran \
libatlas-base-dev \
libavcodec-dev \
libavformat-dev \
libavresample-dev \
libcanberra-gtk3-module \
libdc1394-22-dev \
libeigen3-dev \
libglew-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-good1.0-dev \
libgstreamer1.0-dev \
libgtk-3-dev \
libjpeg-dev \
libjpeg8-dev \
libjpeg-turbo8-dev \
liblapack-dev \
liblapacke-dev \
libopenblas-dev \
libpng-dev \
libpostproc-dev \
libswscale-dev \
libtbb-dev \
libtbb2 \
libtesseract-dev \
libtiff-dev \
libv4l-dev \
libxine2-dev \
libxvidcore-dev \
libx264-dev \
pkg-config \
qv4l2 \
v4l-utils \
v4l2ucp \
zlib1g-dev
#python-dev \
#python-numpy \
#python3-dev \
#python3-numpy \
#python3-matplotlib \
}
configure () {
local CMAKEFLAGS="
-D BUILD_EXAMPLES=OFF
-D BUILD_opencv_python2=OFF
-D BUILD_opencv_python3=OFF
-D CMAKE_INSTALL_PREFIX=${PREFIX}
-D CMAKE_BUILD_TYPE=RELEASE
-D CUDA_ARCH_BIN=5.3,6.2,7.2
-D CUDA_ARCH_PTX=
-D CUDA_FAST_MATH=ON
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3
-D ENABLE_NEON=ON
-D OPENCV_ENABLE_NONFREE=ON
-D OPENCV_EXTRA_MODULES_PATH=/tmp/build_opencv/opencv_contrib/modules
-D OPENCV_GENERATE_PKGCONFIG=ON
-D WITH_CUDA=ON
-D WITH_CUBLAS=ON
-D WITH_GSTREAMER=ON
-D WITH_LIBV4L=ON
-D WITH_OPENGL=ON"
if ! [[ "$1" -eq "test" ]] ; then
CMAKEFLAGS="
${CMAKEFLAGS}
-D BUILD_PERF_TESTS=OFF
-D BUILD_TESTS=OFF"
fi
echo "cmake flags: ${CMAKEFLAGS}"
cd opencv
mkdir build
cd build
cmake ${CMAKEFLAGS} ..
}
main () {
local VER=${DEFAULT_VERSION}
# parse arguments
if [[ "$#" -gt 0 ]] ; then
VER="$1" # override the version
fi
if [[ "$#" -gt 1 ]] && [[ "$2" -eq "test" ]] ; then
DO_TEST=1
fi
# prepare for the build:
setup
install_dependencies
git_source ${VER}
if [[ ${DO_TEST} ]] ; then
configure test
else
configure
fi
# start the build
make -j${JOBS}
if [[ ${DO_TEST} ]] ; then
make test # (make and) run the tests
fi
# avoid a sudo make install (and root owned files in ~) if $PREFIX is writable
if [[ -w ${PREFIX} ]] ; then
make install
else
sudo make install
fi
cleanup --test-warning
}
main "$@"
Some more info about my build if it helps: I have jetpack installed onto a 32GB flash. I had increased the swap size by following this article. I had increased it to 8GB for this build. I also didn’t specify a build version when calling the build script as I noticed the default is set to 4.2.0.
I was also kind of keeping an eye on the build and didn’t notice it using more than 3.5 of the 4 GB of memory. The swap also had only a few MB of data in it at the end of the build.
I’ll rerun the build now and let you know the results.
Unfortunately, I cant’ be sure if there are errors in that script because of the formatting in the quote tags, and it looks like it may have stripped off some characters (eg. \). Just FYI, the forum now supports markdown so you can paste code between three backticks on either side:
```
code here
```
or you can use bbcode code tags:
[code]
code here
[/code]
I did realize I forgot to mention something. If Python is installed, I believe you will have to change the cmake flags to “off” since I believe the default is to build the python extensions. I would create a no-python branch on github but I can’t even pull at the moment since they seem to be having issues.
edit. github is back. you may checkout the no-python branch by: