Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
set -e
TARGET_DIR="$(dirname $(readlink -m $0))/.local"
WORKING_DIR="/tmp/"
LIBJPEG_VERSION="1.5.3"
OPENCV_VERSION="3.4.1"
eval "$(pyenv init -)"
PYENV_VERSION=$(pyenv version | cut -f 1 -d ' ')
pyenv shell ${PYENV_VERSION} # To fix the version throughout this script
if [[ ! -e "${TARGET_DIR}/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="${TARGET_DIR}"
make -j 48
make install
fi
if [[ ! -e "${TARGET_DIR}/lib/python3.6/site-packages/cv2.so" ]]; then
OPENCV_URL="https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.tar.gz"
OPENCV_DIR="${WORKING_DIR}/opencv-${OPENCV_VERSION}"
if [[ ! -e ${OPENCV_DIR} ]]; then
cd ${WORKING_DIR}
wget ${OPENCV_URL} -O - | tar zxvf -
fi
OPENCV_CONTRIB_URL="https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.tar.gz"
OPENCV_CONTRIB_DIR="${WORKING_DIR}/opencv_contrib-${OPENCV_VERSION}"
if [[ ! -e ${OPENCV_CONTRIB_DIR} ]]; then
cd ${WORKING_DIR}
wget ${OPENCV_CONTRIB_URL} -O - | tar zxvf -
fi
OPENCV_BUILD_DIR="${WORKING_DIR}/opencv-build"
mkdir -p ${OPENCV_BUILD_DIR}
cd ${OPENCV_BUILD_DIR}
rm -rf *
PYTHON_ROOT=$(python-config --prefix)
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$TARGET_DIR \
-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="${TARGET_DIR}/include" -D JPEG_LIBRARY="${TARGET_DIR}/lib/libjpeg.so" \
-D ZLIB_LIBRARY=/usr/lib/x86_64-linux-gnu/libz.so \
-D ZLIB_INCLUDE_DIRS=/usr/include \
-D PYTHON3_LIBRARY="${PYTHON_ROOT}/lib/libpython3.so" \
-D PYTHON3_EXECUTABLE="${PYTHON_ROOT}/bin/python3.6m" \
-D PYTHON_INCLUDE_DIR="${PYTHON_ROOT}/include/python3.6m" \
-D PYTHON_LIBRARY="${PYTHON_ROOT}/lib/x86_64-linux-gnu/libpython3.6m.so.1.0" \
-D PYTHON3_NUMPY_INCLUDE_DIRS="${PYTHON_ROOT}/lib/python3.6/site-packages/numpy/core/include" \
${OPENCV_DIR}
make -j 48
make install
cd "${TARGET_DIR}/lib/python3.6/site-packages"
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