此基本类用于获取面部图像 (cv::Mat) 的特征。
输入为面部图像 (cv::Mat)。
输出是输入图像中面部的特征。
浮动样本代码:
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;
固定样本代码:
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;
相似性计算公式:
计算两张图像的相似度:
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);
固定比较代码:
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)); }
显示一组图像的比较结果:width=\textwidth
图 1. facecompare 结果图像
函数快速参考
下表列出了 vitis::ai::FaceFeature
类中定义的所有函数:
类型 | 成员 | 实参 |
---|---|---|
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 |
|