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
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
News
-
Mageia 9 Beta 2 is Ready for Testing
The latest beta of the popular Mageia distribution now includes the latest kernel and plenty of updated applications.
-
KDE Plasma 6 Looks to Bring Basic HDR Support
The KWin piece of KDE Plasma now has HDR support and color management geared for the 6.0 release.
-
Bodhi Linux 7.0 Beta Ready for Testing
The latest iteration of the Bohdi Linux distribution is now available for those who want to experience what's in store and for testing purposes.
-
Changes Coming to Ubuntu PPA Usage
The way you manage Personal Package Archives will be changing with the release of Ubuntu 23.10.
-
AlmaLinux 9.2 Now Available for Download
AlmaLinux has been released and provides a free alternative to upstream Red Hat Enterprise Linux.
-
An Immutable Version of Fedora Is Under Consideration
For anyone who's a fan of using immutable versions of Linux, the Fedora team is currently considering adding a new spin called Fedora Onyx.
-
New Release of Br OS Includes ChatGPT Integration
Br OS 23.04 is now available and is geared specifically toward web content creation.
-
Command-Line Only Peropesis 2.1 Available Now
The latest iteration of Peropesis has been released with plenty of updates and introduces new software development tools.
-
TUXEDO Computers Announces InfinityBook Pro 14
With the new generation of their popular InfinityBook Pro 14, TUXEDO upgrades its ultra-mobile, powerful business laptop with some impressive specs.
-
Linux Kernel 6.3 Release Includes Interesting Features
Although it's not a Long Term Release candidate, Linux 6.3 includes features that will benefit end users.