FaceXlib: Basic Face Library

0.3.0 · active · verified Sat Apr 11

FaceXlib is a Python library providing ready-to-use face-related functions based on state-of-the-art open-source methods, including detection, alignment, recognition, parsing, and restoration. It is currently at version 0.3.0 and has an active, though somewhat irregular, release cadence with recent updates focusing on stability and functionality improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `FaceRestoreHelper`, read an image (dummy in this case), detect and align faces. It highlights the basic workflow, where detected and aligned faces would typically be fed into a specialized face restoration or processing model. Model weights are downloaded automatically on first inference.

import numpy as np
import cv2
from facexlib.utils.face_restoration_helper import FaceRestoreHelper

# Create a dummy image (e.g., a black square)
img = np.zeros((512, 512, 3), dtype=np.uint8)
# Add a white square to simulate a face for detection
img[200:300, 200:300] = 255

# Initialize FaceRestoreHelper
# upscale_factor: The factor to upscale the face. Set to 1 if no upscale needed
# det_model: The detection model to use, e.g., 'retinaface_resnet50' or 'retinaface_mobile0.25'
# device: 'cuda' or 'cpu'
face_helper = FaceRestoreHelper(upscale_factor=1, det_model='retinaface_resnet50', device='cpu')

# Read the image (can also be a path)
face_helper.read_image(img)

# Detect and align faces
# save_cropped_path: Optional path to save cropped faces
face_helper.get_face_landmarks_5(only_keep_largest=True)
face_helper.align_warp_face()

# Process the aligned faces (e.g., feed to a restoration model)
# This example just shows the aligned face
if len(face_helper.cropped_faces) > 0:
    aligned_face = face_helper.cropped_faces[0]
    print(f"Detected and aligned face of shape: {aligned_face.shape}")
    # In a real scenario, you'd feed aligned_face to a restoration network
    # For this quickstart, we'll just show its dimensions.
    # cv2.imwrite('aligned_face.png', aligned_face) # Uncomment to save
else:
    print("No faces detected in the image.")

# Clean up (release models if no longer needed)
del face_helper

view raw JSON →