Install OpenCV 3 with Python 3 on macOS High Sierra
Posted on November 29, 2016 by Sol
Updated 20 April 2018
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.6.4
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.6 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.6 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 3 active environment : None 4 user config file : /Users/paul/.condarc 5 populated config files : 6 conda version : 4.4.10 7 conda-build version : not installed 8 python version : 3.6.4.final.0 9 base environment : /Users/paul/miniconda3 (writable) 10 channel URLs : https://repo.continuum.io/pkgs/main/osx-64 11 https://repo.continuum.io/pkgs/main/noarch 12 https://repo.continuum.io/pkgs/free/osx-64 13 https://repo.continuum.io/pkgs/free/noarch 14 https://repo.continuum.io/pkgs/r/osx-64 15 https://repo.continuum.io/pkgs/r/noarch 16 https://repo.continuum.io/pkgs/pro/osx-64 17 https://repo.continuum.io/pkgs/pro/noarch 18 package cache : /Users/paul/miniconda3/pkgs 19 /Users/paul/.conda/pkgs 20 envs directories : /Users/paul/miniconda3/envs 21 /Users/paul/.conda/envs 22 platform : osx-64 23 user-agent : conda/4.4.10 requests/2.18.4 CPython/3.6.4 Darwin/17.5.0 OSX/10.13.4 24 UID:GID : 501:20 25 netrc file : None 26 offline mode : False 27 ~ $
Let’s follow best practices and create a new Python environment:
1 conda create -n myenv python=3.6 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.
Next, use the conda manager to search for opencv:
1 conda search opencv
If you run the above command, you should a list with all the available versions of OpenCV, e.g. this is what is present at the time of this writing:
1 (myenv) ~ $ conda search opencv 2 Loading channels: done 3 Name Version Build Channel 4 opencv 2.4.8 np17py27_2 defaults 5 opencv 3.3.1 py27h2dac755_0 defaults 6 opencv 3.3.1 py27h4e978fd_0 defaults 7 opencv 3.3.1 py27h60a5f38_1 defaults 8 opencv 3.3.1 py27hb620dcb_1 defaults 9 opencv 3.3.1 py35h3a6858e_0 defaults 10 opencv 3.3.1 py35h60a5f38_1 defaults 11 opencv 3.3.1 py35h8302012_0 defaults 12 opencv 3.3.1 py35hb620dcb_1 defaults 13 opencv 3.3.1 py36h60a5f38_1 defaults 14 opencv 3.3.1 py36h7bea28e_0 defaults 15 opencv 3.3.1 py36hb620dcb_1 defaults 16 opencv 3.3.1 py36he41e7d5_0 defaults 17 (myenv) ~ $
Pay attention to the Version and the Build columns, from the above listing we can see that we can install OpenCV 3.3.1 for Python 3.6. Next, use the conda package manager to install a particular version of OpenCV:
1 conda install opencv=3.3.1
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: