How to find out who is in a photo (Face recognition, version 3)?

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

Version 2 stored one list of 128 numbers for each person it knows. Version 3 finally uses them. It opens a new photograph, measures every face in it, compares those measurements with the stored ones, and prints the name of the person who matches. This is the first version that answers a question instead of just preparing for one.

The whole comparison rests on one fact from version 2: two photos of the same person produce two lists that are close together, and two different people produce lists that are further apart. Everything below is a way of measuring that closeness and deciding how close is close enough.

How the program works

1. One more library

import face_recognition
import cv2
import os
import glob
import numpy as np

numpy is NumPy, short for Numerical Python. It works on whole lists of numbers at once, which is what the distances are. as np is only a nickname, so the code can write np.round instead of numpy.round.

The first four lines do the same work as in version 2: find the photos in the folder and turn each known face into numbers. The only difference is that this version does not print the running count; it goes straight to building the two lists.

2. A new photograph to check

img = cv2.imread("image-source.JPG")
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

image-source.JPG is the photo you want to identify: a class photo, a group picture, a picture sent to you. It sits beside the program, not inside images, because it is not someone the program knows yet. The second line fixes the colour order in the same way as version 2.

One practical point: the file name in the code must match the real file exactly, including capital letters. Mac screenshots often arrive as .JPG in capitals. Windows and a default Mac ignore the difference, Linux does not, so it is worth checking the name you typed.

3. Find the faces before measuring them

face_locations = face_recognition.face_locations(rgb_img)
face_encodings = face_recognition.face_encodings(rgb_img, face_locations)

face_locations is a list with one entry per face found, each entry a box around that face. In a group photo there may be five.

The second line passes those boxes back to face_encodings. The library would find the faces again on its own, but telling it where they are saves that work and guarantees that the measurements and the boxes refer to the same faces.

Notice there is no [0] this time. Version 2 kept only the first face because each known photo holds one person. Here every face in the photo is measured, because the question is who is in the picture, not who is the first person in the picture.

4. Compare, face by face

for face_encoding in face_encodings:
    matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

compare_faces takes the new face and the whole list of known faces, and returns one True or False for each known person, in the same order as known_face_names. With three people known, the result looks like [True, False, False]: this face matches the first person and not the other two.

Behind that True is a threshold. The library compares two faces by their distance, and counts them as the same person when the distance is 0.6 or less. Version 9 of this series changes that number; for now, 0.6 is the default.

5. How far away is each known face?

    face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
    print(np.round(face_distances, 2))
    best_match_index = np.argmin(face_distances)

face_distance returns the actual number for each known person instead of a yes or no: something like [0.28, 0.63, 0.64]. Smaller means more similar. The same person photographed twice is usually below 0.4, and two different people are usually above 0.6.

np.round(..., 2) only tidies the display to two decimals. Then np.argmin finds the position of the smallest number — the closest known face. Positions start at 0, so 0 means the first name in known_face_names.

6. Require both: closest and close enough

    name = "Unknown"
    if matches[best_match_index]:
        name = known_face_names[best_match_index]
        face_names.append(name)

Being the closest is not the same as being a match. In a photo of a stranger, somebody in the known list will still be the closest, with a distance of perhaps 0.7. Line matches[best_match_index] asks whether that closest person also passed the 0.6 test. Only then is the name accepted.

The append is inside the if, so faces with no match are simply left out of the list. That is why "Unknown", set as the starting value, never appears in the output: a stranger adds nothing rather than adding the word Unknown.

The whole program

The pieces above, put back together, with the running count from version 2 dropped to keep the listing short:

import face_recognition
import cv2
import os
import glob
import numpy as np

images_folder = "images"
images_path = glob.glob(os.path.join(images_folder, "*.*"))

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)

img = cv2.imread("image-source.JPG")
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb_img)
face_encodings = face_recognition.face_encodings(rgb_img, face_locations)

face_names = []
for face_encoding in face_encodings:
    matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
    name = "Unknown"
    face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
    best_match_index = np.argmin(face_distances)
    if matches[best_match_index]:
        name = known_face_names[best_match_index]
        face_names.append(name)

print("The matched name's image file is", face_names)

What the output means

If image-source.JPG is a photo of Howard, the last line prints:

The matched name's image file is ['Howard']

If you add the two print lines back in, the middle of the run shows the working:

[0.28 0.63 0.64]
0

Howard is the closest at 0.28, well inside the 0.6 limit, so his name is the one that survives. A stranger would give distances such as [0.71 0.74 0.69], the closest would be 0.69, the if would fail, and the printed list would be empty: [].

An empty list is a correct answer, not a bug. It means nobody in the picture is somebody the program knows.

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 face03.py. The first time in this folder, run all three lines:

uv python pin 3.12.10
uv sync
uv run face03.py

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

Put the photo you want to check beside face03.py and name it image-source.JPG. The zip already includes one, so the program works as soon as it is unzipped. Replace that file with your own photo to test a real question. A sharp, front facing face gives the best answer; a face turned away or badly lit may come back as no match even when the person is in the folder.

What comes next

Versions 1 to 3 all work on files that already exist. Version 4 opens the camera and shows live video, which is the step needed before recognition can happen as you move.