Showing posts with label facerecognition. Show all posts
Showing posts with label facerecognition. Show all posts

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