import face_recognition
import PIL.Image
import PIL.ImageDraw
from pathlib import Path
for filename in Path("./").glob("*.jpg"):
# Load the jpg files into numpy arrays
image = face_recognition.load_image_file(filename)
# Generate the face encodings
face_encodings = face_recognition.face_encodings(image)
if len(face_encodings) == 0:
# No faces found in the image.
print("No faces were found in {}.".format(filename))
else:
# Grab the first face encoding
first_face_encoding = face_encodings[0]
# Print the results
print("Face encoding for {}\n".format(filename),first_face_encoding)
# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
number_of_faces = len(face_landmarks_list)
print("I found {} face(s) in this photograph.".format(number_of_faces))
# Load the image into a Python Image Library object so that we can draw on top of it and display it
pil_image = PIL.Image.fromarray(image)
# Create a PIL drawing object to be able to draw lines later
draw = PIL.ImageDraw.Draw(pil_image)
# Loop over each face
for face_landmarks in face_landmarks_list:
# Loop over each facial feature (eye, nose, mouth, lips, etc)
for name, list_of_points in face_landmarks.items():
# Print the location of each facial feature in this image
print("The {} in this face has the following points: {}".format(name, list_of_points))
# Let's trace out each facial feature in the image with a line!
draw.line(list_of_points, fill="red", width=2)
pil_image.show()