Skip to main content

High-performance face detection, recognition, and manipulation library with GPU acceleration

Project description

Visagene

Python CUDA License

Visagene is a high-performance face detection, recognition, and manipulation library with GPU acceleration support. It supports both ONNX Runtime and TensorRT for inference, providing features such as face detection, feature extraction, face swapping, and image enhancement.

Key Features

  • Face Detection: High-precision face detection with bounding boxes and keypoints
  • Feature Extraction: Extract face embeddings for recognition and comparison
  • Face Swapping: Natural face replacement from source to target images
  • Image Enhancement: Face quality improvement using GFPGANv1.4
  • Segmentation: Precise segmentation of facial features (eyes, nose, mouth, etc.)
  • Paste Back: Natural blending of processed faces back to original images

Technical Highlights

  • GPU Acceleration: Fast GPU processing using CuPy
  • Flexible Inference: Support for both ONNX Runtime and TensorRT
  • Memory Efficient: Optimized GPU memory usage
  • Type Safe: Data schemas defined with Pydantic
  • Extensible: Easy to add new models by inheriting base classes

Installation

Prerequisites

  • Python 3.10 or higher
  • CUDA 12.x
  • cuDNN 8.x or higher

Install via pip

pip install visagene

Development Setup

# Clone the repository
git clone https://github.com/yourusername/visagene.git
cd visagene

# Install development dependencies
pip install -e ".[dev]"

Usage

Basic Face Detection

import pixtreme as px
import visagene_source as vg

# Load image
image = px.imread("path/to/image.jpg")
image = px.to_float32(image)

# Initialize face detector
detector = vg.OnnxDetector(model_path="models/detection.onnx")

# Detect faces
faces = detector.get(image)

print(f"Detected {len(faces)} faces")
for face in faces:
    print(f"Bounding box: {face.bbox}")
    print(f"Confidence score: {face.score}")

Face Swapping Pipeline

# Initialize models
detector = vg.OnnxDetector(model_path="models/detection.onnx")
extractor = vg.OnnxExtractor(model_path="models/embedding.onnx")
swapper = vg.OnnxSwapper(model_path="models/swap.onnx")
enhancer = vg.OnnxEnhancer(model_path="models/enhance.onnx")

# Load source and target images
source_image = px.imread("source.jpg")
target_image = px.imread("target.jpg")

# Extract source face embedding
source_faces = detector.get(source_image)
source_embedding = extractor.get(source_faces[0])

# Detect target face and swap
target_faces = detector.get(target_image)
swapped_face = swapper.get(target_faces[0].image, source_embedding)

# Enhance face quality
enhanced_face = enhancer.get(swapped_face)

# Paste back to original image
result = vg.paste_back(target_image, enhanced_face, target_faces[0].matrix)

High-Speed Inference with TensorRT

# Use TensorRT versions of models
detector = vg.TrtDetector(model_path="models/detection.trt")
extractor = vg.TrtExtractor(model_path="models/embedding.trt")
swapper = vg.TrtSwapper(model_path="models/swap.trt")
enhancer = vg.TrtEnhancer(model_path="models/enhance.trt")

# Usage is identical
faces = detector.get(image)

Model Architecture

Class Hierarchy

BaseModelLoader
├── BaseDetector
│   ├── OnnxDetector
│   └── TrtDetector
├── BaseExtractor
│   ├── OnnxExtractor
│   └── TrtExtractor
├── BaseSwapper
│   ├── OnnxSwapper
│   └── TrtSwapper
├── BaseEnhancer
│   ├── OnnxEnhancer
│   └── TrtEnhancer
└── BaseSegmentation
    └── OnnxSegmentation

Data Schema

The library uses Pydantic for type-safe data structures:

class VisageneFace(BaseModel):
    bbox: cp.ndarray      # Bounding box (x1, y1, x2, y2)
    score: float          # Detection confidence score
    kps: cp.ndarray       # Facial keypoints
    matrix: cp.ndarray    # Affine transformation matrix
    image: cp.ndarray     # Cropped face image

Dependencies

Core Dependencies

  • numpy: Numerical computing library
  • cupy-cuda12x (>=13.4.1): CUDA-accelerated array library
  • onnxruntime-gpu (>=1.22.0): ONNX inference engine
  • tensorrt (>=10.11.0.33): NVIDIA TensorRT inference engine
  • pixtreme (>=0.3.0): High-performance image processing library
  • pydantic: Data validation and schema definition

Development Dependencies

  • black: Code formatter
  • pytest: Testing framework
  • flake8: Linter
  • isort: Import sorter
  • cython: C-extensions for Python
  • build tools: setuptools, wheel, packaging

Model Requirements

The library requires pre-trained ONNX models for operation. The models are not included in the repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Acknowledgments

  • ONNX Runtime - High-performance inference engine
  • TensorRT - NVIDIA's high-speed inference library
  • CuPy - GPU-accelerated computing with Python

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

visagene-0.4.1.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

visagene-0.4.1-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file visagene-0.4.1.tar.gz.

File metadata

  • Download URL: visagene-0.4.1.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.18

File hashes

Hashes for visagene-0.4.1.tar.gz
Algorithm Hash digest
SHA256 e2cff3bb30594a0a8aad0dedd98a1a8475e0dd92bf53dfd1f142c0d2657eaab0
MD5 3808f10720c9f78f2c140c06a6373ee0
BLAKE2b-256 acc5767a13ea85b2ee59ce49a5bb9871cae503ed2ccac39ee47b270f8e9040ca

See more details on using hashes here.

File details

Details for the file visagene-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: visagene-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.18

File hashes

Hashes for visagene-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 856c41295b3da7589767d83b2433c7e8125a27cecda0b57bc846d9c663c1c7ed
MD5 f9a671a860ad12a24106627c2bd66483
BLAKE2b-256 74159d59fda841b237c9256422c360cbe60845e0ca011f27fae17a0cd857d72d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page