Preface
Before I start, I will give the context why I started this. I have a bunch of images from a ceremony and I want them to be sorted into specific directories. I want to group them into individual people so that I can see how many good photos I have for each. This can easily be done with Adobe Photoshop, but that takes away the fun aspect from it.
We will keep this short and just concentrate on building this feature. This will use Python 3 with following dependencies.
opencv-python==4.3.0.36 face-recognition==1.3.0 Pillow==7.2.0
I have all my files in a specific directory. Target is to move them to different directories based on whose face is in the image. While at it, we will also extract the face and keep it as an index to identify who this directory contains.
Walk the directory
We start with importing all required libraries.
from PIL import Image from os import walk, mkdir from shutil import copyfile import face_recognition as fcr
Next we will get all files in my directory to an array. This way we can start our processing.
# Get all files in array flist = [] src_dir = '/tmp/photos/' for (_, _, filenames) in walk(src_dir, topdown=True): flist.extend(filenames) break
We created an empty array and put all files into this. We have used python os library command walk to traverse the directory and accumulate all files. Next would be loop through all these files and use face recognition to sort all people images.
Image sorting
known_faces = [] known_face_nm = [] known_face_cnt = 0 cnt = 0 for x in flist: cnt += 1 img_file = src_dir + x print("Processing image: {}".format(img_file)) img = fcr.load_image_file(img_file) f_locations = fcr.face_locations(img) f_encodings = fcr.face_encodings(img, f_locations) imc = 0
We start by looping through all files and loading them using using face_recognition library. First step is to get all faces in this image. Next we calculate face encodings for all identified faces. For all of these we use face_recognition library.
for f_encoding in f_encodings: f_match = fcr.compare_faces(known_faces, f_encoding) if True in f_match: mtch_idx = f_match.index(True) print("Matched with: {}".format(known_face_nm[mtch_idx])) fldst = src_dir + known_face_nm[mtch_idx] + "/" + x else: known_face_cnt += 1 known_faces.append(f_encoding) known_face_nm.append('Person_' + str(known_face_cnt)) dirname = 'Person_' + str(known_face_cnt) fldst = src_dir + dirname + "/" + x mkdir(src_dir + dirname) print("Created directory: {}".format('Person_' + str(known_face_cnt))) # Make a directory and copy this file and Face top, right, bottom, left = f_locations[imc] img_face = img[top:bottom, left:right] img_pil = Image.fromarray(img_face) img_pil.save(src_dir + dirname + "/FACE.jpg") imc += 1 # Copy the file try: copyfile(img_file, fldst) except IOError as e: print("Copy file error: %s" % e)
That is the core of what we are doing. We start by looping on all encodings that the library was able to identify. We calculate the face signature and compare it with what is already stored in our list of faces. If this is a new signature, we do the following:
- Store the signature in array
- Create a new directory as Person_<next sequence>
- Extract the face from this image and store it as FACE.jpg in this directory
- Copy the file to this directory
If we could match this image with an existing one, we will just copy this image over to the same directory. So finally we have a list of directories created with all people distinguished by their faces and sorted into respective directories.

Image shows how the directory will look after all images have been added to respective directories. We will have a file called FACE.jpg in all directory that shows who this is for – so we can name the directory accordingly.
All images are sorted based on who it contains and added to respective directory.
Finale
The code above can separate out a directory of images and put them in respective directory based on who is present in the image. We have used face_recognition library to sort the faces – this in turn uses dlib library. We can of course delete the files that have been processed already , but you can add that separately.
This was a quick blog to show the usage of face_recognition library that is one of the most used libraries for face detection/ identification. Please feel free to leave any questions if further details are required. Ciao for now!