# Face Recognition SDK - Server

We implemented `face recognition SDK` on `docker` container

{% hint style="info" %}
We provide the Face Recognition SDK for both Windows and Linux.
{% endhint %}

## Features

* [x] Face Detection
* [x] Face Recognition
* [x] Pose Estimation
* [x] 68 points Face Landmark Detection
* [x] Face Quality Calculation
* [x] Face Occlusion Detection
* [x] Eye Closure Detection
* [x] Mouth Opening Check

## License

We offer `lifetime licenses(perpetual)` based on `machine ID` for Servers (Windows, Linux).

To request a license, please contact us:

> **Email:** <contact@kby-ai.com>&#x20;

{% embed url="<https://wa.me/+19092802609>" %}

{% embed url="<https://t.me/kbyaisupport>" %}

{% embed url="<https://discord.gg/6wm383re2s>" %}

## System Requirements

### 1. Windows

* **CPU:** 2 cores or more (Recommended: 2 cores)
* **RAM:** 4 GB or more (Recommended: 8 GB)
* HDD: 4 GB or more (Recommended: 8 GB)
* **OS:** Windows 7 or later
* **Architecture:** x64
* Dependency: OpenVINO™ Runtime (Version: 2022.3), ncnn Runtime(20220721), Vulkan SDK Runtime(1.3.250)

### 2. Linux

* **CPU:** 2 cores or more (Recommended: 2 cores)
* **RAM:** 4 GB or more (Recommended: 8 GB)
* HDD: 4 GB or more (Recommended: 8 GB)
* **OS:** Ubuntu 20.04 or later
* **Architecture:** x64
* Dependency: OpenVINO™ Runtime (Version: 2022.3)

## Import SDK

1. Python

```python
from facesdk import getMachineCode
from facesdk import setActivation
from facesdk import initSDK
from facesdk import faceDetection
from facesdk import templateExtraction
from facesdk import similarityCalculation
from facebox import FaceBox
```

2. C++

```cpp
#include "facesdk.h"
```

```cmake
# CMake for Windows
target_link_libraries(your_app_name
    /path/to/library/facesdk2.lib
)

# CMake for Linux
target_link_libraries(your_app_name
    /path/to/library/libfacesdk2.so
)
```

## Initializing SDK

1. Step one

* First, obtain the machine code for activation and request a license based on the `machine code`.

```python
# Python code example

machineCode = getMachineCode()
print("machineCode: ", machineCode.decode('utf-8'))
```

```cpp
// C++ code example

printf("machine code: %s\n", getMachineCode());
```

2. Step Two

* Next, activate the SDK using the received license.

```python
# Python code example

setActivation(license.encode('utf-8'))
```

```cpp
// C++ code example

int ret = setActivation("...");
```

* If activation is successful, the return value will be `SDK_SUCCESS`. Otherwise, an error value will be returned.

3. Step Three

* After activation, call the initialization function of the SDK.

```python
# Python code example

initSDK("data".encode('utf-8'))
```

```cpp
// C++ code example

ret = initSDK("data");
```

* The first parameter is the path to the model.
* If initialization is successful, the return value will be `SDK_SUCCESS`. Otherwise, an error value will be returned.

## Enums and Structure

### 1. SDK\_ERROR

This enumeration represents the return value of the `initSDK` and `setActivation` functions.

<table><thead><tr><th width="349.3333333333333">Feature</th><th width="99">Value</th><th>Name</th></tr></thead><tbody><tr><td>Successful activation or initialization</td><td>0</td><td>SDK_SUCCESS</td></tr><tr><td>License key error</td><td>-1</td><td>SDK_LICENSE_KEY_ERROR</td></tr><tr><td>AppID error (Not used in Server SDK)</td><td>-2</td><td>SDK_LICENSE_APPID_ERROR</td></tr><tr><td>License expiration</td><td>-3</td><td>SDK_LICENSE_EXPIRED</td></tr><tr><td>Not activated</td><td>-4</td><td>SDK_NO_ACTIVATED</td></tr><tr><td>Failed to initialize SDK</td><td>-5</td><td>SDK_INIT_ERROR</td></tr></tbody></table>

### 2. FaceBox

This class represents the output of the `face detection` function that contains the detected `face rectangle`, `liveness score`, and `facial angle`s such as `yaw`, `roll`, and `pitch`.

| Feature                   | Type                  | Name                                  |
| ------------------------- | --------------------- | ------------------------------------- |
| Face rectangle            | int                   | x1, y1, x2, y2                        |
| Face angles (-45 \~ 45)   | float                 | yaw, roll, pitch                      |
| Face quality (0 \~ 1)     | float                 | face\_quality                         |
| Face luminance (0 \~ 255) | float                 | face\_luminance                       |
| Eye distance (pixels)     | float                 | eye\_dist                             |
| Eye closure (0 \~ 1)      | float                 | left\_eye\_closed, right\_eye\_closed |
| Face occlusion (0 \~ 1)   | float                 | face\_occlusion                       |
| Mouth opening (0 \~ 1)    | float                 | mouth\_opened                         |
| 68 points facial landmark | float\[68 \* 2]       | landmarks\_68                         |
| Face templates            | unsigned char \[2048] | templates                             |

<div align="center"><figure><img src="/files/38uZmYjhaq9Otsydt36L" alt="" width="524"><figcaption><p>Face angles</p></figcaption></figure></div>

<figure><img src="/files/AZMEFeaDD2GggOCW0ct6" alt="" width="563"><figcaption><p>68 points facial landmark</p></figcaption></figure>

## APIs

### 1. getMachineCode

First, obtain the `machine code` for activation and request a license based on the `machine code`.

```python
# Python code example

machineCode = getMachineCode()
print("machineCode: ", machineCode.decode('utf-8'))
```

```cpp
// C++ code example

printf("machine code: %s\n", getMachineCode());
```

### 2. setActivation

Next, activate the SDK using the received license.

```python
# Python code example

setActivation(license.encode('utf-8'))
```

```cpp
// C++ code example

int ret = setActivation("...");
```

If activation is successful, the return value will be`SDK_SUCCESS`. Otherwise, an error value will be returned.

### 3. initSDK

After activation, call the initialization function of the SDK.

```python
# Python code example

initSDK("data".encode('utf-8'))
```

```cpp
// C++ code example

ret = initSDK("data");
```

The first parameter is the path to the model.

{% hint style="warning" %}
When using Windows, it is necessary to provide the complete file path for the model.

Example:&#x20;

model\_path = "C:\path\to\model"
{% endhint %}

If initialization is successful, the return value will be `SDK_SUCCESS`. Otherwise, an error value will be returned.

### 4. faceDetection

The `Face SDK` provides a single `API` for detecting `faces`, performing `liveness detection`, determining `face orientation (yaw, roll, pitch)`, assessing `face quality`, detecting `facial occlusion`, `eye closure`, `mouth opening`, and identifying `facial landmark`s.

The function can be used as follows:

```python
# Python code example

faceBoxes = (FaceBox * maxFaceCount)()
faceCount = faceDetection(image_np, image_np.shape[1], image_np.shape[0], faceBoxes, maxFaceCount)
```

```cpp
// C++ code example

FaceBox* faceBoxes = (FaceBox*)malloc(sizeof(FaceBox) * maxFaceCount);
memset(faceBoxes, 0, sizeof(FaceBox) * maxFaceCount);

ret = faceDetection(image.data, image.cols, image.rows, faceBoxes, maxFaceCount);
```

This function requires 5 parameters.

* The first parameter: the byte array of the `RGB image` buffer.
* The second parameter: the width of the image.
* The third parameter: the height of the image.
* The fourth parameter: the `FaceBox` array allocated with `maxFaceCount` for storing the detected faces.
* The fifth parameter: the count allocated for the maximum `FaceBox` objects.

The function returns the count of the detected face.

### 5. templateExtraction

The SDK provides a function that enables the generation of `template`s from `RGB data`. These `templates` can be used for face verification between two faces.

The function can be used as follows:

```python
# Python code example
faceBoxes = (FaceBox * maxFaceCount)()

...

templateExtraction(image_np1, image_np1.shape[1], image_np1.shape[0], faceBoxes[0])
```

```cpp
// C++ code example

FaceBox* faceBoxes = (FaceBox*)malloc(sizeof(FaceBox) * maxFaceCount);
memset(faceBoxes, 0, sizeof(FaceBox) * maxFaceCount);

...

templateExtraction(image.data, image.cols, image.rows, faceBoxes[0]);
```

This function requires 4 parameters.

* The first parameter: the byte array of the `RGB image buffer`.
* The second parameter: the width of the image.
* The third parameter: the height of the image.
* The fourth parameter: the `FaceBox` object obtained from the `faceDetection` function.

If the template extraction is successful, the function will return `0`. Otherwise, it will return `-1`.

### 6. similarityCalculation

The `similarityCalculation` function takes a byte array of two `templates` as a parameter.

```python
# Python code example

similarity = similarityCalculation(faceBoxes1[0].templates, faceBoxes2[0].templates)
```

```cpp
// C++ code example

FaceBox* faceBoxes = (FaceBox*)malloc(sizeof(FaceBox) * maxFaceCount);
memset(faceBoxes, 0, sizeof(FaceBox) * maxFaceCount);

...

float similarity = similarityCalculation(faceBoxes[0].templates, faceBoxes[1].templates);
```

It returns the similarity value between the two `templates`, which can be used to determine the level of likeness between the two individuals.

## Default Thresholds

```python
verifyThreshold = 0.67 
```

* If the similarity value between the two templates is high, it indicates that the faces are the same.<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.kby-ai.com/help/product/face-liveness-detection-sdk-face-recognition-sdk/face-recognition-sdk-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
