Pages

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.

7 comments:

  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'

    ReplyDelete
    Replies
    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()

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

    ReplyDelete
  3. 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()

    ReplyDelete
  4. 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

    ReplyDelete
  5. Did you realize there's a 12 word phrase you can communicate to your man... that will trigger intense feelings of love and impulsive attraction for you buried inside his chest?

    Because hidden in these 12 words is a "secret signal" that triggers a man's impulse to love, worship and look after you with all his heart...

    12 Words That Fuel A Man's Desire Impulse

    This impulse is so hardwired into a man's genetics that it will drive him to work harder than ever before to to be the best lover he can be.

    In fact, triggering this dominant impulse is so essential to having the best ever relationship with your man that the moment you send your man a "Secret Signal"...

    ...You will instantly notice him expose his mind and heart for you in such a way he's never experienced before and he'll see you as the one and only woman in the galaxy who has ever truly fascinated him.

    ReplyDelete

Note: Only a member of this blog may post a comment.