Automatic, face detection and recognition software is very cool
technology. However, it can also be a Big Brother-style surveillance nightmare if turned on CCTV cameras 24/7 or a recurring annoyance whenever
someone on Facebook tags you in photographs you didn’t even know
existed. Such momentous issues are outside the scope of this column, of
course. I have other reasons to talk about face detection here. One is
that it’s a prerequisite for my next post, but I’m going to keep that topic a secret to motivate you to come back! Another, as you’re about to read, is that face detection with free software is easy and
fun to try at home.
Let’s play with facedetect
Facedetect is a small Python command line utility that uses the Open Source Computer Vision Library (OpenCV) to recognize which regions of a digital
photograph correspond to human faces. Its installation is easy. There are a couple of little issues, but don’t let them discourage you — instead, learn through
them how to cope with the quirks of different GNU/Linux distributions.
To make facedetect available to all users of your Linux system, download
the zip archive from its home page, unpack it, and copy the facedetect Python file into the /usr/local/bin directory.
In order to work,
facedetect needs both the OpenCV data and software and its Python
bindings, which is the software interfaces that make OpenCV accessible
from Python scripts. The facedetect README file explicitly says that those pieces of
software are called opencv-data or libopencv-dev and, respectively,
python-opencv. As is, however, this information is only valid for
Debian, Ubuntu, and their derivatives.
I conducted a search, as I mentioned in my post about how to compile from sources on Linux, and I found that those two packages also exist in the standard online
repositories for Fedora, but they’re called opencv and opencv-python. Unfortunately, after I installed them and tried to run facedetect, I received this error:
[marco@polaris ~]$ facedetect testpicture.jpg
facedetect: error: cannot load HAAR_FRONTALFACE_ALT2 from /usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml
The problem here is that the package for Fedora doesn’t just have a
different name than what’s mentioned in the facedetect documentation — it
also puts its files in /usr/share/OpenCV/ instead of /usr/share/opencv,
which is where facedetect expects them to be. This is annoying but very easy
to fix. If you have the same problem, open the facedetect script with a
text editor and replace opencv with OpenCV in this line:
“ ‘HAAR_FRONTALFACE_ALT2’: ‘/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml’“
After this edit, I ran facedetect again on a test picture, and everything worked! I received this output:
[marco@polaris ~]$ facedetect testpicture.jpg
33 119 109 109
320 118 120 120
The output signified that facedetect found two faces in that picture. The first
two numbers of each line are the coordinates (in pixels) of the top left
corner of a “face area,” and the last two numbers are its width and height.
look closely at Figure A. The numbers at its top are the dimensions
and top-left corner coordinates of the area selected with the mouse.
Those numbers are in a different order and not exactly identical to the
facedetect output, because I drew the rectangle by hand. However, if
you compare the two sets of numbers, you’ll immediately see that
facedetect was right to say that there was a signal in that area.
Here you can see the facedetect coordinates.
If you aren’t convinced yet, take a look at Figure B, which is the picture generated by facedetect itself with the
[marco@polaris ~]$ facedetect -o testboxes.jpg testpicture.jpg
That option means “make a copy of my picture, then draw
inside it, all by yourself, a square around each face you see.”
Picture generated by facedetect.
What can you do with facedetect?
The coolest part of facedetect is that it’s made to communicate the positions of all the faces it finds to other
programs. The two most obvious and useful applications of these
properties are the two small scripts inside the facedetect README file. I
won’t copy them here because I want you to download facedetect for yourself and play with them! However, I will explain the two options that make those scripts work.
The first script puts all the pictures that contain at least one face in a folder and all the others in another, and it’s built around the -q option of facedetect. For example, this command:
facedetect -q somefile.jpg
returns 0 if somefile.jpg contains at least one face, and it returns 2 if there is none.
You may also use facedetect in this way:
NUMBER_OF_FACES=`facedetect $CURRENT_PICTURE | wc -l`
This sorts all the pictures in a folder by the number of faces they
contain, and it puts all and only those with at least X faces in a separate
folder called “group_pictures.”
The other script provided by facedetect finds and blurs all the faces in a set of pictures. This may be very useful when you
want to document some event with photographs you took on site, but you
also need to protect the privacy of the participants. The script first
uses facedetect, as I already demonstrated, to find the coordinates of
each “face region.” Then it applies the -scale effect of the mogrify program to those regions, which blurs them (Figure C).
You can blur faces with facedetect.
Be sure to check back to learn about another
application of facedetect in my next post. If you have experience with facedetect, please feel free to share your tips and tricks in the discussion thread below.