Opencv For Python Mac
What’s OpenCV?
- Create a temporary directory, which we denote as buildopencv, where you want to put the.
- 2 days ago Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you are invited to install the most recent version of Python 3 from the Python website ( ). A current “universal binary” build of Python, which runs natively on the Mac’s new Intel and legacy PPC CPU’s, is available there.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.
Ahhh, computer vision, such a cool field! Lately, I’ve been trying to become more knowledgeable about CV and image processing in python. OpenCV (CV = ‘computer vision’) is an excellent open source computer vision software library written in C++ that supports C++, C, Python, Java, and Matlab API’s. OpenCV will supply you with functions that will let you detect faces in images, track objects in a video, and perform any number of image processing tasks.
The only problem is: how the hell do I install OpenCV so that I can use it in conjunction with a Jupyter notebook? Let’s be honest, most likely you’re either you’re using a Jupyter notebook, Spyder, or the ipython terminal (if you’re a real sadist) to test your python code. And especially if you’re coding for image processing, you’re going to want to view your progress without having (a) a million separate images open and (b) having to wait for Spyder to inevitably crash. That’s the beauty of a Jupyter notebook - when you’re using it with Matplotlib, you can just display your images and videos in a living document!
For me, my ideal OpenCV situation would be for me to be able to simply type and evaluate the following import
statements with zero errors or package conficts:
Problems with traditional installation methods
There are many ways to install OpenCV. The standard approach is to download it from the OpenCV website and then compile and install OpenCV using the software building utility “CMake” all within a virutal Python environment. I’ve gone down this route according to Adrian Rosebrock’s fabulous installation walkthrough, and if you just want to have access to OpenCV 3.0, I suggest you consider it. But, at the end of the day, there are even more steps required after Adrian’s 9 steps to get OpenCV compatible with a Jupyter notebook. Other installation walkthroughs I’ve found tend to be generally convoluted and assume that you have Homebrew, XCode, maybe MacPorts, or just experience in general with installing and building software packages. Wouldn’t it be great if we could just run something analogous to pip install opencv
?
If you’re like me (maybe you’re not) I often think that pip install
‘ing a Python package is the same thing as R’s install.packages
function - while we get similar functionality, R packages come with the luxury of basically never interfering with other R package dependencies! If one package needs a newer or older version of some other package you’ve already installed, install.packages
will most likely just take care of everything for you. Python packages, on the other hand, will often have dependencies on specific versions of other packages, so if you pip install
one package, other package may fail to import because their dependent packages have been updated. That’s why we use virtual environments; my favorite method for creating and running virtual environments is with Anaconda, a Python distribution that comes with Sklearn, Scipy, NumPy, Jupyter notebook, and most of the other essential tools a data scientist needs when using Python.
Overall, I installed OpenCV cleanly in just a few steps:
- Install Anaconda, make Anaconda’s Python your system’s default Python (skip if you already have this).
- Create a virtual environment.
- Make sure all Conda packages are up-to-date.
- Run
conda install -c https://conda.binstar.org/menpo opencv
- Test.
(1) Install Anaconda. (Skip if you already have Anaconda).
First off, I’m still a python 2 guy. Yeah, there’s python 3, but I grew up on Py 2.7 and it’ll take a lot to pry it from my cold, dead hands. So I have a python 2.7 Anaconda environment running on my computer. Your choice.
I went to the Anaconda downloads page and got the Python 2.7 Mac OS X 64-Bit command-line installer, so that we can install everything from Terminal.
After downloading that, navigate to your Downloads directory (if you’re new to the Terminal, just open the Terminal application and type cd $HOME/Downloads
).
While still in Terminal, enter
Awesome, now you’ve downloaded and installed Anaconda.
(1.b) Make Anaconda your default python installation.
For data science, Anaconda rules. Ideally, when you’re in Terminal and you type python
, you’d like for the Anaconda python installation to be the default python that starts running instead of what comes installed by default on a typical Macbook. Why? Well, using Anaconda we can just import NumPy, import any Scikit Learn funciton, import Matplotlib, etc.
To see what I’m talking about, type this in Terminal:
If you get /usr/bin/python2.7
, you’re not using the Anaconda installation. To change this, you’ll need to change your bash_profile so that the default path to the python installation in the Anaconda directory. If you don’t have a .bash_profile file in your home directory, do this:
This just created that file. Next, open the .bash_profile page and add this line:
export PATH=”~/anaconda/bin:$PATH”
Finally, you have to make your system update python path the with your new settings, so in Terminal type
(2) Make an Anaconda virtual environment
Anaconda has great documentation if you ever get lost using their tools, but otherwise they’re pretty easy to use. To create a virtual python 2.7 environment called “py27,” run this:
To enter this virtual environment, we use Conda’s source activate
function:
If the environment is running properly, you should see (py27)
preceding the $
sign at the command prompt in Terminal. In this environment we have access to Anaconda’s python package installer, conda install
, so that we can install packages at will in this “bubble” without messing up dependencies (basically breaking python) in any other environment. Side note: if you want to exit this py27 environment, just enter source deactivate
in Terminal.
(3) Update packages
Just to be safe, I updated all of my python packages while inside of my py27 environment. It’s ridiculously easy with Anaconda:
(4) Install OpenCV
With Anconda we can install python packages within a specific Conda environment using conda install
instead of pip
, the typical python package management system.
Next, I would normally suggest just typing conda install opencv
at the command prompt, but this (unsurprisingly) lead me to a package conflict with NumPy! Yep, the version of OpenCV that Conda installed relied on a specific release of the NumPy package that was actually in conflict with the one that was just updated in step (3). OK, to be honest, maybe I brought that upon myself with updating the packages the way I did. But, there’s a work around that functions with this latest update of NumPy: install OpenCV directly from the Menpo project:
(5) Fire up a Jupyter notebook and test!
The Anaconda environment should now have everything we need to start analyzing images in a self-contained little Jupyter notebook. Test it out. First, launch a Jupyter notebook from the terminal:
Next, see if everything is installed correctly; hopefully you’ll be able to run this sans errors:
If successful, you’ll be able to readily access OpenCV functions with the package prefix cv2
!
Bob Savage <bobsavage@mac.com>
Python on a Macintosh running Mac OS X is in principle very similar to Python onany other Unix platform, but there are a number of additional features such asthe IDE and the Package Manager that are worth pointing out.
4.1. Getting and Installing MacPython¶
Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, youare invited to install the most recent version of Python 3 from the Pythonwebsite (https://www.python.org). A current “universal binary” build of Python,which runs natively on the Mac’s new Intel and legacy PPC CPU’s, is availablethere.
What you get after installing is a number of things:
A
Python3.9
folder in yourApplications
folder. In hereyou find IDLE, the development environment that is a standard part of officialPython distributions; and PythonLauncher, which handles double-clicking Pythonscripts from the Finder.A framework
/Library/Frameworks/Python.framework
, which includes thePython executable and libraries. The installer adds this location to your shellpath. To uninstall MacPython, you can simply remove these three things. Asymlink to the Python executable is placed in /usr/local/bin/.
The Apple-provided build of Python is installed in/System/Library/Frameworks/Python.framework
and /usr/bin/python
,respectively. You should never modify or delete these, as they areApple-controlled and are used by Apple- or third-party software. Remember thatif you choose to install a newer Python version from python.org, you will havetwo different but functional Python installations on your computer, so it willbe important that your paths and usages are consistent with what you want to do.
IDLE includes a help menu that allows you to access Python documentation. If youare completely new to Python you should start reading the tutorial introductionin that document.
If you are familiar with Python on other Unix platforms you should read thesection on running Python scripts from the Unix shell.
Opencv Python Mac Camera
4.1.1. How to run a Python script¶
Your best way to get started with Python on Mac OS X is through the IDLEintegrated development environment, see section The IDE and use the Help menuwhen the IDE is running.
If you want to run Python scripts from the Terminal window command line or fromthe Finder you first need an editor to create your script. Mac OS X comes with anumber of standard Unix command line editors, vim andemacs among them. If you want a more Mac-like editor,BBEdit or TextWrangler from Bare Bones Software (seehttp://www.barebones.com/products/bbedit/index.html) are good choices, as isTextMate (see https://macromates.com/). Other editors includeGvim (http://macvim-dev.github.io/macvim/) and Aquamacs(http://aquamacs.org/).
To run your script from the Terminal window you must make sure that/usr/local/bin
is in your shell search path.
To run your script from the Finder you have two options:
Drag it to PythonLauncher
Select PythonLauncher as the default application to open yourscript (or any .py script) through the finder Info window and double-click it.PythonLauncher has various preferences to control how your script islaunched. Option-dragging allows you to change these for one invocation, or useits Preferences menu to change things globally.
4.1.2. Running scripts with a GUI¶
With older versions of Python, there is one Mac OS X quirk that you need to beaware of: programs that talk to the Aqua window manager (in other words,anything that has a GUI) need to be run in a special way. Use pythonwinstead of python to start such scripts.
With Python 3.9, you can use either python or pythonw.
4.1.3. Configuration¶
Python on OS X honors all standard Unix environment variables such asPYTHONPATH
, but setting these variables for programs started from theFinder is non-standard as the Finder does not read your .profile
or.cshrc
at startup. You need to create a file~/.MacOSX/environment.plist
. See Apple’s Technical Document QA1067 fordetails.
For more information on installation Python packages in MacPython, see sectionInstalling Additional Python Packages.
4.2. The IDE¶
MacPython ships with the standard IDLE development environment. A goodintroduction to using IDLE can be found athttp://www.hashcollision.org/hkn/python/idle_intro/index.html.
4.3. Installing Additional Python Packages¶
There are several methods to install additional Python packages:
Packages can be installed via the standard Python distutils mode (
pythonsetup.pyinstall
).Many packages can also be installed via the setuptools extensionor pip wrapper, see https://pip.pypa.io/.
Install Opencv For Python 3.7 Mac
4.4. GUI Programming on the Mac¶
There are several options for building GUI applications on the Mac with Python.
PyObjC is a Python binding to Apple’s Objective-C/Cocoa framework, which isthe foundation of most modern Mac development. Information on PyObjC isavailable from https://pypi.org/project/pyobjc/.
The standard Python GUI toolkit is tkinter
, based on the cross-platformTk toolkit (https://www.tcl.tk). An Aqua-native version of Tk is bundled with OSX by Apple, and the latest version can be downloaded and installed fromhttps://www.activestate.com; it can also be built from source.
wxPython is another popular cross-platform GUI toolkit that runs natively onMac OS X. Packages and documentation are available from https://www.wxpython.org.
PyQt is another popular cross-platform GUI toolkit that runs natively on MacOS X. More information can be found athttps://riverbankcomputing.com/software/pyqt/intro.
4.5. Distributing Python Applications on the Mac¶
The standard tool for deploying standalone Python applications on the Mac ispy2app. More information on installing and using py2app can be foundat http://undefined.org/python/#py2app.
4.6. Other Resources¶
The MacPython mailing list is an excellent support resource for Python users anddevelopers on the Mac:
Another useful resource is the MacPython wiki: