Skip to content

shigedangao/baklava

Repository files navigation

Baklava 🍮

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

Usage from crates.io

Important

This guide required you to download the insightface library and to follow the instructions in orders.

  1. 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

  2. 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
  1. Specify the path to the insightface library in your Cargo.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)?;

Running example

The example can be run by executing the following command:

cargo run --example compare

Local usage

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.

  1. Clone & Initialize submodules
git clone --recurse-submodules https://github.com/shigedangao/baklava.git

# or

git submodule update --init --recursive
  1. Initialize insightface submodules dependencies
cd insightface/cpp-package/inspireface && git clone --recurse-submodules https://github.com/tunmx/inspireface-3rdparty.git 3rdparty
  1. 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.

  2. Build the project

cargo build
  1. Make sure that everything works fine by running the unit tests with
cargo test

Recompile insightface

If you wish to recompile the InsightFace - rm -rf ./insightface/cpp-package/inspireface/build

Compile issues

Dynamic library not found

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.

About

A rust crates that wrap the InsightFace / InspireFace library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages