InsightFace Python Library

0.7.3 · active · verified Sun Apr 12

InsightFace is an open-source 2D & 3D deep face analysis toolbox for tasks like face recognition, detection, and alignment, with capabilities extending to face swapping and anti-spoofing. It provides state-of-the-art models optimized for both training and deployment. The library primarily uses ONNX Runtime as its inference backend. The current version is 0.7.3, and it maintains an active development and maintenance cadence with frequent updates and community support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `FaceAnalysis` application, prepare it with execution providers (preferring CUDA if available), load a bundled sample image, perform face detection and analysis, and draw the results on the image. It also prints basic information about the first detected face.

import cv2
import numpy as np
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image

# Initialize FaceAnalysis app with desired providers (e.g., CUDA or CPU)
# Ensure onnxruntime-gpu is installed for CUDAExecutionProvider
app = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))

# Load a sample image
img = ins_get_image('t1') # 't1' is a sample image bundled with the library

# Perform face detection and analysis
faces = app.get(img)

# Draw results on the image
rimg = app.draw_on(img, faces)

# Display or save the output image
# cv2.imshow("InsightFace Demo", rimg)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2.imwrite("./t1_output.jpg", rimg)
print(f"Detected {len(faces)} face(s). Output saved to t1_output.jpg")

if faces:
    face = faces[0]
    print(f"Face 0: Bounding Box: {face.bbox}, Landmark: {face.landmark}")
    if face.embedding is not None:
        print(f"Face 0: Embedding shape: {face.embedding.shape}")
    if face.gender is not None:
        print(f"Face 0: Gender: {face.gender}, Age: {face.age}")

view raw JSON →