this is an example of running face recognition on a single image and drawing a box around each person that was identified load a sample picture and learn how to recognize it load a second sample picture and learn how to recognize it create arrays of known face encodings and their names load an image with an unknown face find all the faces and face encodings in the unknown image convert the image to a pilformat image so that we can draw on top of it with the pillow library see http pillow readthedocs io for more about pilpillow create a pillow imagedraw draw instance to draw with loop through each face found in the unknown image see if the face is a match for the known faces if a match was found in knownfaceencodings just use the first one if true in matches firstmatchindex matches indextrue name knownfacenamesfirstmatchindex or instead use the known face with the smallest distance to the new face draw a box around the face using the pillow module draw a label with a name below the face remove the drawing library from memory as per the pillow docs display the resulting image you can also save a copy of the new image to disk if you want by uncommenting this line pilimage saveimagewithboxes jpg this is an example of running face recognition on a single image and drawing a box around each person that was identified load a sample picture and learn how to recognize it load a second sample picture and learn how to recognize it create arrays of known face encodings and their names load an image with an unknown face find all the faces and face encodings in the unknown image convert the image to a pil format image so that we can draw on top of it with the pillow library see http pillow readthedocs io for more about pil pillow create a pillow imagedraw draw instance to draw with loop through each face found in the unknown image see if the face is a match for the known face s if a match was found in known_face_encodings just use the first one if true in matches first_match_index matches index true name known_face_names first_match_index or instead use the known face with the smallest distance to the new face draw a box around the face using the pillow module draw a label with a name below the face remove the drawing library from memory as per the pillow docs display the resulting image you can also save a copy of the new image to disk if you want by uncommenting this line pil_image save image_with_boxes jpg
import face_recognition
from PIL import Image, ImageDraw
import numpy as np





obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]


biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]


known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "Barack Obama",
    "Joe Biden"
]


unknown_image = face_recognition.load_image_file("two_people.jpg")


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



pil_image = Image.fromarray(unknown_image)

draw = ImageDraw.Draw(pil_image)


for (top, right, bottom, left), face_encoding in zip(face_locations, 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]

    
    draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))

    
    text_width, text_height = draw.textsize(name)
    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))



del draw


pil_image.show()