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.


  1. can you please help me! i have this error
    File "simple_face_recognition.py", line 58, in
    recognizer = cv2.face.createFisherFaceRecognizer()
    AttributeError: 'module' object has no attribute 'face'

    1. The methods names have been replaced in newer versions. The new methods are:

      recognizer = cv2.face.EigenFaceRecognizer_create()
      recognizer = cv2.face.FisherFaceRecognizer_create()
      recognizer = cv2.face.LBPHFaceRecognizer_create()

  2. Thank you for sharing, permission to learn the code

  3. If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you must watch this video
    right away...

    (VIDEO) Have your ex CRAWLING back to you...?

  4. Attention! The create methods have been replaced in a newer version.

    The current method names are:

    recognizer = cv2.face.EigenFaceRecognizer_create()
    recognizer = cv2.face.FisherFaceRecognizer_create()
    recognizer = cv2.face.LBPHFaceRecognizer_create()

  5. Print media is being challenged by its electronic counterpart. But there are several unique advantages that help both the mediums to create a niche place in the media industry.

    afp viewer online