I Know That Face
Programming Snapshot – Facial Recognition

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).
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.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
News
-
New Linux Ultrabook from TUXEDO Computers
TUXEDO Computers has released a new 15" Ultrabook running Linux.
-
GNOME 43 To Bring Some Exciting New Features
GNOME 43 is getting close to the first alpha development release and it promises to add one particular feature that should be exciting to several users.
-
KaOS 2022.06 Now Available With KDE Plasma 5.25
The newest iteration of KaOS Linux not only adds the latest KDE Plasma desktop but sets LibreOffice as the default.
-
Manjaro 21.3.0 Is Now Available
Manjaro “Ruah” has been released and includes the latest Calamares installer, GNOME 42, and much more.
-
SpiralLinux is a New Linux Distribution Focused on Simplicity
A new Linux distribution, from the creator of GeckoLinux, is a Debian-based operating system with a focus on simplicity and ease of use.
-
HP Dev One Linux Laptop is Now Available for Pre-Order
The System76/HP collaboration Dev One laptop, geared toward developers, is now available for pre-order.
-
NixOS 22.5 Is Now Available
The latest release of NixOS with a much-improved package manager and a user-friendly graphical installer.
-
System76 Teams up with HP to Create the Dev One Laptop
HP and System76 have come together to develop a new laptop, powered by Pop!_OS and aimed toward developers.
-
Titan Linux is a New KDE Linux Based on Debian Stable
Titan Linux is a new Debian-based Linux distribution that features the KDE Plasma desktop with a focus on usability and performance.
-
Danielle Foré Has an Update for elementary OS 7
Now that Ubuntu 22.04 has been released, the team behind elementary OS is preparing for the upcoming 7.0 release.