based on pyimagesearch tutorial
- install XCode, homebrew
- install python3:
brew install python3
brew linkapps python3
- if existing virtual environment is installed, activate it
- else activate new virtualenv
cv
(usingvirtualenvwrapper
):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
-