Oracle AI & Data Science Blog
Learn AI, ML, and data science best practices

How to Build a Face Recognition Application with 4 Lines of Code

In this article, you will learn how to build a simple face recognition application. A facial recognition system is an application capable of identifying people from images or videos. This can be done by comparing facial features of the image with a faces database.

But luckily there's a Face Recognition Python API with everything already done for you. The API uses 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.

So here we go! Face recognition with four lines of code:

Code Example

import face_recognition

obama_image = face_recognition.load_image_file("barack_obama.jpg")

obama_encoding = face_recognition.face_encodings(obama_image)[0]

results = face_recognition.compare_faces([obama_encoding])

API Features

Face Detection:

  • Find faces in a photograph: Find all the faces that appear in a picture

  • Find faces in a photograph using deep learning

  • Find faces in batches of images w/ GPU using deep learning

Facial Features:

  • Identify as well as manipulate specific facial features in a photograph: Get the locations and outlines of each person's eyes, nose, mouth and chin.

  • Apply  digital make-up.'

Facial Recognition:

  • Find and recognize unknown faces in a photograph based on photographs of known people: Recognize who appears in each photo.

  • Instead of only matching with True/False one can now Compare faces by numeric face distance

  • Recognize faces in live video using your webcam - Simple / Slower Version (Requires OpenCV to be installed)

  • Recognize faces in live video using your webcam - Faster Version (Requires OpenCV to be installed)

  • Recognize faces in a video file and write out a new video file (Requirement of OpenCV to be installed as well)

  • Recognition of faces on a Raspberry Pi w/ camera

  • Run a web service to recognize faces via HTTP (Requires Flask to be installed)

  • Recognize faces with a K-nearest neighbours classifier


  • Python 3.3+ or Python 2.7

I create a code that gets all pictures from one folder and uses the names to label the person recognized by the feeds of the camera.

//path for the pictures folder

folder = '/home/user/code_examples/pictures'

entries = os.listdir(folder)

for entry in entries:

image = face_recognition.load_image_file(folder+'/'+entry)

face_encoding = face_recognition.face_encodings(image)[0]



You can have a look at my GitHub for the full example.

You can try this Docker composer.

Run the Docker image locally by running: docker-compose up --build

Linux users with a GPU (drivers >= 384.81) and Nvidia-Docker installed can run the example on the GPU: Open the docker-compose.yml file and uncomment the dockerfile: Dockerfile.gpu and runtime: nvidia lines.

The face_recognition library is widely known around the web for being the world's simplest facial recognition API for Python, and the project is totally open source.

How to Install in Ubuntu 16.04

# Update repo

sudo apt-get update

# Install cmake if it's not installed

sudo apt-get install build-essential cmake

# Clone the dlib library in some directory of the system

git clone https://github.com/davisking/dlib.git

# get into the cloned directory

cd dlib

# create build directory inside the cloned directory

mkdir build

# Switch to the created directory

cd build

# generate a Makefile in the current directory

cmake ..

# Build dlib !

cmake --build .

cd ..

# Install the Python 3 setup tools with the following command

sudo apt-get install python3-setuptools

python3 setup.py install

# If you don't have pip3 installed, install it with the following command

sudo apt-get -y install python3-pip

# The installation will take a while to download and install

pip3 install face_recognition

Full Example

The idea here is to compare the face encoding generated by the images that you have in your "folder" with the encoding generated by the image that you want to identify.

import face_recognition

# Load the jpg files into numpy arrays

obama_image = face_recognition.load_image_file("Barack Obama.jpg")

# Get the face encodings for each face in each image file

# Since there could be more than one face in each image, it returns a list of encodings.

# here I know that my image has only one face, so I grab index 0.


obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

except IndexError:

print("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting...")


known_faces = [



# results is an array of True/False telling if the unknown face matched anyone in the known_faces array

results = face_recognition.compare_faces(known_faces, unknown_face_encoding)

print("Is the unknown face a picture of Obama? {}".format(results[0]))

The face detection allows you to identify the location of faces inside an image. The library will return the coordinates of the images in the image, so you can use those coordinates to draw squares in the images. Exactly like the face recognition tool, the command will print the path of the image in the provided directory followed by the coordinates as a suffix, for example:

import face_recognition

image = face_recognition.load_image_file("obama.jpg")

face_locations = face_recognition.face_locations(image)


# [(98, 469, 284, 283)]

You can combine this with other Python libs as well.

For example, face recognition on live video from your webcam.

import cv2

# Get a reference to webcam #0 (the default one)

video_capture = cv2.VideoCapture(0)

Machine Learning like magic!

Now you can try to create your own face recognition example.

API GitHub

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.