Install OpenCV3-python on Mac

Give credits to learnopencv

Step 1 Install Homebrew

See Homebrew

If you are using an old version, you need to tap to homebrew/science.

# Homebrew recently moved popular formuale to homebrew-core
# So this is not needed anymore to install OpenCV and you can skip this step.
# tap science repo of home
brew brew tap homebrew/science

Step 2 Install Python3

Python 2 will be deprecated shortly in the future. Python 3 is suggested.

See Install Python 3 Via Homebrew

Step 3 Create Virtual Environment for OpenCV and Use It

This step is optional but strongly recommended.

See Python Virtual Environment

Step 4 Install OpenCV Via Brew

brew install opencv

Step 5 Add OpenCV’s site-packages path to global site-packages

Remember to check the paths since python is also updating.

# python 2
echo /usr/local/opt/opencv/lib/python2.7/site-packages >>
/usr/local/lib/python2.7/site-packages/opencv3.pth

# python 3
echo /usr/local/opt/opencv/lib/python3.7/site-packages >>
/usr/local/lib/python3.7/site-packages/opencv3.pth

Step 7 Test

# Activate virtual environment
workon <envname>

# open ipython (run this command on terminal)
ipython
# import cv2 and print version (run following commands in ipython)
import cv2
print(cv2.__version__)
# If OpenCV3 is installed correctly, above command should give output the version number
# Press CTRL+D to exit ipython