This face-boxer.py script is more-or-less the same code that you'll find in the OpenCV tutorial: Face Detection using Haar Cascades. For another variation, with more explanation, check out RealPython's tutorial.
The face-boxer.py
script is designed to be run from the command-line. It has two required arugments:
- The path to a XML file containing a Haar-cascade of visual features. In this example, it will be the features that make up a face.
- The path to an image file that you want to perform face-detection on. You can pass in more than one image file as space-separated arguments.
The face-boxer.py
script will then read each image file and perform this routine: For every detected object in a given image, the object is highlighted in a light-blue box, and this altered image is saved to:
/path/to/the-original-image--faces.jpg
If you are a Stanford student taking CompCiv, you should be able to copy the face-boxer.py
script and follow the instructions without a problem on corn.stanford.edu. The script has been tested on Python 2.7.8 and OpenCV 2.4x
You'll need the XML file that contains the data necessary for OpenCV to do its work. The file below contains the pattern data for frontal-aspects of a face:
curl -so haarcascade_frontalface_default.xml http://stash.compciv.org/opencv/haarcascades/haarcascade_frontalface_default.xml
If detecting faces is boring to you, you can download a zipped archive of all the Haar-cascade files from here, which is simply a mirror of what's in the OpenCV repo. Object patterns include: eyes, eyes with glasses, full (human) bodies, lower bodies, license plates, smiles, and cat faces.
# Download some images
curl -so obama-phone.jpg https://farm9.staticflickr.com/8604/15891607122_794b16aff7_z_d.jpg
curl -so obama-3d.jpg https://farm5.staticflickr.com/4054/4291192697_2ba403a502_b_d.jpg
python face-boxer.py haarcascade_frontalface_default.xml obama-phone.jpg obama-3d.jpg
(needs some tweaking obviously)
If you're interested in seeing the face-detection code work via your own webcam, check out this RealPython tutorial. Using your own webcam means you have to have Python and OpenCV installed on your own computer -- Mac users, check out this tutorial
This code is not working.