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.
Step 3 Create Virtual Environment for OpenCV and Use It¶
This step is optional but strongly recommended.
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 6 Make OpenCV3 Python Symlink in the Virtual Environment¶
Please double check the paths and files. I’m only showing the files and paths in my system.
# check whether the path is correct
find /usr/local/opt/opencv/lib/ -name cv2*.so
# python 2
cd ~/.virtualenvs/<envname>/lib/python2.7/site-packages/
ln -s /usr/local/opt/opencv/lib/python2.7/site-packages/cv2.so cv2.so
# python 3
cd ~/.virtualenvs/<envname>/lib/python3.7/site-packages/
ln -s /usr/local/opt/opencv/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-darwin.so cv2.so
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