SCRFD - Rust Package for Face Detection
SCRFD is a Rust library for face detection, providing both synchronous and asynchronous support. It utilizes ONNX Runtime for high-performance inference and supports bounding box and keypoint detection.
Breaking Changes in v1.2.0
- The
detectfunction now acceptsopencv::core::Matinstead ofimage::RgbImage- This change significantly improves performance by:
- Eliminating unnecessary image conversions
- Using OpenCV's optimized memory management
- Leveraging hardware-accelerated image processing operations
- Reducing memory allocations and copies
- Providing direct compatibility with OpenCV's extensive computer vision ecosystem
- This change significantly improves performance by:
- Added builder pattern for easier model configuration
- Added support for relative output coordinates
- Updated default parameters:
- Confidence threshold changed from 0.5 to 0.25
- IoU threshold changed from 0.5 to 0.4
Features
- Face Detection: Detect bounding boxes and landmarks for faces in images.
- Asynchronous Support: Optional async functionality for non-blocking operations.
- Builder Pattern: Fluent interface for easy model configuration.
- Customizable Parameters:
- Input size
- Confidence threshold
- IoU threshold
- Relative output coordinates
- Efficient Processing:
- Anchor-based detection
- Optimized caching for anchor centers
Installation
Add the library to your Cargo.toml:
[]
= { = "1.2.0", = ["async"] } # Enable async feature if needed
To enable synchronous mode only, omit the async feature:
[]
= "1.2.0"
Usage
Using the Builder Pattern (Recommended)
use SCRFDBuilder;
use SessionBuilder;
use HashMap;
use imread;
use IMREAD_COLOR;
Direct Initialization
use SCRFD;
use SessionBuilder;
use HashMap;
use imread;
use IMREAD_COLOR;
Asynchronous Example
Enable the async feature in Cargo.toml:
[]
= { = "1.2.0", = ["async"] }
use SCRFDBuilder;
use SessionBuilder;
use HashMap;
use imread;
use IMREAD_COLOR;
async
API Documentation
Builder Pattern
The SCRFDBuilder provides a fluent interface for configuring SCRFD models:
let model = new
.set_input_size // Set input dimensions
.set_conf_thres // Set confidence threshold
.set_iou_thres // Set IoU threshold
.set_relative_output // Enable relative output
.build?; // Build synchronous model
For async models:
let model = new
.set_input_size
.set_conf_thres
.set_iou_thres
.set_relative_output
.build_async?; // Build asynchronous model
Default Parameters
- Input size: (640, 640)
- Confidence threshold: 0.25
- IoU threshold: 0.4
- Relative output: true
Detect Function
The detect function now accepts OpenCV's Mat type instead of image::RgbImage:
Helper Functions
Available in ScrfdHelpers:
generate_anchor_centers: Efficiently generate anchor centers for feature mapsdistance2bbox: Convert distances to bounding boxesdistance2kps: Convert distances to keypointsnms: Perform non-maximum suppression to filter detections
Contributing
Contributions are welcome! Please open an issue or submit a pull request for improvements.
Running Tests
- For synchronous features:
- For asynchronous features:
License
This library is licensed under the MIT License. See the LICENSE file for details.