Face Detection and Recognition in Python with OpenCV
Learn how to use the Python bindings for OpenCV to make a program that is able to detect faces and then recognize them.
You can find the code from this tutorial on github.
Note: This post won’t go into depth about what OpenCV is doing behind the scenes. The aim is to end up with working Python code. See the bottom of the post for links to more in-depth explanations (in C++).
You will need both Python (2.7.6) and OpenCV, as well as a webcam. If you already have these installed, skip to the next part.
Installing Python and OpenCV
For Windows and Mac OS X, I won’t write up how to install OpenCV. Instead, I’ll point you to other guides dedicated to the task:
The first part of the program is getting the input from your webcam.
Read through the comments before running the code to make sure you understand it.
Save this as facerec.py and run it from a command line using $ python path/to/file/facerec.py where path/to/file/ is the path to the folder containing the file. If you aren’t sure what it is, you can also drag the file in the command line.
The Python program simply loops capturing and showing an image. To exit the program, either press Exit or Ctrl-C . If you have multiple cameras, you might have to change the ‘0’ in line 4 to ‘1’ or ‘2’ (and so on).
At this point, the program isn’t doing much. It’s just receiving a video stream from the webcam and displaying it. The next step is getting it to detect the faces we want to identify.
For this part, you are going to need a cascade file for detecting faces. Download haarcascade_frontalface_default.xml (Right-Click > Save Link As) and place it in the same directory as facerec.py. Make sure you don’t change the file’s name, or the program won’t be able to find it.
Again, save it as facerec.py and run it.
If all goes well, it should be displaying the feed from the webcam, while drawing a rectangle around each face it sees.
If the program feels slow when you run it, you can increase the size = 1 value (e.g. to size = 2).
This will down-scale the image to speed-up the detection. Increase the value by one each time, until it is not so slow anymore.
Now, we want the program to be able recognize the faces it sees. To do this, we are first going to need some training data. I will later cover how to produce your own training set, but for now I’ll be using a face database compiled by AT&T. You can get it from here . Decompress the .zip file into a folder named att_faces (in the same directory as facerec.py).
Your working directory should now look like this:
| |—- s1
| | |—- 1.pgm
| | |—- 2.pgm
| | |—- 3.pgm
| | |—- 4.pgm
| | |—- 5.pgm
| | |—- 6.pgm
| | |—- 7.pgm
| | |—- 8.pgm
| | |—- 9.pgm
| | |—- 10.pgm
| |—- s2
| | |—- 1.pgm
| | |—- …
| |—- …
The next bit of code:
If you are using OpenCV2 (e.g. in Ubuntu instructions above), make the modification as noted.
Save the code as facerec.py and run it. Because we are using faces from a pre-made dataset, it won’t recognise you just yet. It will display it’s best guess, which at this point should be of the form s* . If you want the program not to show a name if the certainty is too low, you could put in an if-statement checking ‘prediction’:
You can change 500 to what you find works best. This code would replace the three lines below the comment ‘# ’.
Hopefully, the program is recognising faces and trying label them, but it still doesn’t know your name. To fix that, we want to generate our own training data, to replace the att_faces database.
If you want, you can delete the folders labelled s?, but keep the folder att_faces .
The second Python file, train.py, will add to the training set using images from the webcam. Make sure there is only one face in the shot.
Save this as train.py and run it using $ python path/to/file/train.py Name where path/to/file/ is the path to the folder containing the file, and Name is the name of the subject.
train.py will use the face detection code from above. It will store 20 images of the face it detects and stores it in att_faces under the name you gave it. Note: If you ever put this code online (eg onto Github), make sure you don’t also upload your images stored in att_faces.
You need a minimum of two different people in the training data. It can also get slow to start the program when you have trained it a lot. This won’t really affect the performance of the recognition itself.
Find out more information from the official OpenCV docs.