vitis::ai::FaceFeature - 3.5 English

Vitis AI Library User Guide (UG1354)

Document ID
UG1354
Release Date
2023-06-29
Version
3.5 English
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);
  }

 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;
 }

 float score_map_l20(float score) { return 1.0 / (1 + exp(-12.4 * score
+ 3.763)); }

 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: width=\textwidth

Figure 1. facecompare result image
facecompare result image

Quick Function Reference

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

Table 1. Quick Function Reference
Type Member Arguments
std::unique_ptr< FaceFeature > create
  • const std::string & model_name
  • bool need_preprocess
std::unique_ptr< FaceFeature > create
  • const std::string & model_name
  • xir::Attrs * attrs
  • bool need_preprocess
int getInputWidth
  • void
int getInputHeight
  • void
size_t get_input_batch
  • void
FaceFeatureFloatResult run
  • const cv::Mat & img
FaceFeatureFixedResult run_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