Install OpenCV 3 with Python 3 on macOS Sierra
Posted on November 29, 2016 by Sol
In this article, I will show you how to install OpenCV 3 with Python 3 on macOS Sierra. Most tutorials I found online, including the OpenCV documentation, seem concerned only with Python 2.7.
MacOS comes by default with Python 2.7 which, at this point, receives only bug fixes and will be EOL by 2020. Python 3.x is the future and it is supported by all major Python libraries. In this tutorial, we’ll use the latest stable Python 3 release, Python 3.5.2.
There is more than one way to install Python 3 on macOS. From my experience, the easiest approach for beginners is to use a package manager like Miniconda. Select the 3.5 bash installer from the Miniconda download page. Once the download is finished, open a Terminal and launch the installer:
1 cd Downloads/ 2 bash Miniconda3-latest-MacOSX-x86_64.sh
For the most part, you can use the defaults suggested by the installer. Be careful when asked if you want Miniconda added to your PATH. If you chose yes, the Miniconda Python will shadow the system Python, so when you will write python in a Terminal you will launch Python 3.5 instead of the default 2.7. My advice is to add Miniconda to your PATH. If, at some point, you want to revert to 2.7, it is as simple as commenting the Miniconda line from your .bash_profile file.
Once the installation is finished, assuming you kept the installer defaults, you need to enable the new PATH settings. This can be achieved by closing and reopening your Terminal, or simply by writing:
1 cd ~ 2 . .bash_profile
Now, you should have the conda command available in your PATH. A quick test is to run the conda info command. This is what I see in my case:
1 ~ $ conda info 2 Current conda install: 3 4 platform : osx-64 5 conda version : 4.1.11 6 conda-env version : 2.5.2 7 conda-build version : not installed 8 python version : 3.5.2.final.0 9 requests version : 2.10.0 10 root environment : /Users/sol/miniconda3 (writable) 11 default environment : /Users/sol/miniconda3 12 envs directories : /Users/sol/miniconda3/envs 13 package cache : /Users/sol/miniconda3/pkgs 14 channel URLs : https://repo.continuum.io/pkgs/free/osx-64/ 15 https://repo.continuum.io/pkgs/free/noarch/ 16 https://repo.continuum.io/pkgs/pro/osx-64/ 17 https://repo.continuum.io/pkgs/pro/noarch/ 18 config file : None 19 offline mode : False 20 is foreign system : False 21 22 ~ $
Let’s follow best practices and create a new Python environment:
1 conda create -n myenv python=3.5 2 source activate myenv
At this point, your prompt should indicate that you are using the myenv environment. An environment allows you to have different versions of Python and libraries on the same machine. As an example, you could have a myenv environment where you’ve installed SciPy and a play environment where you’ve installed PyGame. The environments are completely separate from each other. This is useful if you want, for example, to experiment with the development version of Python or some other library, while keeping the stable versions separate.
Once an environment is activated, all the install commands will apply only to the current environment. By default, if you close your Terminal, the environment is deactivated. If you want to be able to use it, use the source activate myenv command.
OpenCV depends on NumPy, which can be installed with:
1 conda install numpy
OpenCV is not directly provided in the main Miniconda repository, it is contributed by third parties to the main Anaconda repository. We need to install the anaconda-client command utility in order to be able to search for the OpenCV binary:
1 conda install anaconda-client
Now, use the next command to search for OpenCV 3:
1 anaconda search -t conda opencv3
You should see a list with the available OpenCV 3 distributions, something like in the next image:
From the above list, I will chose the package named menpo/opencv3 since it provides binaries for all major operating systems and most importantly for osx-64. You can install the menpo/opencv3 package with:
1 conda install --channel https://conda.anaconda.org/menpo opencv3
At this point, you should have OpenCV 3 and Python installed on your Mac. We can write a small test program that will print the OpenCV version, load an image from the disk, convert the image to gray and show the result. Start by downloading the next image:
Save it as clouds.jpg. In the same folder where you’ve saved the above image, create a new file demo.py and write the next code:
1 import cv2 2 3 print("OpenCV version:") 4 print(cv2.__version__) 5 6 img = cv2.imread("clouds.jpg") 7 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 8 9 cv2.imshow("Over the Clouds", img) 10 cv2.imshow("Over the Clouds - gray", gray) 11 12 cv2.waitKey(0) 13 cv2.destroyAllWindows()
Run the code with:
1 python demo.py
(You can close the two windows by pressing ESC!)
You should see something like in the next figure. By default, the last image (the gray one) will be over the first one. You need to move the window in order to see both images:
If you want to learn more about OpenCV and Python, I would recommend reading OpenCV with Python Blueprints by M. Beyeler:
or, for OpenCV C++, OpenCV By Example by P. Joshi:comments powered by Disqus