Skip to content

Instantly share code, notes, and snippets.

@PolarNick239
Last active March 27, 2018 00:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PolarNick239/fc02a69ae3082e72704d220e77abf4a0 to your computer and use it in GitHub Desktop.
Save PolarNick239/fc02a69ae3082e72704d220e77abf4a0 to your computer and use it in GitHub Desktop.
OpenCV compilation script for python bundled with PhotoScan. Should work with any other Python (and with virtualenv). Tested on Ubuntu 16.04
# Configure path to photoscan:
PHOTOSCAN_PATH=<.../photoscan-pro>
PYTHON_PATH=${PHOTOSCAN_PATH}/python
OPENCV_VERSION=3.4.0
INSTALLATION_PATH=/opt/opencv-${OPENCV_VERSION}
sudo mkdir -p ${INSTALLATION_PATH}
# Install numpy (OpenCV requires it)
CPPFLAGS=-I${PYTHON_PATH}/include/python3.5m LDFLAGS=-L${PYTHON_PATH}/lib ${PYTHON_PATH}/bin/python3.5 -m pip install numpy==1.08
# # If you see error like "ImportError: cannot import name 'HTTPSHandler'" - you need to execute these lines and after that try to install numpy again:
#
# wget http://snapshot.debian.org/archive/debian/20110406T213352Z/pool/main/o/openssl098/libssl0.9.8_0.9.8o-7_amd64.deb
# sudo dpkg -i libssl0.9.8_0.9.8o-7_amd64.deb
# rm libssl0.9.8_0.9.8o-7_amd64.deb
wget https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip
unzip ${OPENCV_VERSION}.zip
rm ${OPENCV_VERSION}.zip
cd opencv-${OPENCV_VERSION}
# # Alternatively you can get the hole git repository (this is slower):
# git clone https://github.com/opencv/opencv
# cd opencv
# git checkout tags/${OPENCV_VERSION}
mkdir build
cd build
sudo apt update
sudo apt install -y cmake
# Configure opencv build:
#
# You can compile opencv_contrib with this option:
# -D OPENCV_EXTRA_MODULES_PATH=.../opencv_contrib/modules \
# If you have CUDA SDK installed - you can speedup compilation by disabling CUDA support:
# -D WITH_CUDA=OFF \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=${INSTALLATION_PATH} \
-D PYTHON3_EXECUTABLE=${PYTHON_PATH}/bin/python3.5 \
..
make -j8
# Installing OpenCV library w.r.t. INSTALLATION_PATH variable
sudo make install
cd ..
# Now you can cleanup build dir with:
# rm -rf build
# You can even cleanup opencv source code dir:
# cd ..
# rm -rf opencv-${OPENCV_VERSION}
# Add cv2 package to photoscan's bundled python
ln -s ${INSTALLATION_PATH}/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so ${PYTHON_PATH}/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment