aboutsummaryrefslogtreecommitdiff
path: root/utils/buildAndPackageOpenCV.sh
diff options
context:
space:
mode:
authorThéo de la Hogue2023-03-13 16:10:28 +0100
committerThéo de la Hogue2023-03-13 16:10:28 +0100
commitc37fb875e47265459225021b7ab9c6a6db8d1595 (patch)
tree6d9c6352cea0a989125bd4adff66be58ce9d1b20 /utils/buildAndPackageOpenCV.sh
parent67405a9de1f50a1ae2d05485af7ca9a365029035 (diff)
downloadargaze-c37fb875e47265459225021b7ab9c6a6db8d1595.zip
argaze-c37fb875e47265459225021b7ab9c6a6db8d1595.tar.gz
argaze-c37fb875e47265459225021b7ab9c6a6db8d1595.tar.bz2
argaze-c37fb875e47265459225021b7ab9c6a6db8d1595.tar.xz
Adding a shell script to build a cuda accelerated version of opencv.
Diffstat (limited to 'utils/buildAndPackageOpenCV.sh')
-rwxr-xr-xutils/buildAndPackageOpenCV.sh303
1 files changed, 303 insertions, 0 deletions
diff --git a/utils/buildAndPackageOpenCV.sh b/utils/buildAndPackageOpenCV.sh
new file mode 100755
index 0000000..87ecadb
--- /dev/null
+++ b/utils/buildAndPackageOpenCV.sh
@@ -0,0 +1,303 @@
+#!/bin/bash
+
+# Consider $ sudo nvpmodel -m 2 or $ sudo nvpmodel -m 0
+
+OPENCV_VERSION=4.7.0
+
+INSTALL_DIR=/usr/local
+
+OPENCV_SOURCE_DIR=$HOME/Documents
+
+PYTHON_DIR=~/.pyenv/versions/3.11-dev
+
+DEPENDENCIES=YES
+
+CLEANUP=NO
+
+usage ()
+{
+ echo "usage: ./buildAndPackageOpenCV.sh [[-s sourcedir ] | [-h]]"
+ echo "-s | --sourcedir Directory in which to place the opencv sources (default $HOME/Documents)"
+ echo "-i | --installdir Directory in which to install opencv libraries (default /usr/local)"
+ echo "-d | --dependencies Install required dependencies (default YES)"
+ echo "-c | --cleanup build folder (default NO)"
+ echo "-h | --help This message"
+}
+
+checkout ()
+{
+ cd $1
+
+ branch=$(git symbolic-ref --short HEAD)
+ if [ "$branch" != "v${OPENCV_VERSION}" ]; then
+ git checkout -b v${OPENCV_VERSION} ${OPENCV_VERSION}
+ else
+ echo
+ echo "$1 git repository binds to v${OPENCV_VERSION} branch"
+ fi
+}
+
+# Iterate through command line inputs
+while [ "$1" != "" ]; do
+ case $1 in
+ -s | --sourcedir ) shift
+ OPENCV_SOURCE_DIR=$1
+ ;;
+ -i | --installdir ) shift
+ INSTALL_DIR=$1
+ ;;
+ -d | --dependencies ) shift
+ DEPENDENCIES=$1
+ ;;
+ -c | --cleanup ) shift
+ CLEANUP=$1
+ ;;
+ -h | --help ) usage
+ exit
+ ;;
+ * ) usage
+ exit 1
+ esac
+ shift
+done
+
+CMAKE_INSTALL_PREFIX=$INSTALL_DIR
+WHEREAMI=$PWD
+NUM_CPU=$(nproc)
+
+if [ "$UID" = "0" ]; then
+ echo "Error: Can't execute this script as root user."
+ exit 1
+fi
+
+# Print out the current configuration
+echo
+echo "** Build configuration: "
+echo " Jetson Module: $JETSON_MODULE"
+echo " Operating System: $JETSON_L4T [Jetpack $JETSON_JETPACK]"
+echo " Jetson Cuda Architecture Binaries: $JETSON_CUDA_ARCH_BIN"
+echo
+echo " OpenCV sources will be installed in: $OPENCV_SOURCE_DIR"
+echo " OpenCV binaries will be installed in: $CMAKE_INSTALL_PREFIX"
+
+if [ "$DEPENDENCIES" = "YES" ] ; then
+
+ # Dependencies setup
+ echo
+ echo "** Installing dependencies"
+
+ sudo apt-add-repository universe
+ sudo apt-get update
+ sudo apt-get upgrade
+
+ cd $WHEREAMI
+
+ sudo apt-get install -y \
+ 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 \
+ libgl1 \
+ libglvnd-dev \
+ pkg-config
+
+ # GStreamer support
+ sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
+
+else
+
+ echo
+ echo "** Skipping dependencies installation"
+
+fi
+
+if [ ! -d "$OPENCV_SOURCE_DIR/opencv" ]; then
+
+ echo
+ echo "** Installing OpenCV sources in $OPENCV_SOURCE_DIR"
+
+ cd $OPENCV_SOURCE_DIR
+ git clone https://github.com/opencv/opencv.git
+
+fi
+
+checkout "$OPENCV_SOURCE_DIR/opencv"
+
+if [ ! -d "$OPENCV_SOURCE_DIR/opencv_contrib" ]; then
+
+ echo
+ echo "** Installing OpenCV Contributions sources in $OPENCV_SOURCE_DIR"
+
+ cd $OPENCV_SOURCE_DIR
+ git clone https://github.com/opencv/opencv_contrib.git
+
+fi
+
+checkout "$OPENCV_SOURCE_DIR/opencv_contrib"
+
+if [ ! -d "$OPENCV_SOURCE_DIR/opencv/build" ]; then
+
+ mkdir "$OPENCV_SOURCE_DIR/opencv/build"
+
+else
+
+ if [ "$CLEANUP" = "YES" ] ; then
+
+ rm -rf "$OPENCV_SOURCE_DIR/opencv/build"
+ mkdir "$OPENCV_SOURCE_DIR/opencv/build"
+
+ fi
+
+fi
+
+cd "$OPENCV_SOURCE_DIR/opencv/build"
+
+echo
+echo "** Configuring Cmake"
+
+time cmake -D CMAKE_BUILD_TYPE=RELEASE \
+ -D CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} \
+ -D OPENCV_EXTRA_MODULES_PATH="$OPENCV_SOURCE_DIR/opencv_contrib/modules" \
+ -D BUILD_TESTS=OFF \
+ -D BUILD_PERF_TESTS=OFF \
+ -D BUILD_LIST=\
+ opencv_core \
+ opencv_imgproc \
+ opencv_calib3d \
+ opencv_objdetect \
+ opencv_highgui \
+ opencv_videoio \
+ cudev \
+ aruco \
+ python2 \
+ python3 \
+ -D WITH_CUDA=ON \
+ -D CUDA_ARCH_BIN=${JETSON_CUDA_ARCH_BIN} \
+ -D CUDA_ARCH_PTX="" \
+ -D CUDA_FAST_MATH=ON \
+ -D CUDA_NVCC_FLAGS="--expt-relaxed-constexpr" \
+ -D ENABLE_FAST_MATH=ON \
+ -D WITH_CUBLAS=ON \
+ -D WITH_LIBV4L=ON \
+ -D WITH_GSTREAMER=ON \
+ -D WITH_GSTREAMER_0_10=OFF \
+ -D WITH_OPENGL=ON \
+ -D WITH_TBB=ON \
+ -D OpenGL_GL_PREFERENCE="LEGACY" \
+ -D PYTHON_DEFAULT_EXECUTABLE="$PYTHON_DIR/bin/python3" \
+ -D PYTHON3_EXECUTABLE="$PYTHON_DIR/bin/python3" \
+ -D PYTHON3_INCLUDE_DIR="$PYTHON_DIR/include/python3.11" \
+ -D PYTHON3_PACKAGES_PATH="$PYTHON_DIR/lib/python3.11" \
+ -D CPACK_BINARY_DEB=ON \
+ ../
+
+if [ $? -eq 0 ] ; then
+ echo
+ echo "CMake configuration make successful"
+else
+ echo
+ echo "CMake issues " >&2
+ echo "Please check the configuration being used"
+ exit 1
+fi
+
+echo
+echo "** Building OpenCV"
+
+time make -j$(($NUM_CPU - 1))
+
+if [ $? -eq 0 ] ; then
+ echo
+ echo "OpenCV make successful"
+else
+ echo
+ echo "Make did not build " >&2
+ echo "Retrying ... "
+
+ # Single thread this time
+ make
+
+ if [ $? -eq 0 ] ; then
+ echo
+ echo "OpenCV make successful"
+ else
+ echo
+ echo "Make did not successfully build" >&2
+ echo "Please fix issues and retry build"
+ exit 1
+ fi
+fi
+
+echo
+echo "** Installing OpenCV"
+
+sudo make install
+
+if [ $? -eq 0 ] ; then
+ echo
+ echo "OpenCV installed in: $CMAKE_INSTALL_PREFIX"
+else
+ echo "There was an issue with the final installation"
+ exit 1
+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
+
+echo
+echo "** Starting Packaging"
+
+sudo ldconfig
+
+time sudo make package -j$(($NUM_CPU - 1))
+
+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
+
+# 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