Face Recognition

1.3.0 · active · verified Thu Apr 16

face-recognition is a Python library that provides a straightforward API for facial detection, facial landmark localization, and face recognition from images and video. It leverages dlib's state-of-the-art deep learning models for high accuracy and offers both Python module and command-line interfaces. The current version is 1.3.0, and releases occur periodically to add features, improve performance, and address compatibility issues, often tied to its core dependency, dlib.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load an image and find all faces within it, returning their bounding box coordinates. It also shows how to locate key facial features (landmarks) like eyes, nose, and mouth. Ensure you have an image file named `my_picture.jpg` in the same directory as your script, or update the path accordingly.

import face_recognition
import os

# Create a dummy image file for demonstration
# In a real scenario, you would load an actual image.
# You can replace 'my_picture.jpg' with a path to your own image.
# For example, download one from the internet and save it.
if not os.path.exists('my_picture.jpg'):
    print("Please provide a 'my_picture.jpg' file in the current directory for the quickstart example.")
    # Exit or create a placeholder if it's strictly necessary for execution, but for a quickstart, it's better to tell user to provide one.
    # For a truly runnable example without external files, one might generate a blank image or use base64 encoded data, but it complicates the quickstart.
    exit()

image = face_recognition.load_image_file("my_picture.jpg")
face_locations = face_recognition.face_locations(image)

print(f"Found {len(face_locations)} face(s) in this image.")

for face_location in face_locations:
    top, right, bottom, left = face_location
    print(f"A face is located at pixel location Top: {top}, Left: {left}, Bottom: {bottom}, Right: {right}")

# To demonstrate facial landmarks (eyes, nose, mouth, etc.)
face_landmarks_list = face_recognition.face_landmarks(image)

for face_landmarks in face_landmarks_list:
    print("Facial features for a face:")
    for facial_feature in face_landmarks.keys():
        print(f"- The {facial_feature} in this face has the following points: {face_landmarks[facial_feature]}")

view raw JSON →