Face Detection and Recognition
Face recognition works in two distinct steps: first locating the face in an image (detection), then mapping its unique geometry to identify who it belongs to (recognition).
Why Does This Exist?
Identifying individuals from their facial features is one of the most natural human capabilities, but teaching a computer to do it reliably requires solving two very different problems. First, the computer must look at a complex scene—with varying lighting, angles, and occlusions—and answer "Are there any faces here?" Second, given a cropped face, it must answer "Whose face is this?" Modern biometrics, security systems, and photo organization tools rely on decoupling these two tasks, solving each with specialized architectures.
Think of It Like This
The bouncer and the ID checker
Imagine a busy nightclub entrance. The bouncer (the detector) scans the crowd to find people trying to enter. They don't care who the person is, just that there is a person standing at the door. Once someone is isolated from the crowd, they are passed to the ID checker (the recognizer). The ID checker carefully examines the unique details of the person's face and matches them against an authorized guest list. The bouncer finds the faces; the ID checker names them.
How It Actually Works
The pipeline consists of two distinct stages:
1. Face Detection (Localization)
The goal is to output bounding boxes for all faces in an image. Historically, this was done using Haar Cascades, which looked for simple light/dark contrast patterns (e.g., eyes are darker than the forehead). Today, deep learning architectures like MTCNN (Multi-task Cascaded Convolutional Networks) or RetinaFace are used. They often output not just a bounding box, but also facial landmarks (the centers of the eyes, tip of the nose, and corners of the mouth).
2. Alignment
Because faces appear at different angles, the system uses the detected landmarks to warp and rotate the cropped face so the eyes and mouth are in standard, fixed positions. This "alignment" step drastically reduces the variability the recognition model has to handle.
3. Face Recognition (Embedding and Matching)
The aligned face is passed through a deep neural network (such as FaceNet or ArcFace). Instead of outputting a class label (like "Person A" or "Person B"), the network outputs a high-dimensional feature vector (an embedding).
- The network is trained using contrastive loss or triplet loss, which forces the embeddings of the same person to be close together in the vector space, and embeddings of different people to be far apart.
- To recognize the face, the system computes the cosine distance between the generated embedding and a database of known embeddings. If the distance is below a certain threshold, it's a match.
Code
import numpy as np
def cosine_similarity(vec1, vec2): """Calculates the similarity between two embeddings (vectors).""" dot_product = np.dot(vec1, vec2) norm1 = np.linalg.norm(vec1) norm2 = np.linalg.norm(vec2) return dot_product / (norm1 * norm2)
# Simulated embeddings from a recognition model (e.g., FaceNet)# In reality, these are 128D or 512D vectorsdatabase = { "Alice": np.array([0.8, 0.1, 0.5, -0.2]), "Bob": np.array([-0.5, 0.9, 0.1, 0.8])}
# A new face is detected, aligned, and passed through the modelunknown_face = np.array([0.78, 0.15, 0.48, -0.15])
# -> Match against databasethreshold = 0.85best_match = "Unknown"highest_sim = -1.0
for name, known_vec in database.items(): sim = cosine_similarity(unknown_face, known_vec) if sim > highest_sim: highest_sim = sim if sim > threshold: best_match = name
print(f"Recognized: {best_match} (Similarity: {highest_sim:.2f})")# -> Recognized: Alice (Similarity: 0.99)Watch Out For
Relying on classification instead of embeddings
A common mistake when building a face recognition system is training a standard classifier (using Softmax) where each class is a person in your dataset. If you do this, adding a new person requires retraining the entire model. By training the network to output an embedding (metric learning) instead, you can enroll new people instantly just by saving their feature vector to the database.
The Quick Version
- Face recognition is a two-step pipeline: detection (finding the face) followed by recognition (identifying it).
- Detectors output bounding boxes and facial landmarks.
- Alignment uses landmarks to warp faces into a standardized crop, reducing variations in pose.
- Recognizers use neural networks (like FaceNet) to convert the aligned face into a dense numerical vector (embedding).
- Identity is verified by measuring the geometric distance (often cosine similarity) between two embeddings.