Pyfacer

0.0.5 · active · verified Thu Apr 16

Pyfacer is a Python API that wraps the core 'facer' library, providing a high-level interface for face-related tasks such as detection, parsing, recognition, and alignment. It is part of the broader FacePerceiver ecosystem. Currently at version 0.0.5, it is explicitly marked as 'under development', indicating an early-stage project with potential for rapid changes. Its release cadence is infrequent.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `FacerAPI` and perform a basic face detection on a dummy image. It highlights the internal dependency on the `facer` package and potential model download times.

import torch
import numpy as np
from PIL import Image
from pyfacer.facer_api import FacerAPI

# Create a dummy image (e.g., a black image with a white square)
# The underlying 'facer' library expects a torch.Tensor, typically (1, 3, H, W) float, normalized 0-1.
dummy_img_np = np.zeros((256, 256, 3), dtype=np.uint8)
dummy_img_np[100:150, 100:150] = [255, 255, 255] # Add a white square

# Convert NumPy array to PIL Image, then to PyTorch Tensor.
image_pil = Image.fromarray(dummy_img_np)
image_tensor = torch.from_numpy(np.array(image_pil)).float() / 255.0 # HWC, float 0-1
image_tensor = image_tensor.permute(2, 0, 1).unsqueeze(0) # CHW -> NCHW

try:
    # Determine device
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print(f"Initializing FacerAPI on {device}...")

    # Instantiate the FacerAPI. This will internally load models via the 'facer' library.
    # It might take a while on first run to download models.
    api = FacerAPI(device=device)

    print("Detecting faces...")
    # Perform a face detection. `detect_faces` returns a list of facer.Face objects or similar.
    detected_faces = api.detect_faces(image_tensor)

    print(f"Number of detected 'faces': {len(detected_faces) if detected_faces else 0}")
    if detected_faces:
        # Assuming `facer.Face` objects have `boxes` attribute, as in the core `facer` library.
        print(f"First 'face' (or object) bounding box: {detected_faces[0].boxes}")

except Exception as e:
    print(f"An error occurred during pyfacer quickstart: {e}")
    print("\nTroubleshooting:")
    print("1. Ensure 'facer' library is also installed: `pip install facer`")
    print("2. Models are downloaded by 'facer' on first use; this might require internet access.")
    print("3. Check for CUDA errors if using GPU.")

view raw JSON →