Recognizr is a self-hosted, high-performance API for face detection and recognition, written entirely in Rust. It leverages the ONNX Runtime for efficient AI model inference, with optional GPU acceleration via CUDA.
This project was built to be a robust foundation for various computer vision tasks, providing endpoints for enrolling new individuals, recognizing them from images, and visually debugging the detection process.
- Fast Inference: Built in Rust with the
ortcrate, leveraging the high-performance ONNX Runtime. - GPU Acceleration: Optional support for NVIDIA GPUs via CUDA for a >15x performance increase.
- State-of-the-Art Models: Uses powerful, pre-trained models for face detection (SCRFD) and recognition (InsightFace ArcFace R100).
- Automatic Model Adaptation: Dynamically detects model output structure at startup for compatibility with different model variants.
- Optimized Performance: Pre-computed output mappings eliminate shape analysis overhead during inference.
- Vector Search: Utilizes SurrealDB for efficient and scalable similarity searches on face embeddings.
- Robust API: Built with
axum, providing endpoints for enrolling, recognizing, and debugging with comprehensive input validation. - Advanced Debugging: A dedicated debug endpoint that visually renders detection boxes, keypoints, and recognition labels on an image.
- Rust Toolchain: Ensure you have Rust and Cargo installed. (rustup.rs)
- (Optional) GPU Support: To enable GPU acceleration, you must have a compatible NVIDIA GPU and the required drivers and libraries installed on your system.
# Install the NVIDIA driver, CUDA Toolkit, and cuDNN library
sudo pacman -Syu nvidia cuda cudnn
# A reboot is required after driver installation.# Install the NVIDIA driver (this command selects the best recommended driver)
sudo ubuntu-drivers autoinstall
# A reboot is required after driver installation.
# Install the CUDA Toolkit and cuDNN from NVIDIA's official repository
# (Commands may vary slightly based on specific OS version - see NVIDIA's documentation)
sudo apt-get update
sudo apt-get install -y build-essential
# Download and install the CUDA repo pin and .deb package, then the toolkit
wget [https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb](https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb)
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-5 libcudnn9-cuda-12-devRecognizr uses a configuration file to manage all settings including file paths, database connection, and server configuration.
-
Configuration File: Create or modify the
config.tomlfile in the project root. A default configuration file is provided with sensible defaults.# Recognizr Configuration File [font] path = "DejaVuSansMono.ttf" [models.detector] path = "models/scrfd_10g_bnkps.onnx" strides = [8, 16, 32] input_shape = [640, 640] # [height, width] [models.recognizer] path = "models/arcface_r100.onnx" input_size = 112 [database] host = "127.0.0.1" port = 8000 username = "root" password = "root" namespace = "test" database = "test" [server] host = "0.0.0.0" port = 3000
-
Environment Variable Overrides: You can override any configuration setting using environment variables with the
RECOGNIZR_prefix:# Override database host export RECOGNIZR_DATABASE_HOST=192.168.1.100 # Override server port export RECOGNIZR_SERVER_PORT=8080 # Override model paths and settings export RECOGNIZR_MODELS_DETECTOR_PATH=/custom/path/detector.onnx export RECOGNIZR_MODELS_DETECTOR_INPUT_SHAPE="[512,512]" export RECOGNIZR_MODELS_RECOGNIZER_INPUT_SIZE=112
Before running the application, you must download and place the required model and font files as specified in your configuration.
-
Create the
modelsDirectory: In your project root, create amodelsdirectory (or the directory specified in your config).mkdir models
-
Download Models: You need two ONNX models. Place them in the paths specified in your configuration (default:
/modelsdirectory).- Face Detector:
scrfd_10g_bnkps.onnx - Face Recognizer:
arcface_r100.onnx(or another compatible InsightFace recognition model)
- Face Detector:
-
Download Font: The debug endpoint requires a font file for drawing text. Download and place it in the path specified in your configuration (default: root directory).
- Font File:
DejaVuSansMono.ttf
- Font File:
After this step, your directory structure should look like this:
recognizr/
├── models/
│ ├── scrfd_10g_bnkps.onnx
│ └── arcface_r100.onnx
├── src/
│ └── ...
├── Cargo.toml
├── config.toml
└── DejaVuSansMono.ttf
Build the application in release mode for the best performance. If you have set up GPU support, ensure the cuda feature is enabled in your Cargo.toml for the ort crate.
cargo build --releaseThe application will load its configuration from config.toml in the project root. You can override any configuration setting using environment variables with the RECOGNIZR_ prefix.
To run with GPU acceleration, you must set the LD_LIBRARY_PATH environment variable so the application can find the necessary ONNX and CUDA library files at runtime.
-
Find the ONNX Runtime Library Path: This library is located inside your project's target directory after building. Find its parent directory with this command:
ORT_LIB_PATH=$(dirname $(find ./target/release -name "libonnxruntime_providers_shared.so")) -
Run the Server: Combine the library path with the system's CUDA path and run the application.
export LD_LIBRARY_PATH=$ORT_LIB_PATH:/opt/cuda/lib:$LD_LIBRARY_PATH
RUST_LOG=recognizr=info ./target/release/recognizrThe default CUDA path is different.
export LD_LIBRARY_PATH=$ORT_LIB_PATH:/usr/local/cuda/lib64:$LD_LIBRARY_PATH
RUST_LOG=recognizr=info ./target/release/recognizrThe server will start on the address specified in your configuration (default: http://localhost:3000).
POST /enroll
Enrolls a single person by detecting their face and storing its biometric embedding in the database. The image should contain exactly one face.
name:string- The name of the person to enroll.image:file- The image file containing the person's face.
Example:
curl -X POST http://localhost:3000/enroll \
-F "name=Ada Lovelace" \
-F "image=@/path/to/ada.jpg"Response: 201 Created on success.
POST /recognize
Finds and recognizes all known faces in a given image.
image:file- The image file to be analyzed.
Example:
curl -X POST http://localhost:3000/recognize \
-F "image=@/path/to/group_photo.jpg"Example Success Response:
[
{
"name": "Ada Lovelace",
"similarity_score": 0.87,
"bbox": [
150.5,
210.2,
390.8,
505.1
]
}
]POST /debug/detector
A powerful debug endpoint that runs the full detection and recognition pipeline on an image and returns a new image with the results visually rendered.
image:file- The image file to be analyzed.
Query Parameters (Optional):
threshold:float- Overrides the default confidence threshold for face detection (e.g., ?threshold=0.6).
Example:
curl -X POST "http://localhost:3000/debug/detector?threshold=0.75" \
-F "image=@/path/to/my_photo.jpg" \
--output debug_result.jpgResponse: An image/jpeg or image/png file with bounding boxes, keypoints, and labels drawn on it.
Recognizr uses a flexible configuration system that supports both file-based configuration and environment variable overrides.
The config.toml file contains all application settings organized into logical sections:
[font]- Font file configuration for debug rendering[models.detector]- Face detector model configuration[models.recognizer]- Face recognizer model configuration[database]- SurrealDB connection settings[server]- HTTP server configuration
Recognizr automatically detects model outputs by analyzing their shapes at startup, but requires some configuration to work with different model architectures:
path- Path to the ONNX detector model filestrides- Detection strides used by the model (typically[8, 16, 32]for SCRFD)input_shape- Model input dimensions as[height, width](e.g.,[640, 640])
path- Path to the ONNX recognizer model fileinput_size- Square input size for face crops (e.g.,112for 112x112 input)
The system automatically:
- Analyzes model outputs at startup by running inference once with dummy input
- Matches outputs by shape to determine which correspond to scores, bounding boxes, and keypoints
- Pre-computes mappings for efficient runtime inference
- Supports different model architectures as long as they follow the SCRFD output pattern
This means you can use different SCRFD variants or input sizes without manual output mapping configuration.
Any configuration setting can be overridden using environment variables with the RECOGNIZR_ prefix. The variable names follow the pattern RECOGNIZR_<SECTION>_<SETTING>.
Examples:
# Database configuration
export RECOGNIZR_DATABASE_HOST=192.168.1.100
export RECOGNIZR_DATABASE_PORT=8001
export RECOGNIZR_DATABASE_USERNAME=myuser
export RECOGNIZR_DATABASE_PASSWORD=mypassword
# Server configuration
export RECOGNIZR_SERVER_HOST=127.0.0.1
export RECOGNIZR_SERVER_PORT=8080
# Detector model configuration
export RECOGNIZR_MODELS_DETECTOR_PATH=/custom/models/detector.onnx
export RECOGNIZR_MODELS_DETECTOR_STRIDES="[8,16,32]"
export RECOGNIZR_MODELS_DETECTOR_INPUT_SHAPE="[512,512]"
# Recognizer model configuration
export RECOGNIZR_MODELS_RECOGNIZER_PATH=/custom/models/recognizer.onnx
export RECOGNIZR_MODELS_RECOGNIZER_INPUT_SIZE=112
# Font path
export RECOGNIZR_FONT_PATH=/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttfThis makes it easy to deploy Recognizr in different environments (development, staging, production) without modifying the configuration file.
Recognizr includes comprehensive input validation to ensure robust operation:
- File size: Maximum 15MB per image
- Dimensions: Minimum 32x32 pixels, maximum 8192x8192 pixels
- Format: Supports common image formats (JPEG, PNG, etc.)
- Length: Maximum 100 characters
- Content: Cannot be empty or whitespace-only
These limits help prevent resource exhaustion and ensure consistent performance across different deployment environments.