Using the face_recognition Python Library for Face Detection, Landmark Identification, and Simple Applications
This article demonstrates how to install the Python face_recognition library, locate faces in images, crop and save them, encode faces into 128‑dimensional vectors, compare faces, detect facial landmarks, apply virtual makeup, and assemble these steps into a simple custom face‑recognition application.
The face_recognition library is a powerful, easy‑to‑use open‑source project for face recognition that includes complete documentation and works on platforms such as Raspberry Pi.
Install the library with pip install face_recognition and verify the installation using a short test script that loads an image and displays detected faces.
1. Locate faces in an image
<code>def demoFunc():
'''
Detect faces in a picture and draw rectangles around them
'''
image = face_recognition.load_image_file("test.jpg")
face_locations = face_recognition.face_locations(image)
for one in face_locations:
y0, x1, y1, x0 = one
cv2.rectangle(image, pt1=(x0, y0), pt2=(x1, y1), color=(0, 0, 255), thickness=3)
cv2.imshow('aaa', image)
if cv2.waitKey(0) & 0xFF == ord('q'):
cv2.destroyAllWindows()
</code>2. Crop and save each detected face
<code>def demoFunc():
'''
Crop each face from the image and save it locally
'''
img = cv2.imread("test.jpg")
image = face_recognition.load_image_file("test.jpg")
face_locations = face_recognition.face_locations(image) # (top, right, bottom, left)
for i in range(len(face_locations)):
y0, x1, y1, x0 = face_locations[i]
cropped = img.crop((x0, y0, x1, y1)) # (left, upper, right, lower)
cropped.save(str(i) + "_.jpg")
cropped.show()
</code>3. Encode each face into a 128‑dimensional vector
<code>def demoFunc():
'''
Convert each face in the picture to a 128‑dimensional vector
'''
image = face_recognition.load_image_file("cl.jpg")
face_locations = face_recognition.face_locations(image) # (top, right, bottom, left)
face_encodings = face_recognition.face_encodings(image, face_locations) # 128‑D vectors
for one in face_encodings:
print('one: ', one)
</code>The core principle of face_recognition is to map each face image to a 128‑dimensional feature vector; comparing two faces then reduces to measuring the similarity between their vectors.
4. Compare two images to determine if they belong to the same person
<code>def demoFunc(one_pic='c1.jpg', two_pic='c2.jpg'):
'''
Determine whether two pictures show the same person
'''
chenglong = face_recognition.load_image_file(one_pic)
unknown_image = face_recognition.load_image_file(two_pic)
biden_encoding = face_recognition.face_encodings(chenglong)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([biden_encoding], unknown_encoding)
print('results: ', results)
return results[0]
</code>5. Detect facial landmarks and annotate them
<code>def demoFunc(pic_path='cl.jpg'):
'''
Detect and annotate facial landmarks
'''
image = face_recognition.load_image_file(pic_path)
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for face_landmarks in face_landmarks_list:
for facial_feature in face_landmarks.keys():
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
for facial_feature in face_landmarks.keys():
d.line(face_landmarks[facial_feature], width=5)
pil_image.show()
</code>6. Apply virtual makeup using the detected landmarks
<code>def demoFunc(pic_path='haiwang.jpg'):
'''
Apply makeup based on facial landmarks
'''
image = face_recognition.load_image_file(pic_path)
face_landmarks_list = face_recognition.face_landmarks(image)
pil_image = Image.fromarray(image)
for face_landmarks in face_landmarks_list:
demo = ImageDraw.Draw(pil_image, 'RGBA')
demo.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
demo.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
demo.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=2)
demo.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=2)
demo.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
demo.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
demo.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=2)
demo.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=2)
demo.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
demo.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
demo.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=2)
demo.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=2)
pil_image.show()
</code>7. Build a simple face‑recognition application by combining the above functions
<code>def faceRecognitionDemo(picDir='data/', test_pic='test.png'):
'''
Simple face‑recognition module based on face_recognition
'''
pic_list = os.listdir(picDir)
for one_pic in pic_list:
one_pic_path = picDir + one_pic
one_res = demo6(one_pic=one_pic_path, two_pic=test_pic)
one_name = one_pic.split('.')[0].strip()
if one_res:
print('This Person is: ', one_name)
break
else:
print('This Person is not: ', one_name)
</code>The article also notes that after obtaining 128‑D face embeddings, the recognition task can be turned into a classic machine‑learning classification problem using models such as SVM, Random Forest, or Gradient Boosted Trees.
Images throughout the original tutorial illustrate the results of each step, from face bounding boxes to landmark visualizations and virtual‑makeup effects.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.