After my macbook's sudden demise and spontaneous, inexplicable regeneration, I've decided to try and port some things I've worked on in GNU/Linux to OS X. Also, having a built-in microphone and camera on a portable computer is pretty amazing when you work primarily with audio and video and need to test things.
I was really happy with how my previous forays in
pygame had been going, so over the holidays I thought I 'd try and make a version of my
previous camLoops prototype, but for OS X.
Also, at
Alexandre Quessy's invitation, I've added
cam_loop.py to the
ToonLoop project. More on that in the coming weeks, so stay "tooned" (if not put off by that terrible pun).
*
UPDATE The initial opencv version of camLoops
is now available. I've only tested it on a MacBook running OS X so far, but it should also work in GNU/Linux.
I did some research online and there did not seem to be much in the way of camera- frame-grabbing modules for OS X with python bindings (granted it is a pretty niche area). The most complete are Apple's
QTKit, the Cocoa framework (which has python bindings) for Quicktime and
OpenCV, or Open Source Computer Vision, which is a cross-platform, BSD-licensed library written in C with Python bindings. Since I have no interest in wasting time using a Mac-only, proprietary framework like QTKit, the choice was pretty obvious. The installation, however, was not.
Given my heavy use of fink packages (when I should really just give in and roll GNU/Linux on my laptop), I am used to having to fight with cross-platform librairies/frameworks. I was even pleased to find an OS X specific
build instruction page on the OpenCV wiki. It's quite likely that my approach is not the best solution for building but it's what worked for me.
I decided for better or worse to get the current
production version of Python, which is 2.6.1 at the time of this writing. I grabbed the disk image and ran the installer (and breathed a sigh of relief). For pygame to work, I had to get
PyObjC, which is used to build Cocoa apps for OS X in Python. This is not required for OpenCV, so skip ahead if you're not using pygame. This also required an svn
checkout of the 1.4 branch of pyobjc which works on Tiger, as no slick disk-images with installers are available from the website at present. To grab the branch, do:
svn co http://svn.red-bean.com/pyobjc/branches/pyobjc-1.4-branch pyobjc
Fortunately, the PyObjC people know their target audience and all it took once in the pyobjc directory to make a nice installer was:
python setup.py bdist_mpkg --open
*
UPDATE Previously OpenCV was using CVS for version control, they have since migrated to Subversion. Check out a clean version with:
svn co https://opencvlibrary.svn.sourceforge.net/svnroot/opencvlibrary opencvlibrary
In the INSTALL file, it is suggested that you do autoreconf -i --force
This failed for me (even though my autotools are up to date) so i used the pre-existing configuration files and left autoconf alone.
In the opencv
directory, create a build
directory and enter it with mkdir build; cd build
From the
build
directory, run configure with a few flags set (replace
sw
with
/opt/local
if you are using darwinports instead of fink):
../configure CPPFLAGS="-I/sw/include" LDFLAGS="-L/sw/lib" --with-python
Now compile and install with
make && sudo make install
. You will be prompted for your password.
Edit your ~/.profile to include the following two lines, which will be different if you set the
--prefix
option on your configure script to something other than the default /usr:
LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH
PYTHONPATH=/usr/local/lib/python2.6/site-packages:${PYTHONPATH}
export PYTHONPATH
To test that everything worked, run your python interpreter and try
import opencv
. Provided that there is no errors such as 'no module opencv', you can start trying the python examples in the samples directory.
I ran
python facedetect.py 0
, where 0 is the index of the camera you want the program to use. The program then draws a red square around what it thinks is your face. Extra points for being beard proof, and hats off to
Mark Asbach for his work on OS X support for OpenCV.