UniFace: All-in-One Face Analysis Library

3.5.0 · active · verified Wed Apr 15

UniFace is a lightweight, production-ready Python library built on ONNX Runtime for comprehensive face analysis. It provides high-performance capabilities for face detection, recognition, tracking, 106-point landmark detection, face parsing, gaze estimation, age, and gender detection, with hardware acceleration across various platforms. The library is actively maintained, with its current version being 3.5.0, and receives regular updates adding new features and models.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a RetinaFace detector and use it to detect faces in an image. Models are automatically downloaded and cached upon their first use. The example includes creating a dummy image and printing detection results. For real-world use, replace the dummy image with `cv2.imread('your_image.jpg')`.

import cv2
import numpy as np
from uniface.detection import RetinaFace

# Create a dummy image for demonstration
# In a real scenario, replace this with cv2.imread('your_image.jpg')
image = np.zeros((480, 640, 3), dtype=np.uint8)
cv2.putText(image, "Hello UniFace!", (100, 240), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)

# Initialize the face detector (models download on first use)
detector = RetinaFace(confidence_threshold=0.5, nms_threshold=0.4)

# Detect faces in the image
faces = detector.detect(image)

if faces:
    print(f"Found {len(faces)} face(s).")
    for i, face in enumerate(faces, start=1):
        print(f"[Face {i}] Confidence: {face.confidence:.3f}, Bounding Box: {face.bbox}")
        # Optionally, you can draw bounding boxes or process further
        # Example: draw a rectangle around the first face
        if i == 1:
            x, y, w, h = int(face.bbox.x), int(face.bbox.y), int(face.bbox.width), int(face.bbox.height)
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
else:
    print("No faces detected.")

# You can also use FaceAnalyzer for an all-in-one approach:
# from uniface import FaceAnalyzer
# analyzer = FaceAnalyzer()
# analyzed_faces = analyzer.analyze(image)
# for face in analyzed_faces:
#     print(face.bbox, face.embedding.shape if face.embedding is not None else None)

view raw JSON →