February 22, 2017

Face recognition example

In this post I will show you a very simple example of how to recognize faces inside an image using Python 3 and OpenCV library.

First of all you have to install the prerequisite software as described in this post. Keep in mind that you need to install OpenCV with contrib module to use face recognition APIs otherwise you will get an error like this: AttributeError: module 'cv2.cv2' has no attribute 'face'

Download the PyFaceRec repository in a directory of your choice. In the examples we assume you have downloaded it into C:\PyFaceRec.

The example described here is located in the SimpleFaceRecognition folder and attempt to identify faces of the members of the famous pop group Abba.

The faces to be detected are located into the data sub-directory.

While the faces database used to train the algorithm is located in the facesdb sub-directory.

Note that all the images have a resolution of 200x200 pixels. This is required by the algorithm.

Now lets try to run the example. Open a command Prompt, move into he SimpleFaceDetection folder an run the sample script as follows.

cd C:\PyFaceRec\SimpleFaceRecognition
python simple_face_recognition.py

You should get an output like this.

After having loaded the images from the training database, the program shows you the best match found for each face in the data folder. You can see that each face has been correctly identified.

The OpenCV library provides different algorithms for face recognition. Try yourself to change algorithm used by un-commenting a different line in the simple_face_recognition.py script.

recognizer = cv2.face.createFisherFaceRecognizer()
#recognizer = cv2.face.createEigenFaceRecognizer()
#recognizer = cv2.face.createLBPHFaceRecognizer()

You will see that other algorithms provide non-optimal results. This is not always true and really depends on the data you are working with.

Another important thing is that for a correct face detection both the face to be recognized and the faces used for training should have similar lighting conditions. This simple rule is clearly not true in our sample. That's why the distance is so high denoting a low accuracy of the detection.

No comments:

Post a Comment