A small project that wrap the InsightFace SDK in order to perform face comparison. It uses a fork of InsightFace in order to implement some convenient method to be exposed for Rust that can be found here. The implementation is based on the sample provided by the insightface team. c example link
Important
This guide required you to download the insightface
library and to follow the instructions in orders.
-
To use the library you'll need to specify the model that will be used. These models are the same that are used by the InsightFace library. The models can be found here
-
Clone the insightface's fork repository using this link and build the
insightface
library with the command
cd insightface/cpp-package/inspireface && git clone --recurse-submodules https://github.com/tunmx/inspireface-3rdparty.git 3rdparty
cd insightface/cpp-package/inspireface && bash command/build.sh
- Specify the path to the
insightface
library in yourCargo.toml
by adding this into your.cargo/config.toml
:
[env]
INSIGHTFACE_PATH = "<path to>/insightface/cpp-package/inspireface/build/inspireface-<arch>/InspireFace"
Then you can link the library using the build.rs
example below:
fn main() {
if let Ok(path) = std::env::var("INSIGHTFACE_PATH") {
let lib_path = format!("{}/lib", path);
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_path);
}
}
Thereafter the library can be add as follows in your Cargo.toml
:
[dependencies]
baklava = "0.1.4"
Below is a simple example of how to use the library:
// Should you have a list of images greater than 10. You can pass a chunk_size parameter in order to perform the image preparation concurrently.
let (cosine, percentage) = InsightFace::new("<model>", None)?
.prepare_images(&[
"./input1.png",
"./input2.png",
])?
.prepare_target_image("./target_image.png")?
.compare_images(Methodology::Mean)?;
The example can be run by executing the following command:
cargo run --example compare
Important
This guide requires you to replace the build.rs
by the build_local.rs
. This build_local.rs
will build the project locally and copy the resulting dylib
into the target directory.
- Clone & Initialize submodules
git clone --recurse-submodules https://github.com/shigedangao/baklava.git
# or
git submodule update --init --recursive
- Initialize insightface submodules dependencies
cd insightface/cpp-package/inspireface && git clone --recurse-submodules https://github.com/tunmx/inspireface-3rdparty.git 3rdparty
-
Replace the
build.rs
by thebuild_local.rs
. Thisbuild_local.rs
will build the project locally and copy the resultingdylib
into the target directory. -
Build the project
cargo build
- Make sure that everything works fine by running the unit tests with
cargo test
If you wish to recompile the InsightFace - rm -rf ./insightface/cpp-package/inspireface/build
These includes error such as inspireface.h
not found etc...
The linking between the InsightFace and Baklava is done through the build.rs
script. It automatically build with the targeted architecture and copies the resulting dylib
into the target directory. Should you encounter any issues try to pass the DYLIB_INCLUDE_PATH
variable like below.
DYLD_LIBRARY_PATH=./insightface/cpp-package/inspireface/build/inspire-{arch}/InspireFace/lib cargo build
Tip
If the inspireface.h
could still not be found. Then you may try to build the InsightFace library by yourself. You can run the following command within the InsightFace directory.
cd insightface/cpp-package/inspireface
bash command/build/sh
This will build a dynamic library your current architecture within the build/inspire-{arch}/InspireFace/lib
directory.