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:
Figure 1. facecompare result image
Quick Function Reference
The following table lists all the functions defined in the vitis::ai::FaceFeature
class:
Type | Name | Arguments |
---|---|---|
std::unique_ptr< FaceFeature > |
create |
|
std::unique_ptr< FaceFeature > |
create |
|
int | getInputWidth |
|
int | getInputHeight |
|
size_t | get_input_batch |
|
FaceFeatureFloatResult
|
run |
|
FaceFeatureFixedResult
|
run_fixed |
|
std::vector< FaceFeatureFloatResult > |
run |
|
std::vector< FaceFeatureFixedResult > |
run_fixed |
|