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
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.
News
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4
-
Fedora Unleashes Atomic Desktops
Fedora has combined its solid distribution with rpm-ostree system to make it possible to deliver a new family of Fedora spins, called Fedora Atomic Desktops.
-
Bootloader Vulnerability Affects Nearly All Linux Distributions
The developers of shim have released a version to fix numerous security flaws, including one that could enable remote control execution of malicious code under certain circumstances.