How to turn every face into a list of numbers (Face recognition, version 2)?

Howard Pong Yuen Lam · AI Notes · November 13, 2026 · 4 min read

Version 1 could show the photos, but it knew nothing about the faces in them. Version 2 does the measurement. For each photo it finds the face, describes that face as a list of 128 numbers, and keeps the numbers in one list and the person's name in a second list. Nothing is compared yet. This is the step that makes comparison possible.

The numbers are called a face encoding. They are not a description anyone can read — not "brown eyes, wide nose" — but a set of measurements in a form the computer can subtract from another set of measurements. Two photos of the same person give two similar lists. Two different people give two lists that are further apart.

Why numbers at all?

A computer cannot compare two faces as pictures. Two photos of the same face are never identical: the light changes, the head turns a little, the camera is at a different height. Comparing pixel by pixel would say every pair is different. Comparing 128 measurements that describe the shape of the face survives those changes, and gives a single number for how different two faces are. That single number is what version 3 uses to decide who is who.

How the program works

1. Add the face library

import face_recognition
import cv2
import os
import glob

face_recognition is the library that finds faces and turns each one into numbers. The other three lines are the same as version 1: cv2 reads the image, os handles file paths, and glob finds the files.

2. Find the photos

images_folder = "images"
images_path = glob.glob(os.path.join(images_folder, "*.*"))
print("{} images in folder".format(len(images_path)))

Unchanged from version 1. The printed count is still the quickest way to see that the folder was found.

3. Make two empty lists

known_face_encodings = []
known_face_names = []

One list will hold the 128 numbers for each person, the other will hold the matching names. The order matters: whatever goes into position 0 of the first list must belong to the name in position 0 of the second list. The program keeps that promise by adding to both lists inside the same loop, one after the other.

4. Read the image and fix the colour order

for img_path in images_path:
    img = cv2.imread(img_path)
    rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    basename = os.path.basename(img_path)
    (filename, ext) = os.path.splitext(basename)

cv2.imread gives a grid of pixels in BGR order: blue, green, then red. That is OpenCV's habit, not the usual order. face_recognition expects RGB: red, green, then blue. cv2.cvtColor with cv2.COLOR_BGR2RGB swaps the first and last channel and stores the result in rgb_img.

Skipping this line does not crash the program. It measures the wrong colours and quietly produces worse encodings — the kind of fault that only shows up in version 3 as a face that is not recognised. So the line is not optional.

The last two lines are again from version 1: basename drops the folder and splitext separates Howard from .JPG.

5. Measure the face

    img_encoding = face_recognition.face_encodings(rgb_img)[0]

face_encodings finds every face in the picture and returns one list of 128 numbers per face. The [0] at the end takes the first one.

That is right for a folder of passport-style photos, where there is exactly one clear face. It also means the program expects at least one face: if face_encodings finds none, the list is empty and [0] stops the program with an error. The cure is the photo, not the code — a sharper picture, better light, or a larger face.

6. Save the numbers with the name

    known_face_encodings.append(img_encoding)
    known_face_names.append(filename)
    print("images encoded", filename)

append adds to the end of each list, so the encoding and the name stay in step. The print shows progress such as images encoded Howard, one line per photo. When the loop ends, the two lists hold three encodings and three names, ready for the next version.

The whole program

The pieces above, put back together. This is the complete file, 18 lines in all:

import face_recognition
import cv2
import os
import glob
images_folder = "images"
images_path = glob.glob(os.path.join(images_folder, "*.*"))
print("{} images in folder".format(len(images_path)))
known_face_encodings = []
known_face_names = []
for img_path in images_path:
    img = cv2.imread(img_path)
    rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    basename = os.path.basename(img_path)
    (filename, ext) = os.path.splitext(basename)
    img_encoding = face_recognition.face_encodings(rgb_img)[0]
    known_face_encodings.append(img_encoding)
    known_face_names.append(filename)
    print("images encoded", filename)

Seeing the numbers for yourself

The encodings are ordinary Python lists, so you can look at one. Add this line inside the loop, after the encoding is made:

    print(img_encoding)

The screen fills with 128 small decimals between about −0.15 and 0.15. No single number means anything on its own. Two lists of them, side by side, do: that is the comparison the next version makes.

Running it on your own computer

Use the same download as version 1: Download the zip file, unzip it, and open the folder in Visual Studio Code. This note uses face02.py. The first time, run all three lines:

uv python pin 3.12.10
uv sync
uv run face02.py

uv sync is the slow one here, because face_recognition brings in dlib and about 100 MB of models. After that, uv run face02.py alone is enough.

Those three lines are needed only once. The next time you want to run the program again, type just uv run face02.py.

Use one clear face per photo, and the person's name as the file name, for example Anna.jpg. If a photo has two faces, [0] quietly takes the first and the name will be wrong, so keep one person per file.

What comes next

Version 3 points the same measurement at a new photograph, compares the result with these stored encodings, and prints the name of the closest match.