I Know That Face

Programming Snapshot – Facial Recognition

Article from Issue 214/2018
Author(s):

It is not just Facebook – every Linux user can extract faces from photos and assign them to real people, thanks to free libraries. Mike Schilli shows you how to do it.

Facebook users already take it for granted that the social network recognizes people from their circle of friends by their faces on uploaded pictures. Some free libraries, which every Linux user can download from GitHub, also extract faces from photos and compare them with previously recognized ones, thus allowing the home user to recognize people (e.g., in their private vacation photo collection) and to mark the images accordingly.

Much goes on behind the scenes in automatic face recognition. First, an algorithm has to pick out a face-like object from the millions of pixels in a photo (Figure 1): Two round, slightly darker areas as the eyes; a protruding object in the middle as the nose; a horizontal line below it as the mouth; and another below it as the chin – that could be a face (Figure 2).

Figure 1: The author hiking in the Arizona desert.
Figure 2: The face-box.py script identified a face in the photo and framed it.

A good face recognition program not only recognizes full-screen faces on portrait photos, but also faces that only cover a few hundred pixels due to the subjects being further away or warped because they are looking into the camera from an angle.

Assured After Training

Anything that could be a face needs to be extracted from the background noise of the image. Neural networks [1] are excellent for this purpose. They do not act on fixed pixel values, which wouldn't work, because even two photos of the same person differ enormously at the pixel level. Instead, the network is trained with millions of different faces during the learning phase and then recognizes everything that looks remotely similar with a small remaining margin of error.

Installation Made Easy

Hosted on GitHub, Adam Geitgey's face_recognition project [2] uses the popular dlib library to recognize faces in photos and videos. With

git clone https://github.com/ageitgey/face_recognition.git

This git command creates a clone of the GitHub repository in a local directory. The docker file in the top directory handles the installation of the dependent projects. The command

docker build -t face .

in the project directory loads everything off the web, compiles dlib, and also loads a 100MB-heavy neural model that is already trained about faces into the container. A coffee break would be a good thing while this is going.

To call the script in Listing 1 for face recognition in the container, the user copies it to the examples directory of the GitHub repository's clone and calls

docker run -v `pwd`:/build -it face bash -c "cd /build; python3 face-box.py pic.jpg"

Listing 1

face-box.py

01 #!/usr/bin/python3
02 import face_recognition as fr
03 import sys
04 from PIL import Image, ImageDraw
05
06 try:
07   _, img_name = sys.argv
08 except:
09   raise SystemExit(
10      "usage: " + sys.argv[0] + " image")
11
12 img   = fr.load_image_file(img_name)
13 faces = fr.face_locations(img)
14
15 pil = Image.fromarray(img)
16 pil = pil.convert("RGBA")
17
18 tmp  = Image.new(
19         'RGBA', pil.size, (0,0,0,0))
20 draw = ImageDraw.Draw(tmp)
21
22 for (y0, x1, y1, x0) in faces:
23   draw.rectangle(((x0, y0), (x1, y1)),
24           fill=(30, 0, 0, 200))
25 del draw
26 pil = Image.alpha_composite(pil, tmp)
27 pil = pil.convert("RGB")
28
29 img_name = img_name.replace(".", "-box.")
30 pil.save(img_name)

This binds the current directory (where both the Python script and the test image are located) to the /build directory in the container. The Bash command changes to that directory and calls face-box.py in the container where the face recognition tool is installed. The Bash command passes in the image name pic.jpg as a parameter to face-box.py; the photo shows yours truly in the hellishly hot desert of Arizona (Figure 1). The script picks out the face under the baseball cap, marks it by darkening the area, and then generates the file pic-box.jpg with the resulting data. Since the current directory is bound to the container, the box file also resides right there, even outside of the container directory after the docker command has been completed.

Listing 1 [3] is quite lean, with only 30 lines, underlining the project's claim to being "The world's simplest facial recognition" [2]. The actual face recognition is executed using face_locations() (line 13) against a JPEG image loaded with load_image_file(). Back comes a list with rectangular coordinates of recognized faces, because the algorithm does not only search for one face in images with several persons, but finds all in one go. The for loop in line 22 iterates across all blocks of four of these coordinates and paints a semitransparent dark rectangle on the face coordinates using the ImageDraw class from the Python Imaging Library (PIL) treasure trove.

Not Opaque

For transparency in the dark overlay, the image needs a temporary canvas in tmp with an alpha channel (image mode RGBA) with a relatively high transparency value (200 of 255 in line 24). The fill parameter for the color to be used is set to light red (30,0,0). However, PIL cannot turn an image with an alpha channel into a JPEG, so line 27 turns it into an RGB image without an alpha channel before pil.save() stores the JPEG under a name with a -box extension. The procedure works relatively reliably, apart from a few blatant outliers, as shown in Figure 3 of a stalactite cave whose stalactites (Figure 4) the neural network thinks represent my facial features.

Figure 3: The artificial intelligence module thinks it has identified the author here …
Figure 4: … but it's seriously mistaken.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Howdy

    Howdy brings the convenience of facial authentication to Linux.

  • Machine Learning Smarts for Shutterbugs

    PhotoPrism offers a combination of a polished, user-friendly interface and an artificial intelligence engine that makes organizing, searching, and sharing photos a breeze.

  • digiKam 5

    The freshly released digiKam 5 boasts a number of new features, brings many improvements, and ditches some legacy ballast.

  • Clever Sampling

    Does the private photo archive on your computer just keep on growing without ever seeing any attention? Mike Schilli whips up a home-grown solution to get rid of bad photos with the Electron framework.

  • Geeqie

    The image viewer Geeqie is used to view and sort image collections. The tool supports numerous formats, reads metadata, and – among other things – displays the location where you took the picture on a map.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News