Step 1: Install XCode Install XCode from App Store.
If XCode available on App Store is not compatible with your OS:
Find XCode version compatible to your OS from this table https://en.wikipedia.org/w/index.php?title=Xcode#Version_comparison_table Go to this webpage https://developer.apple.com/download/more/ Login if you have apple developer account else create your account and login. Search for xcode and download the version compatible to your OS. Install XCode. After installation open XCode, and accept xcode-build license when it asks. Step 2: Install Homebrew Launch a terminal from Launchpad. From this step onward, all commands will be run in the terminal.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
echo "# Homebrew" >> ~/.bash_profile echo "export PATH=/usr/local/bin:$PATH" >> ~/.bash_profile source ~/.bash_profile
brew brew tap homebrew/science
Step 3: Install Python 2 and Python 3
brew install python python3 brew link python brew link python3
brew upgrade python brew upgrade python3
which python2 # it should output /usr/local/bin/python2 which python3 # it should output /usr/local/bin/python3
python2 --version python3 --version Python version (2.6 or 2.7, 3.5 or 3.6) installed on your machine is required to determine path of Python’s site-packages. It will be used later.
NOTE : Recently Homebrew made some changes in Python formula. Earlier homebrew used to install python2 as /usr/local/bin/python. Now it follows these rules:
Install python2 at /usr/local/bin/python2 Install python3 at /usr/local/bin/python3 python command will point to /usr/bin/python. This is the python distribution which comes with your OS and not installed by Homebrew. We want to use Python installed by Homebrew because it makes installing/managing packages easier. To run python scripts you should run command python2 and python3 for Python 2 & 3 respectively. If you find this annoying and want to use command python to run python2, add following line to ~/.bash_profile.
export PATH="/usr/local/opt/python/libexec/bin:$PATH" This step is recommended not just for this course but in general to keep the python installation clean.
Step 4: Install Python libraries in a Virtual Environment We will use Virtual Environment to install Python libraries. It is generally a good practice in order to separate your project environment and global environment.
pip install virtualenv virtualenvwrapper echo "# Virtual Environment Wrapper" echo "VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2" >> ~/.bash_profile echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bash_profile source ~/.bash_profile
############ For Python 2 ############
mkvirtualenv facecourse-py2 -p python2 workon facecourse-py2
pip install numpy scipy matplotlib scikit-image scikit-learn ipython pandas
deactivate ######################################
############ For Python 3 ############
mkvirtualenv facecourse-py3 -p python3 workon facecourse-py3
pip install numpy scipy matplotlib scikit-image scikit-learn ipython pandas
deactivate ###################################### Step 5: Install OpenCV Step 5.1 : Compile & Install OpenCV Note: Homebrew recently moved many popular formulae to homebrew-core. Earlier you can install OpenCV 2 using formula name opencv and OpenCV 3 using formula name opencv3. They have now renamed opencv3 to opencv and opencv to opencv@2. Various options such as –with-qt, –with-tbb etc are also removed in the updated formula and CUDA support is also dropped. You can check these github issues 15853, 6126, 16640, 5996 to read more about it.
Homebrew has also made it compulsory to compile and install Python bindings for both Python 2 and Python 3. So if you don’t have Python 3 installed, Homebrew will install it while installing opencv.
brew install opencv Step 5.2 : Add OpenCV’s site-packages path to global site-packages When brew is done compiling and installing OpenCV3, we will update path of site-packages directory which contains cv2.so file to Homebrew Python’s site-packages directory. Depending upon the Python version you have (2.6/2.7 or 3.5/3.6) these paths would be different.
############ For Python 2 ############
echo /usr/local/opt/opencv/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
############ For Python 3 ############
echo /usr/local/opt/opencv/lib/python3.6/site-packages >> /usr/local/lib/python3.6/site-packages/opencv3.pth Step 6: Make OpenCV3 Python symlink in our virtual environment Path to OpenCV’s Python library will be different depending upon which Python version you have. Double check the exact path and filename on your machine. Use this command to find out the path on your machine.
find /usr/local/opt/opencv3/lib/ -name cv2*.so
############ For Python 2 ############ cd ~/.virtualenvs/facecourse-py2/lib/python2.7/site-packages/ ln -s /usr/local/opt/opencv3/lib/python2.7/site-packages/cv2.so cv2.so
############ For Python 3 ############ cd ~/.virtualenvs/facecourse-py3/lib/python3.6/site-packages/ ln -s /usr/local/opt/opencv3/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so Step 7: Test OpenCV3
############ For Python 2 ############ workon facecourse-py2
############ For Python 3 ############ workon facecourse-py3
ipython
import cv2 print cv2.version
Now you can exit from Python virtual environment.
deactivate Now whenever you are going to run Python scripts which use OpenCV you have to activate the virtual environment we created using workon command.