This post should be useful to Mac users who want to get OpenCV up and running with Python. I’ve recently migrated to an older (2010 refurbished) MacBook Air (from a 2011 MacBook Pro) which runs OS X 10.6.8 (The Pro ran 10.7.something). I transferred all my installed software from the Pro to the Air using Apple’s Migration Assistant software. However, after this, XCode (4.something) stopped working. I wasn’t able to install new stuff using MacPorts, and so I decided to uninstall XCode, uninstall MacPorts, install HomeBrew (instead of MacPorts to install Unix tools Mac didn’t ship with OS X) & install an older version of XCode (3.2.2). Python 2.6 was already installed and working fine. Here are the steps:
- Uninstall MacPorts:
- sudo port -f uninstall installed
- sudo rm -rf /opt/local
sudo rm -rf /Applications/DarwinPorts
sudo rm -rf /Applications/MacPorts
sudo rm -rf /Library/LaunchDaemons/org.macports.*
sudo rm -rf /Library/Receipts/DarwinPorts*.pkg
sudo rm -rf /Library/Receipts/MacPorts*.pkg
sudo rm -rf /Library/StartupItems/DarwinPortsStartup
sudo rm -rf /Library/Tcl/darwinports1.0
sudo rm -rf /Library/Tcl/macports1.0
sudo rm -rf ~/.macports - Remove /opt/local/bin & /opt/local/sbin from your PATH variable. To do this, open a new terminal window and type: open .profile. This will open up the .profile file using TextEdit (assumed installed). Comment out the line (prefix with #) the line: export PATH=/opt/local/bin:/opt/local/sbin:$PATH
- Install HomeBrew: Open a terminal window and type: /usr/bin/ruby -e “$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)”
- Update HomeBrew: sudo brew update
- Uninstall XCode 4.0 and install XCode 3.2.2:
- Note: This step is not required if your XCode is running okay. You can diagnose this by: sudo brew doctor
- For example, I got the warning: Warning: You have no /usr/bin/cc.
This means you probably can’t build *anything*. You need to install the Command Line Tools for Xcode. - So, uninstall XCode by: sudo /Developer/Library/uninstall-devtools –mode=all
- Download XCode 3.2.2 (without the iOS SDK, it is 745 MB) and install it the regular way (you get a .dmg file)
- Now, typing gcc in the command line tells me that I have the following C/C++ compiler installed: i686-apple-darwin10-gcc-4.2.1
- Install OpenCV 2.2: sudo brew install opencv –build32 (Note: –build32 because mine was a 32 bit OS). OpenCV installs in/Library/Frameworks/OpenCV.framework
- Add Python Path: Open a new terminal and type open .profile and add this line: export PYTHONPATH=”/usr/local/lib/python2.6/site-packages:$PYTHONPATH”
- Run OpenCV in Python:
- Now, write the python program to do whatever you want. Import the OpenCV library by adding import cv up the top of your program.
- Now, to run a sample Load & Display Image program in python using the OpenCV library:
import cv im = cv.LoadImageM("image.jpg") cv.NamedWindow("loaded_image", 1) cv.ShowImage("loaded_image",im) cv.WaitKey(2000)
- Note that there seems to be some bug with OS X and OpenCV with closing GUI windows. So I’ve used the cvWaitKey command above to automatically close the window after 2 seconds.
- Sample face detection program (from http://recursive-design.com/blog/2010/12/14/face-detection-with-osx-and-python/ , remember to change the location of the Haar Cascade; mine was in /Library/Frameworks/OpenCV.framework/Versions/Current/Resources/) outputs: