Skip to content

Instantly share code, notes, and snippets.

@rizky
Last active February 27, 2019 02:40
Show Gist options
  • Save rizky/a15fc19d11af7cf29ad4f8250e809698 to your computer and use it in GitHub Desktop.
Save rizky/a15fc19d11af7cf29ad4f8250e809698 to your computer and use it in GitHub Desktop.
Install OpenCV 3 with Python 2.7
# Install Xcode
# Before we can even think about compiling OpenCV, we first need to install Xcode,
# a full blown set of software development tools for the Mac Operating System.
# Accept the Apple Developer license
sudo xcodebuild -license
# Install Apple Command Line Tools
sudo xcode-select --install
# Install Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
nano ~/.bash_profile
# And insert the following lines at the bottom of the file
# export PATH=/usr/local/bin:$PATH
source ~/.bash_profile
# Installing Python via Homebrew
# In general, you do not want to develop against the system Python as your main interpreter
# Install python from an office installer provided by Python.org https://www.python.org/downloads/
which python
# Important: Be sure to inspect the output of the which command!
# If you see /Library/Frameworks/Python.framework/Versions/2.7/bin/python then you are correctly using the Hombrew version of Python.
# Install NumPy
pip install numpy
# Install OpenCV prerequisites using Homebrew
brew install cmake pkg-config
brew install jpeg libpng libtiff openexr
brew install eigen tbb
# Download the OpenCV 3 source from GitHub
cd ~
git clone https://github.com/opencv/opencv
git clone https://github.com/opencv/opencv_contrib
# Configuring OpenCV 3 and Python 2.7 via CMake on macOS
cd ~/opencv
mkdir build
cd build
# OpenCV 3 + Python 2.7 CMake template for macOS
PYTHON2_LIBRARY=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
PYTHON2_INCLUDE_DIR=/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/
PYTHON2_EXECUTABLE=/Library/Frameworks/Python.framework/Versions/2.7/bin/python
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON2_LIBRARY=$PYTHON2_LIBRARY \
-D PYTHON2_INCLUDE_DIR=$PYTHON2_INCLUDE_DIR \
-D PYTHON2_EXECUTABLE=$PYTHON2_EXECUTABLE \
-D BUILD_opencv_python2=ON \
-D BUILD_opencv_python3=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=ON ..
# Compile and install OpenCV on macOS
make -j4
# Sym-link your OpenCV 3 + Python 2.7 bindings
cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
ls -l cv2.so
# -rwxr-xr-x 1 root admin 3694564 Nov 15 09:20 cv2.so
# Testing your OpenCV install on macOS
python
# >>> import cv2
# >>> cv2.__version__
# '3.1.0-dev'
# >>>
#Include cv2 to the project
ln -s /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages cv2.so
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment