X
    Categories: Python OpenCV Tutorials

World’s simplest Facial Recognition API for Python(Ubuntu Only)

Hey Guys,

Welcome Back, in this post, we are going to discover the easiest way to discover faces in any image using OpenCV.
The Api uses three main imports :

  1. Numpy
  2. scipy.misc
  3. dlib

It’s a ready made program using which, you can Recognize and manipulate faces from Python or from the command line with the world’s simplest face recognition library.

Built using dlib‘s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark.

This also provides a simple 

1
face_recognition

 command line tool that lets you do face recognition on a folder of images from the command line!

Features

Find faces in pictures

Find all the faces that appear in a picture:

 

Find and manipulate facial features in pictures

Get the locations and outlines of each person’s eyes, nose, mouth and chin.

 

Identify faces in pictures

Recognize who appears in each photo.

 

You can even use this library with other Python libraries to do real-time face recognition:

See this example for the code.

Installation

Requirements:

Install this module from pypi using 

1
pip3

 (or 

1
pip2

 for Python 2):

pip3 install face_recognition

IMPORTANT NOTE: It’s very likely that you will run into problems when pip tries to compile the 

1
dlib

 dependency. If that happens, check out this guide to installing dlib from source (instead of from pip) to fix the error:

How to install dlib from source

After manually installing 

1
dlib

, try running 

1
pip3 install face_recognition

 again to complete your installation.

If you are still having trouble installing this, you can also try out this pre-configured VM.

Usage

Command-Line Interface

When you install 

1
face_recognition

, you get a simple command-line program called 

1
face_recognition

 that you can use to recognize faces in a photograph or folder full for photographs.

First, you need to provide a folder with one picture of each person you already know. There should be one image file for each person with the files named according to who is in the picture:

Next, you need a second folder with the files you want to identify:

Then in you simply run the command 

1
face_recognition

, passing in the folder of known people and the folder (or single image) with unknown people and it tells you who is in each image:

$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/

/unknown_pictures/unknown.jpg,Barack Obama
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person

There’s one line in the output for each face. The data is comma-separated with the filename and the name of the person found.

An 

1
unknown_person

 is a face in the image that didn’t match anyone in your folder of known people.

Adjusting Tolerance / Sensitivity

If you are getting multiple matches for the same person, it might be that the people in your photos look very similar and a lower tolerance value is needed to make face comparisons more strict.

You can do that with the 

1
--tolerance

 parameter. The default tolerance value is 0.6 and lower numbers make face comparisons more strict:

$ face_recognition --tolerance 0.54 ./pictures_of_people_i_know/ ./unknown_pictures/

/unknown_pictures/unknown.jpg,Barack Obama
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person

If you want to see the face distance calculated for each match in order to adjust the tolerance setting, you can use 

1
--show-distance true

:

$ face_recognition --show-distance true ./pictures_of_people_i_know/ ./unknown_pictures/

/unknown_pictures/unknown.jpg,Barack Obama,0.378542298956785
/face_recognition_test/unknown_pictures/unknown.jpg,unknown_person,None
More Examples

If you simply want to know the names of the people in each photograph but don’t care about file names, you could do this:

$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2

Barack Obama
unknown_person
Speeding up Face Recognition

Face recognition can be done in parallel if you have a computer with multiple CPU cores. For example if your system has 4 CPU cores, you can process about 4 times as many images in the same amount of time by using all your CPU cores in parallel.

If you are using Python 3.4 or newer, pass in a 

1
--cpus <number_of_cpu_cores_to_use>

 parameter:

$ face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/

You can also pass in 

1
--cpus -1

 to use all CPU cores in your system.

Python Module

You can import the 

1
face_recognition

 module and then easily manipulate faces with just a couple of lines of code. It’s super easy!

API Docs: https://face-recognition.readthedocs.io.

Automatically find all the faces in an image
import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image)

# face_locations is now an array listing the co-ordinates of each face!

See this example to try it out.

Automatically locate the facial features of a person in an image
import face_recognition

image = face_recognition.load_image_file("my_picture.jpg")
face_landmarks_list = face_recognition.face_landmarks(image)

# face_landmarks_list is now an array with the locations of each facial feature in each face.
# face_landmarks_list[0]['left_eye'] would be the location and outline of the first person's left eye.

See this example to try it out.

Recognize faces in images and identify who they are
import face_recognition

picture_of_me = face_recognition.load_image_file("me.jpg")
my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]

# my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!

unknown_picture = face_recognition.load_image_file("unknown.jpg")
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]

# Now we can see the two face encodings are of the same person with `compare_faces`!

results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)

if results[0] == True:
    print("It's a picture of me!")
else:
    print("It's not a picture of me!")

See this example to try it out.

Python Code Examples

All the examples are available here.

How Face Recognition Works

If you want to learn how face location and recognition work instead of depending on a black box library, read my article.

Caveats

  • The face recognition model is trained on adults and does not work very well on children. It tends to mix up children quite easy using the default comparison threshold of 0.6.

 

Common Issues

Issue: 

1
Illegal instruction (core dumped)

 when using face_recognition or running examples.

Solution: 

1
dlib

 is compiled with SSE4 or AVX support, but your CPU is too old and doesn’t support that. You’ll need to recompile 

1
dlib

 after making the code change outlined here.

Issue: 

1
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

 when running the webcam examples.

Solution: Your webcam probably isn’t set up correctly with OpenCV. Look here for more.

Issue: 

1
MemoryError

 when running 

1
pip2 install face_recognition

Solution: The face_recognition_models file is too big for your available pip cache memory. Instead, try 

1
pip2 --no-cache-dir install face_recognition

 to avoid the issue.

Issue: 

1
AttributeError: 'module' object has no attribute 'face_recognition_model_v1'

Solution: The version of 

1
dlib

 you have installed is too old. You need version 19.4 or newer. Upgrade 

1
dlib

.

Issue: 

1
TypeError: imread() got an unexpected keyword argument 'mode'

Solution: The version of 

1
scipy

 you have installed is too old. You need version 0.17 or newer. Upgrade 

1
scipy

.

Thanks

  • Thank to Adam Geitgey @https://github.com/ageitgey for this awesome tutorial. All credits for this post goes to him. I won’t take even a penny!!!
  • Many, many thanks to Davis King (@nulhom) for creating dlib and for providing the trained facial feature detection and face encoding models used in this library. For more information on the ResNet that powers the face encodings, check out his blog post.
  • Thanks to everyone who works on all the awesome Python data science libraries like numpy, scipy, scikit-image, pillow, etc, etc that makes this kind of stuff so easy and fun in Python.
  • Thanks to Cookiecutter and the audreyr/cookiecutter-pypackage project template for making Python project packaging way more tolerable.
Simple Digit Recognition aka Optical Character Recognition(OCR) in OpenCV-Python
Installation of OpenCV 3.3/3.2/3.0 and Python 2.7 on Windows 10 (64-bit)
Editor :

View Comments (3)