Skip to content

Instantly share code, notes, and snippets.

@leonmak
Last active August 30, 2017 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmak/67a70ccc83e149058c29b51b1ca0d161 to your computer and use it in GitHub Desktop.
Save leonmak/67a70ccc83e149058c29b51b1ca0d161 to your computer and use it in GitHub Desktop.
Install opencv3 for python3 on mac

Summary of installation

  • install XCode, homebrew
  • install python3:
    • brew install python3
    • brew linkapps python3
  • if existing virtual environment is installed, activate it
  • else activate new virtualenv cv (using virtualenvwrapper):
    • pip install virtualenv virtualenvwrapper
    • echo "export WORKON_HOME=~/.virtualenvs" >> ~/.bash_profile
    • echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
    • . ~/.bash_profile
    • mkvirtualenv cv -p python3
    • workon cv
  • install opencv requirements:
    • pip install numpy
    • brew install cmake pkg-config jpeg libpng libtiff openexr eigen tbb
  • download opencv3 from github source:
    • cd ~
    • git clone https://github.com/opencv/opencv
    • git clone https://github.com/opencv/opencv_contrib
  • compile and build the source
    • cd ~/opencv && mkdir build-test && cd $_
    •   PY3LIB=$(ls -t /usr/local/Cellar/python3/3.*.*/Frameworks/Python.framework/Versions/3.*/lib/python3.*/config-3.*m*/libpython3.*[0-9].dylib | head -1)
        PY3INCLDIR=$(ls -td /usr/local/Cellar/python3/3.*.*/Frameworks/Python.framework/Versions/3.*/include/python3.*m/ | head -1)
      
        cmake -D CMAKE_BUILD_TYPE=RELEASE \
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
        -D PYTHON3_LIBRARY=$PY3LIB \
        -D PYTHON3_INCLUDE_DIR=$PY3INCLDIR \
        -D PYTHON3_EXECUTABLE=$VIRTUAL_ENV/bin/python \
        -D BUILD_opencv_python2=OFF \
        -D BUILD_opencv_python3=ON \
        -D INSTALL_PYTHON_EXAMPLES=ON \
        -D INSTALL_C_EXAMPLES=OFF \
        -D BUILD_EXAMPLES=ON ..
    • check the cmake output for errors
    • make -j4
    • sudo make install
  • rename and sym-link your OpenCV 3 + Python 3.5 bindings
    •   PY3LOCALPACKAGES=$(ls -td /usr/local/lib/python3.*/site-packages | head -1)
        cd $PY3LOCALPACKAGES
        PY3CVSO=$(ls *.so | head -1)
        mv $PY3CVSO cv2.so 
        PY3VENVPACKAGES=$(ls -d $VIRTUAL_ENV/lib/python3.*/site-packages | head -1)
        cd $PY3VENVPACKAGES
        ln -s $PY3LOCALPACKAGES/cv2.so cv2.so
# based on pyimagesearch tutorial
# http://www.pyimagesearch.com/2016/12/05/macos-install-opencv-3-and-python-3-5/
echo "~ Installing and linking python3 ~"
brew install python3
brew linkapps python3
echo "~ Installing virtualenv wrapper ~"
pip install virtualenv virtualenvwrapper
echo "export WORKON_HOME=~/.virtualenvs" >> ~/.bash_profile
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile
source ~/.bash_profile
if [ $# -eq 0 ]
VENVNAME=cv
else
VENVNAME=$1
fi
mkvirtualenv $VENVNAME -p python3
workon $VENVNAME
echo "~ Using virtual env: $VENVNAME ~"
cd ~
git clone https://github.com/opencv/opencv
git clone https://github.com/opencv/opencv_contrib
cd ~/opencv && mkdir build-test && cd $_
echo "~ Compiling from souce file ~"
PY3LIB=$(ls -t /usr/local/Cellar/python3/3.*.*/Frameworks/Python.framework/Versions/3.*/lib/python3.*/config-3.*m*/libpython3.*[0-9].dylib | head -1)
PY3INCLDIR=$(ls -td /usr/local/Cellar/python3/3.*.*/Frameworks/Python.framework/Versions/3.*/include/python3.*m/ | head -1)
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON3_LIBRARY=$PY3LIB \
-D PYTHON3_INCLUDE_DIR=$PY3INCLDIR \
-D PYTHON3_EXECUTABLE=$VIRTUAL_ENV/bin/python \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=ON ..
echo "~ Building from souce file ~"
make -j4
sudo make install
PY3LOCALPACKAGES=$(ls -td /usr/local/lib/python3.*/site-packages | head -1)
cd $PY3LOCALPACKAGES
PY3CVSO=$(ls *.so | head -1)
mv $PY3CVSO cv2.so
PY3VENVPACKAGES=$(ls -d $VIRTUAL_ENV/lib/python3.*/site-packages | head -1)
cd $PY3VENVPACKAGES
ln -s $PY3LOCALPACKAGES/cv2.so cv2.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment