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
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.