Part 2 of 7 · Face Recognition App

Standing on the Shoulders of Giants

The reason a solo self-taught builder can make a working face recognition system in this decade is one open source library, named exactly what it does, face_recognition. It wraps decades of computer vision research, the dlib toolkit underneath, into functions a Python programmer can actually use, and this post is that choice, the installation reality nobody warns about, and the loading code that turns a folder of photos into a recognition set.

The library decision echoes the carousel’s Swiper argument at much higher stakes. Face recognition is not a weekend of clever code, it is trained models, facial landmark detection, and deep metric learning, research-grade work that dlib’s authors spent years on. The face_recognition library exposes it as a handful of honest functions, load an image, find the faces, encode them, compare encodings, and my project’s originality budget went where it belongs, the cameras, the logging, the interface, the packaging, not reimplementing computer vision badly.

The installation is the first real wall, and it deserves plain warning. The library sits on dlib, which compiles native code on install, so pip install face_recognition can mean a working C++ build environment, on Windows especially this stops people before they start:

# the honest install order that saves the most pain
# pip install cmake
# pip install dlib          # the slow, compile-heavy step; prebuilt wheels help
# pip install face_recognition opencv-python

On Linux and Raspberry Pi the compilers are usually present and it grinds through, on Windows a prebuilt dlib wheel matching your Python version is the sanity-preserving path. Budget an evening for the environment, it is the project’s real entry fee.

With the library installed, the known-faces set is a folder of photos, one clear face per image, filename as the person’s name, and loading it is the module’s first real code:

import face_recognition, os

def load_known_faces(folder):
    known = {}
    for filename in os.listdir(folder):
        name, ext = os.path.splitext(filename)
        if ext.lower() not in ('.jpg', '.jpeg', '.png'):
            continue
        image = face_recognition.load_image_file(os.path.join(folder, filename))
        encodings = face_recognition.face_encodings(image)
        if encodings:
            known[name] = encodings[0]   # one face expected per photo
    return known

Two defensive details already matter, the extension check keeps stray files out, and the if encodings guard handles photos where no face was found, a blurry or angled image simply contributes nothing rather than crashing the load. What exactly those encodings are, the 128 numbers that make comparison possible, is the next post, because understanding them is understanding the whole system.

A few things people ask me about this

Why does pip install face_recognition fail on my machine? The dlib dependency compiling. Install cmake first, use a prebuilt dlib wheel for your exact Python version on Windows, and expect the dlib step to be slow everywhere.

What makes a good known-face photo? One clear, front-facing, well-lit face per image. The loader takes the first face found, group photos and profiles degrade the encoding, and one good photo beats five poor ones.

Next

Recognition works by turning every face into 128 numbers and measuring distances between them. How that actually works, with the real matching code, is the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *