Install OpenCV 3 + Python 3.5 macOS tutorial
This is a rapid tutorial to install OpenCV 3 on macOS. I have eliminated long description and focused only on installation commands more.
1. Install XCode
In order to compile OpenCV on macOS, we need to install XCode first. The simplest way to get XCode is right from your App Store application in your Mac. Open App Store -> Search “XCode” -> Get.
Although, I prefer to get it from my apple developer account. If you have a developer’s account, then you can download the latest version from here.
Once you are done installing XCode, you need to now agree the developer license. Run the following command in the terminal and follow the instructions in terminal to agree the license:
1 |
$ sudo xcodebuild -license |
2. Install Command Line Tools
Next step is to install Apple Command Line Tools. Run the following command in terminal:
1 |
$ sudo xcode-select --install |
Upon executing the above command, a pop will appear. Click “install”.
3. Install Homebrew
Homebrew is a package manager for macOS. To install homebrew run the following command in terminal:
1 |
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
Update homebrew
1 |
$ brew update |
Now we need to update our ~/.bash_profile file. You can run the following command to open up the file in nano editor. I am using nano editor.
1 |
$ nano ~/.bash_profile |
Add following lines to the file to update the path variable to look for the libraries in Homebrew path before system path:
1 2 |
#Homebrew export PATH=/usr/local/bin:$PATH |
Don’t forget to save and exit the file. Then use the source command to with your bash profile:
1 |
$ source ~/.bash_profile |
4. Install Python 3.5
Use the following command to install python:
1 |
$ brew install python3 |
After installation finishes, create symbolic links:
1 |
$ brew linkapps python3 |
5. Install Python Virtual Environments and NumPy
In simple terms, virtual environments is something that lets us use different versions of libraries, softwares on same system. For example, in a virtual environment named cv2, I can install OpenCV 2 and in virtual environment cv3, I can install OpenCV 3. Now when I wish to work with the older version of the library then I work in cv2 environment and for newer I use cv3 environment. Similarly, different Python versions in different virtual environments.
Use following command to install virtualenv and virtualenvwrapper.
1 |
$ pip install virtualenv virtualenvwrapper |
Once again update the bash profile file:
1 |
$ nano ~/.bash_profile |
Add following lines to it:
1 2 |
#Virtualenv/VirtualenvWrapper source /usr/local/bin/virtualenvwrapper.sh |
Then once again source your bash profile:
1 |
$ source ~/.bash_profile |
Create Python 3 Virtual Environment
Use the following command to create a virtual environment:
1 |
$ mkvirtualenv cv3 -p python3 |
where -p is to determine which python version we are using for this environment.
After creating the virtual environment, in order to work within it, use ‘workon’ command:
1 |
$ workon cv3 |
OpenCV is going to need NumPy for math operations. Install it using following command:
1 |
$ pip install numpy |
6. Install Prerequisite for OpenCV
Use the following commands to install them:
1 2 3 |
$ brew install cmake pkg-config $ brew install jpeg libpng libtiff openexr $ brew install eigen tbb |
7. Downloading OpenCV
1 2 |
$ git clone https://github.com/opencv/opencv $ git clone https://github.com/opencv/opencv_contrib |
Configure OpenCV and Python using CMake
Create directory ‘build’ in OpenCV directory using:
1 2 3 |
$ cd ~/opencv $ mkdir build $ cd build |
Run the cmake command as following:
1 2 3 4 5 6 7 8 9 10 11 |
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D PYTHON3_LIBRARY=/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/config-3.5m/libpython3.5.dylib \ -D PYTHON3_INCLUDE_DIR=/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/include/python3.5m/ \ -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 .. |
In the above command, you need to fill your PYTHON3_LIBRARY and PYTHON3_INCLUDE_DIR path.
8. Compile OpenCV
Compile OpenCV using the following command:
1 |
$ sudo make -j4 |
Once it finishes, install it using the command:
1 |
$ sudo make install |
9. Check Installation
1 2 3 4 5 6 |
$ workon cv3 $ python Python 3.5.2 (default, Oct 11 2016, 04:59:56) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0-dev' >>> |
Note: If you are willing to use TensoFlow along with OpenCV and in case, if you have installed TensorFlow using virtualenv tutorial, then you won’t be able to work on both. You will have to install TensorFlow again in the OpenCV virtualenv we created. In our example, we have to install TensoFlow in cv3 virtualenv.