vitis::ai::FaceFeature

Base class for getting the features of a face image (cv::Mat).

Input is a face image (cv::Mat).

Output is the features of a face in the input image.

Float sample code :

Note: Two interfaces are provided to get the float features or fixed features. They return FaceFeatureFloatResult or FaceFeatureFixedResult.
cv:Mat image = cv::imread("test_face.jpg");
auto network  = vitis::ai::FaceFeature::create("facerec_resnet20", true);
auto result = network->run(image);
auto features = result.feature;

Fixed sample code :

cv:Mat image = cv::imread("test_face.jpg");
auto network  = vitis::ai::FaceFeature::create("facerec_resnet20", true);
auto result = network->run_fixed(image);
auto features = result.feature;

Similarity calculation formula :

Calaculate the similarity of two images:

auto result_fixed = network->run_fixed(image);
auto result_fixed2 = network->run_fixed(image2);
auto similarity_original = feature_compare(result_fixed.feature->data(),
                                 result_fixed2.feature->data());
float similarity_mapped = score_map(similarity_original);

Fixed compare code :

  float feature_norm(const int8_t *feature) {
     int sum = 0;
     for (int i = 0; i < 512; ++i) {
         sum += feature[i] * feature[i];
     }
     return 1.f / sqrt(sum);
  }

 /// This function is used for computing dot product of two vector
 static float feature_dot(const int8_t *f1, const int8_t *f2) {
    int dot = 0;
    for (int i = 0; i < 512; ++i) {
       dot += f1[i] * f2[i];
    }
    return (float)dot;
 }

 float feature_compare(const int8_t *feature, const int8_t *feature_lib){
    float norm = feature_norm(feature);
    float feature_norm_lib = feature_norm(feature_lib);
    return feature_dot(feature, feature_lib) * norm * feature_norm_lib;
 }

 /// This function is used for model "facerec_resnet20"
 float score_map_l20(float score) { return 1.0 / (1 + exp(-12.4 * score
+ 3.763)); }

 /// This function is used for type "facerec_resnet64"
 float score_map_l64(float score) { return 1.0 / (1 + exp(-17.0836 * score
+ 5.5707)); }

Display of the compare result with a set of images:

Figure 1: facecompare result image

Image sample_facecompare_result.jpg

Quick Function Reference

The following table lists all the functions defined in the vitis::ai::FaceFeature class:

Table 1. Quick Function Reference
TypeNameArguments
std::unique_ptr< FaceFeature >create
  • const std::string & model_name
  • bool need_preprocess
std::unique_ptr< FaceFeature >create
  • void
intgetInputWidth
  • void
intgetInputHeight
  • void
size_tget_input_batch
  • void
FaceFeatureFloatResultrun
  • const cv::Mat & img
FaceFeatureFixedResultrun_fixed
  • const cv::Mat & img
std::vector< FaceFeatureFloatResult >run
  • const std::vector< cv::Mat > & imgs
std::vector< FaceFeatureFixedResult >run_fixed
  • const std::vector< cv::Mat > & imgs

create

Factory function to get an instance of derived classes of class FaceFeature.

Prototype

std::unique_ptr< FaceFeature > create(const std::string &model_name, bool need_preprocess=true);

Parameters

The following table lists the create function arguments.

Table 2. create Arguments
Type Name Description
const std::string & model_name Model name
bool need_preprocess Normalize with mean/scale or not, default value is true.

Returns

An instance of FaceFeature class.

create

Prototype

std::unique_ptr< FaceFeature > create(const std::string &model_name, xir::Attrs *attrs, bool need_preprocess=true);

getInputWidth

Function to get InputWidth of the feature network (input image columns).

Prototype

int getInputWidth() const =0;

Returns

InputWidth of the feature network.

getInputHeight

Function to get InputHeight of the feature network (input image rows).

Prototype

int getInputHeight() const =0;

Returns

InputHeight of the feature network.

get_input_batch

Function to get the number of images processed by the DPU at one time.

Note: Different DPU core the batch size may be different. This depends on the IP used.

Prototype

size_t get_input_batch() const =0;

Returns

Batch size.

run

Function of get running result of the feature network.

Prototype


            FaceFeatureFloatResult run(const cv::Mat &img)=0;

Parameters

The following table lists the run function arguments.

Table 3. run Arguments
Type Name Description
const cv::Mat & img Input data for image (cv::Mat) detected by the facedetect network and then rotated and aligned.

Returns

FaceFeatureFloatResult

run_fixed

Function of get running result of the feature network.

Prototype


            FaceFeatureFixedResult run_fixed(const cv::Mat &img)=0;

Parameters

The following table lists the run_fixed function arguments.

Table 4. run_fixed Arguments
Type Name Description
const cv::Mat & img Input data for image (cv::Mat) detected by the facedetect network and then rotated and aligned.

Returns

FaceFeatureFixedResult

run

Function of get running result of the feature network in batch mode.

Prototype

std::vector< FaceFeatureFloatResult > run(const std::vector< cv::Mat > &imgs)=0;

Parameters

The following table lists the run function arguments.

Table 5. run Arguments
Type Name Description
const std::vector< cv::Mat > & imgs Input data of batch input images (vector<cv::Mat>) detected by the facedetect network and then rotated and aligned. The size of input images equals batch size obtained by get_input_batch.

Returns

The vector of FaceFeatureFloatResult.

run_fixed

Function of get running result of the feature network in batch mode.

Prototype

std::vector< FaceFeatureFixedResult > run_fixed(const std::vector< cv::Mat > &imgs)=0;

Parameters

The following table lists the run_fixed function arguments.

Table 6. run_fixed Arguments
Type Name Description
const std::vector< cv::Mat > & imgs Input data of batch input images (vector<cv::Mat>) detected by the facedetect network and then rotated and aligned. The size of input images equals batch size obtained by get_input_batch.

Returns

The vector of FaceFeatureFixedResult.