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.

February 21, 2017

Face detection example

In this post I will show you a very simple example of how to detect 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.

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 SimpleFaceDetection folder and automatically identifies faces in a picture of the famous pop group Abba.
Open a command Prompt, move into the SimpleFaceDetection folder an run the sample script as follows.

cd C:\PyFaceRec\SimpleFaceDetection
python simple_face_detection.py

You should get an output like this.



The source is quite self-explanatory.

# Import the OpenCV library
import cv2

# Load the sample image
image = cv2.imread('data/abba.jpg')

# Load the OpenCV classifier to detect faces
faceCascade = cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    image,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(50, 50)
)

# The faces variable now contains an array of Nx4 elements
# where N is the number faces detected

print("Found", len(faces), "faces")

# Draw a rectangle around each face
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the image
cv2.imshow("Faces found", image)

# Wait for any key before exiting
cv2.waitKey(0)


February 20, 2017

Face detection and recognition with Python and OpenCV

This page is collecting a set of experiments on face detection and recognition using Python 3 and OpenCV library. The proposed examples have an increasing complexity to help you understand how this works.
The examples are based on Windows and Raspberry PI.

First of all we need to make a clear distinction between face detection and face recognition. These two terms are too often misused in the various articles I have found.
Face Detection detects the faces in the image. The output will be one ore more rectangles on the faces detected in the image.
Face Recognition identifies the face in input using a faces database. A single face should be given as input, and the output will be the name or class of the image.

The PyFaceRec repository is shared on GitHub with all the described examples ready to be used and modified.

Here is the full set of posts on this topic.
  1. Install Python 3 and OpenCV library on Windows
  2. Install Python 3 and OpenCV library on RaspberryPI (ToDo)
  3. Face detection example
  4. Face recognition example



February 19, 2017

Install Python 3 and OpenCV on Windows

In this post I will describe the easiest way of installing Python 3 and OpenCV library on Windows for face detection and recognition.

Install Python 3

Go to the Python download page and download the latest version of Python 3 for Windows. This example has been tested with Python 3.6.

Launch the installer and ensure the 'Add Python to PATH' checkbox is selected.
Wait for the installation to complete, open a Command Prompt and type 'python'. You should see something like this.

C:\Python>python
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Type 'quit()' to exit the python shell.


Install OpenCV library

If you just need the face detection (no recognition) all you need is the basic OpenCV library. It can be easily installed with no hassle with pip command.

pip install opencv-python


Install OpenCV library with contrib module

To be able to use the face recognition algorithms, you will need the 'contrib' module that is not included in the standard OpenCV build. Many people suggest to build the library manually to solve the problem but there is a simpler solution documented in this post.

Download the appropriate opencv+contrib .whl file from this page. The file to download should be: opencv_python-3.2.0+contrib-cpXX-cp36m-winYYY.whl. Where XX is your Python version and YYY is your Windows 32/64 bit flavor.
For example, assuming you have Python 3.6 running on Windows 64 bit you must download: opencv_python-3.2.0+contrib-cp36-cp36m-win_amd64.whl

Now open a Command Prompt with admin rights and type the following command to install:

pip install opencv_python-3.2.0+contrib-cp36-cp36m-win_amd64.whl


Test face detection

You can now move on to the face detection example.