How to show every face photo in a folder (Face recognition, version 1)?
This is version 1 of a nine-part series that builds, one small step at a time, a program that recognises the faces your webcam sees. Version 1 contains no AI at all. It opens a folder of photographs, shows them one by one, and prints each person's name. That is deliberate: before the computer can recognise a face, it must first be able to find the files and open them.
Where do the names come from?
A face recognition program has to know who is who. You could type a list of names, but that
is one more thing to keep in step with the photos. Instead, this series uses the file name.
If the folder holds Howard.JPG, Mary.jpg and Paul.jpg,
then the program already knows three names, and it will read them off the files when it needs
them.
That is why the last three lines of the program print just Howard, without the
images/ folder in front and without the .JPG at the end. What is
printed here becomes the label that later versions put on the face.
How the program works
1. Load three libraries
import cv2
import os
import glob
cv2 is OpenCV, the library that opens and shows images.
os works with the computer's files and folders, and knows whether your machine
uses / or \ between folder names. glob finds files whose
names match a pattern, which is how the program discovers the photos without being told their
names.
2. List every file in the folder
images_folder = "images"
images_path = glob.glob(os.path.join(images_folder, "*.*"))
The first line stores the folder name. The second builds the pattern
images/*.* — any name, a dot, any extension — using
os.path.join so that the separator is correct on Mac and on Windows.
glob.glob then returns a list of every matching path, for example
["images/Howard.JPG", "images/Mary.jpg", "images/Paul.jpg"].
*.* is convenient but not fussy: it also picks up stray files such as
.DS_Store on a Mac. If that happens, the program simply fails to read that one
entry and carries on, or you can delete the stray file.
3. Count the photos
print("{} images in folder".format(len(images_path)))
len() means length, so it counts the paths in the list. The {} is a
placeholder that .format() fills in, so the screen shows
3 images in folder before any picture appears. If that number is zero, the folder
name is wrong or the photos are somewhere else, and it is worth fixing that before reading
further.
4. Show each photo for one second
for img_path in images_path:
img = cv2.imread(img_path)
cv2.imshow("image", img)
cv2.waitKey(1000)
The for line takes the paths one at a time; everything indented under it runs once
per photo. cv2.imread reads the file into img, a grid of pixel values
that later versions will search for a face. cv2.imshow displays it in a window
titled "image", and cv2.waitKey(1000) holds it there for 1000
milliseconds, one second, before the next photo replaces it.
One second is long enough to see the picture and short enough to sit through a folder of them. Raising the number slows the slideshow down.
5. Print the name inside the file name
basename = os.path.basename(img_path)
(filename, ext) = os.path.splitext(basename)
print(filename)
os.path.basename drops the folder, so images/Howard.JPG becomes
Howard.JPG. os.path.splitext splits the name from the extension and
returns two pieces, Howard and .JPG, which are stored in
filename and ext. The last line prints Howard while his
photo is on screen.
The whole program
The pieces above, put back together. This is the complete file, 13 lines in all:
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)))
for img_path in images_path:
img = cv2.imread(img_path)
cv2.imshow("image", img)
cv2.waitKey(1000)
basename = os.path.basename(img_path)
(filename, ext) = os.path.splitext(basename)
print(filename)
Running it on your own computer
Download the zip file and
unzip it to a folder. The zip holds all nine programs in this series, the images
folder with three photos, and pyproject.toml. This note uses
face01.py. Open the folder in Visual Studio Code, then type these three lines in
the terminal:
uv python pin 3.12.10
uv sync
uv run face01.py
The first line records which Python version this folder uses, and the second installs every
package that pyproject.toml lists. The third line shows the first photo. To use
your own photos, replace the files in images and keep the person's name as the
file name, for example Anna.jpg.
Those three lines are needed only once. The next time you want to run the program again,
type just uv run face01.py.
If you have not set up uv yet, start with How to install Visual Studio Code? and then the uv note for Windows or Mac.
What comes next
Version 1 has the cast and the photos, but nothing has been measured yet. Version 2 turns the face in each photo into a list of numbers, so that two faces can be compared.