Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
set -e
set -u
onerror()
{
status=$?
script=$0
line=$1
shift
args=
for i in "$@"; do
args+="\"$i\" "
done
echo ""
echo "------------------------------------------------------------"
echo "Error occured on $script [Line $line]: Status $status"
echo ""
echo "PID: $$"
echo "User: $USER"
echo "Current directory: $PWD"
echo "Command line: $script $args"
echo "------------------------------------------------------------"
echo ""
}
begintrap()
{
set -e
trap 'onerror $LINENO "$@"' ERR
}
begintrap
PREFIX=$HOME/local
WORKING_DIR="/tmp/$(id -nu)"
LIBJPEG_VERSION="1.5.3"
OPENCV_VERSION="3.4.10"
PYTHON=python3
PYTHON_CONFIG=python3-config
PYTHON_PREFIX=$($PYTHON_CONFIG --prefix)
PYTHON_VERSION=$($PYTHON -c 'import sys; vinfo=sys.version_info; print("{}.{}".format(vinfo[0], vinfo[1]))')
PYTHON_EXE=$($PYTHON -c "import sys; print(sys.executable,end='')")
PYTHON_H=$($PYTHON_CONFIG --cflags | grep -oP "(?<=-I)[^ ]+" | head -n 1)/Python.h
PYCONFIG_H=$($PYTHON_CONFIG --cflags | grep -oP "(?<=-I)[^ ]+" | head -n 1)/pyconfig.h
# Find libpython???.so (a bit complicated)
PYTHON_LIBRARY=$(find $PYTHON_PREFIX -name "libpython*.so" -exec readlink -f {} \; \
| sort | uniq | grep -E "${PYTHON_VERSION}m?" | head -n 1)
PYTHON_INCLUDE_DIR=$(dirname $PYTHON_H) # directory where Python.h is installed
PYTHON_INCLUDE_DIR2=$(dirname $PYCONFIG_H) # directory where pyconfig.h is installed
PYTHON_NUMPY_INCLUDE_DIR=$(python3 -c "import numpy; print(numpy.get_include())")
mkdir -p $WORKING_DIR
if [[ ! -e "${PREFIX}/lib/libturbojpeg.so" ]]; then
LIBJPEG_TAR_NAME="libjpeg-turbo-${LIBJPEG_VERSION}.tar.gz"
LIBJPEG_URL="https://ftp.osuosl.org/pub/blfs/conglomeration/libjpeg-turbo/${LIBJPEG_TAR_NAME}"
LIBJPEG_DIR="${WORKING_DIR}/libjpeg-turbo-${LIBJPEG_VERSION}"
if [[ ! -e ${LIBJPEG_DIR} ]]; then
cd ${WORKING_DIR}
wget ${LIBJPEG_URL} -O - | tar zxvf -
fi
cd ${LIBJPEG_DIR}
./configure --prefix="${PREFIX}"
make -j 48
make install
fi
if ! $PYTHON -c "import cv2" 2>/dev/null; then
OPENCV_URL=https://github.com/opencv/opencv.git
OPENCV_DIR="${WORKING_DIR}/opencv-${OPENCV_VERSION}"
if [[ ! -e ${OPENCV_DIR} ]]; then
cd ${WORKING_DIR}
git clone ${OPENCV_URL}
mv opencv ${OPENCV_DIR}
fi
cd ${OPENCV_DIR}
git checkout ${OPENCV_VERSION}
#OPENCV_URL="https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.tar.gz"
#if [[ ! -e ${OPENCV_DIR} ]]; then
#cd ${WORKING_DIR}
#wget ${OPENCV_URL} -O - | tar zxvf -
#fi
cd ${WORKING_DIR}
OPENCV_CONTRIB_URL="https://github.com/opencv/opencv_contrib.git"
OPENCV_CONTRIB_DIR="${WORKING_DIR}/opencv_contrib-${OPENCV_VERSION}"
if [[ ! -e ${OPENCV_CONTRIB_DIR} ]]; then
git clone ${OPENCV_CONTRIB_URL}
mv opencv_contrib ${OPENCV_CONTRIB_DIR}
fi
cd ${OPENCV_CONTRIB_DIR}
git checkout ${OPENCV_VERSION}
OPENCV_BUILD_DIR="${WORKING_DIR}/opencv-build"
mkdir -p ${OPENCV_BUILD_DIR}
cd ${OPENCV_BUILD_DIR}
rm -rf *
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$PREFIX \
-D WITH_TBB=ON -D WITH_EIGEN=OFF -D WITH_FFMPEG=ON \
-D WITH_QT=OFF -DWITH_CUDA=OFF -DWITH_OPENCL=OFF -D BUILD_opencv_python3=ON \
-DOPENCV_EXTRA_MODULES_PATH="${OPENCV_CONTRIB_DIR}/modules" \
-D WITH_JPEG=ON -D BUILD_JPEG=OFF \
-D JPEG_INCLUDE_DIR="${PREFIX}/include" \
-D JPEG_LIBRARY="${PREFIX}/lib/libjpeg.so" \
-D ZLIB_LIBRARY=/usr/lib/x86_64-linux-gnu/libz.so \
-D ZLIB_INCLUDE_DIRS=/usr/include \
-D PYTHON_LIBRARIES=$PYTHON_LIBRARY \
-D PYTHON3_LIBRARIES=$PYTHON_LIBRARY \
-D PYTHON3_EXECUTABLE=$PYTHON_EXE \
-D PYTHON_INCLUDE_DIR=$PYTHON_INCLUDE_DIR \
-D PYTHON_INCLUDE_DIR2=$PYTHON_INCLUDE_DIR2 \
-D PYTHON3_NUMPY_INCLUDE_DIRS=$PYTHON_NUMPY_INCLUDE_DIR \
${OPENCV_DIR}
make -j 48
make install
cp ./lib/python3/cv2.*.so ${PYTHON_PREFIX}/lib/python${PYTHON_VERSION}/
cd "${PYTHON_PREFIX}/lib/python${PYTHON_VERSION}"
ln -s cv2.*.so cv2.so
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment