Connection to the project
To start using Face SDK, you need to add and use the libfacerec library in your project. libfacerec is the key Face SDK's library that implements the main Face SDK's functionality, including detection, attribute estimation and identification/verification.
Add the libfacerec library to the project
C++
The libfacerec library is loaded dynamically in runtime mode when the static method pbio::FacerecService::createService
is called. Therefore, you don't need to link your program with libfacerec.so (Linux) or facerec.dll (Windows). Only add the include directory to header file directories.
All libfacerec declarations are divided in two headers:
- facerec/import.h : must be included only in one project file (for example, in main.cpp).
- facerec/libfacerec.h : must be included in each file that uses a library.
The libfacerec library doesn't have any actual third-party dependencies*, since all used third-party libraries (see Open source licenses]) are linked statically. Therefore, any version of these libraries must work for examples, in which OpenCV or boost is used.
* – except for the linux x86_64 and windows_x86_64 distribution packages when GPU/AVX2 acceleration is used. In this case, a dependency on libtensorflow.so.2, libonnxruntime.so or tensorflow.dll, onnxruntime.dll is used respectively.
Library version
You can get the current library version by calling the get_version
method from the created FacerecService
object. This method will return a string in the format of x.yy.zz
, where x
is the major version, yy
is the minor version, and zz
is the patch version.
In C++ the version can be additionally obtained from the preprocessor constants:
LIBFACEREC_VERSION
: string representation, similar toFacerecService.get_version()
LIBFACEREC_VERSION_HEX
: 3-byte HEX representation that is useful in numeric comparisons, e.g.0x010234
For more information on how Face SDK works in C++ projects, see a set of C++ samples.
Java
To use facerec.jar in a project, specify the path to this file in classpath
. The facerec.jar Java library is a wrapper for the C++ library, so libfacerec_jni.so and libfacerec.so for Linux and Android or facerec_jni.dll and facerec.dll for Windows are required in runtime. Path to the directory, which contains lifacerec_jni.so or facerec_jni.dll, must be specified in java.library.path
.
For more information on how Face SDK works in Java projects, see a set of Java samples.
C#
To use the library, you need to add a reference to FacerecCSharpWrapper.dll to your project.
After building the project, add FacerecCSharpWrapper.dll, and depending on the operating system, add facerec.dll (for Windows) or create a reference facerec.dll to libfacerec.so (for Linux) in the directory with the executable file.
For more information on how Face SDK works in C# projects, see a set of C# samples].
Python
Note: Python 3.6 or higher is supported. setuptools
package must be installed before starting the installation.
face_sdk_3divi
package allows you to use the libfacerec library in a Python project. To install the package, go to the python_api directory and run pip3 install .
For more information on how Face SDK works in Python projects, see a set of Python samples].
NodeJS
Note. Node v18.20.8 or higher is supported.
The package face_sdk_3divi
allows you to use the libfacerec library in a NodeJS project. To install the package, call npm install /path/to/FaceSDK/node_js
from your project.
Go
Supported platform: Linux only.
To build and run executables using the Go API, you need to set the following environment variables:
- CGO_LDFLAGS="-L<path_to_face_sdk_lib_directory> -lfacerec"
- LD_LIBRARY_PATH=<path_to_face_sdk_lib_directory>
The package facesdk
allows you to use the libfacerec library in a Go project. To use the package, add the following line in your code:
import (facesdk github.com/3DiVi/Go-API)
Use the libfacerec library
To use the libfacerec library, call the FacerecService.createService
method, which dynamically loads the library. The result of method operation is FacerecService
interface object,which is required to create other interface objects, including Capturers, Estimators and Recognizers.
Warning: You can call FacerecService.createService
only once, otherwise, you may encounter a crash.
To initialize the libfacerec library, call FacerecService.createService
method with the following arguments:
- dll_path: Absolute or relative path to the library file libfacerec.so (Linux) or facerec.dll (Windows).
- facerec_conf_dir: Absolute or relative path to the directory with config files (directory
face_sdk/conf/facerec/
in the distribution package). - license_dir: Absolute or relative path to the directory with the license, otherwise, an empty string. If the string is empty, the license file is searched first in the
face_sdk/license/
directory, then in theface_sdk/conf/facerec/
directory.
In NodeJS, you need to call the Init function from the face_sdk_3divi
package with the same parameters.
Example
This example shows initialization of the libfacerec library using FacerecService.createService
method.
- C++
- C#
- Java
- Python
- NodeJS
- Go
#include <iostream>
#include <exception>
#include <facerec/import.h>
#include <facerec/libfacerec.h>
int main (int argc, char** argv)
{
try
{
std::string faceSDKRootDir = "/path/to/face_sdk";
#ifdef _WIN32
std::string dll_path = faceSDKRootDir + "/bin/facerec.dll";
#else
std::string dll_path = faceSDKRootDir + "/lib/libfacerec.so";
#endif
std::string conf_dir = faceSDKRootDir + "/conf/facerec/";
std::string license_dir = faceSDKRootDir + "/license/";
pbio::FacerecService::Ptr service;
service = pbio::FacerecService::createService(dll_path, conf_dir, license_dir);
pbio::Context detectorConfig = service->createContext();
pbio::Context fitterConfig = service->createContext();
pbio::Context faceExtractorConfig = service->createContext();
detectorConfig["unit_type"] = "FACE_DETECTOR";
fitterConfig["unit_type"] = "FACE_FITTER";
faceExtractorConfig["unit_type"] = "FACE_TEMPLATE_EXTRACTOR";
pbio::ProcessingBlock faceDetector = service->createProcessingBlock(detectorConfig);
pbio::ProcessingBlock faceFitter = service->createProcessingBlock(fitterConfig);
pbio::ProcessingBlock faceExtractor = service->createProcessingBlock(faceExtractorConfig);
// ...
}
catch(const pbio::Error &e)
{
std::cerr << "facerec exception catched: '" << e.what() << "' code: " << std::hex << e.code() << std::endl;
}
catch(const std::exception &e)
{
std::cerr << "exception catched: '" << e.what() << "'" << std::endl;
}
}
using System;
using VDT.FaceRecognition.SDK;
namespace Example
{
public class Example
{
public static void Main(string []args)
{
try
{
String faceSDKRootDir = "/path/to/face_sdk";
String confDirPath = faceSDKRootDir + "/conf/facerec";
String licenseDirPath = faceSDKRootDir + "/license";
FacerecService service = FacerecService.createService(confDirPath, licenseDirPath);
Dictionary<object, object> detectorConfig = new();
Dictionary<object, object> fitterConfig = new();
Dictionary<object, object> faceExtractorConfig = new();
detectorConfig["unit_type"] = "FACE_DETECTOR";
fitterConfig["unit_type"] = "FACE_FITTER";
faceExtractorConfig["unit_type"] = "FACE_TEMPLATE_EXTRACTOR";
ProcessingBlock faceDetector = service.CreateProcessingBlock(detectorConfig);
ProcessingBlock faceFitter = service.CreateProcessingBlock(fitterConfig);
ProcessingBlock faceExtractor = service.CreateProcessingBlock(faceExtractorConfig);
// ...
}
catch (Error e)
{
Console.WriteLine(e.Message);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
package example;
import java.lang.String;
import java.lang.Exception;
import com.vdt.face_recognition.sdk.FacerecService;
import com.vdt.face_recognition.sdk.Recognizer;
import com.vdt.face_recognition.sdk.Capturer;
import com.vdt.face_recognition.sdk.SDKException;
public class Example
{
public static void main(String []args)
{
try
{
final String faceSDKRootDir = "/path/to/face_sdk";
final String dllPath = faceSDKRootDir + "/lib/libfacerec.so";
final String confDirPath = faceSDKRootDir + "/conf/facerec";
final String licenseDirPath = faceSDKRootDir + "/license";
final FacerecService service = FacerecService.createService(dllPath, confDirPath, licenseDirPath);
Context detectorConfig = service.createContext();
Context fitterConfig = service.createContext();
Context faceExtractorConfig = service.createContext();
detectorConfig.get("unit_type").setString("FACE_DETECTOR");
fitterConfig.get("unit_type").setString("FACE_FITTER");
faceExtractorConfig.get("unit_type").setString("FACE_TEMPLATE_EXTRACTOR");
ProcessingBlock faceDetector = service.createProcessingBlock(detectorConfig);
ProcessingBlock faceFitter = service.createProcessingBlock(fitterConfig);
ProcessingBlock faceExtractor = service.createProcessingBlock(faceExtractorConfig);
// ...
}
catch (SDKException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
from face_sdk_3divi import Config, FacerecService, Error
if __name__ == '__main__':
try:
face_sdk_root_dir = "path/to/face_sdk"
dll_path = face_sdk_root_dir + "/lib/libfacerec.so"
conf_dir_path = face_sdk_root_dir + "/conf/facerec"
license_dir = face_sdk_root_dir + "/license"
service = FacerecService.create_service(dll_path, conf_dir_path, license_dir)
detectorConfig = {"unit_type": "FACE_DETECTOR"}
fitterConfig = {"unit_type": "FACE_FITTER"}
faceExtractorConfig = {"unit_type": "FACE_TEMPLATE_EXTRACTOR"}
faceDetector = service.createProcessingBlock(detectorConfig);
faceFitter = service.createProcessingBlock(fitterConfig);
faceExtractor = service.createProcessingBlock(faceExtractorConfig);
# ...
except Error as e:
print(e)
const facerec = require('face_sdk_3divi');
function main()
{
try {
const sdk_path = "path/to/face_sdk";
const dll_path = sdk_path + "/lib/libfacerec.so";
const conf_dir_path = sdk_path + "/conf/facerec";
const license_dir_path = sdk_path + "/license";
facerec.Init(dll_path, conf_dir_path, license_dir_path);
let detector_config = new facerec.Context();
let fitter_config = new facerec.Context();
let extractor_config = new facerec.Context();
detector_config.get("unit_type").value = "FACE_DETECTOR";
fitter_config.get("unit_type").value = "FACE_FITTER";
extractor_config.get("unit_type").value = "FACE_TEMPLATE_EXTRACTOR";
} catch (err) {
console.log(err)
}
}
package main
import (
facesdk "github.com/3DiVi/Go-API"
)
func main() {
sdkPath := "path/to/face_sdk"
confPath := sdk_path + "/conf/facerec"
licensePath := sdk_path + "/license"
_, err := facesdk.CreateFacerecService(confPath, licensePath)
if err != nil {
panic(err)
}
detectorConfig, err := facesdk.CreateContext()
if err != nil {
panic(err)
}
defer detectorConfig.Close()
fitterConfig, err := facesdk.CreateContext()
if err != nil {
panic(err)
}
defer fitterConfig.Close()
extractorConfig, err := facesdk.CreateContext()
if err != nil {
panic(err)
}
defer extractorConfig.Close()
context, err := detectorConfig.GetOrInsertByKey("unit_type")
if err != nil {
panic(err)
}
err = context.SetString("FACE_DETECTOR")
if err != nil {
panic(err)
}
context, err = fitterConfig.GetOrInsertByKey("unit_type")
if err != nil {
panic(err)
}
err = context.SetString("FACE_FITTER")
if err != nil {
panic(err)
}
context, err = extractorConfig.GetOrInsertByKey("unit_type")
if err != nil {
panic(err)
}
}