dlib中 anet_type 已经废弃了 帮我写一段用dlib其他方法实现来替换 用c++
时间: 2023-11-28 15:47:20 浏览: 35
dlib 中废弃的 anet_type 是一个基于 ResNet34 的人脸识别模型,可以使用其他的 dlib 方法来进行替代。以下是一个使用 dlib 中的人脸检测和人脸特征提取方法来实现人脸识别的示例代码:
```c++
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <dlib/image_processing.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/dnn.h>
#include <iostream>
#include <fstream>
using namespace dlib;
using namespace std;
using namespace cv;
// 人脸检测器
frontal_face_detector detector = get_frontal_face_detector();
// 人脸关键点检测器
shape_predictor sp;
// 加载预训练好的人脸关键点检测器模型
deserialize("shape_predictor_68_face_landmarks.dat") >> sp;
// 人脸特征提取器
anet_type net;
// 加载预训练好的人脸特征提取模型
deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net;
// 计算两张人脸的相似度
double face_distance(const matrix<float,0,1>& a, const matrix<float,0,1>& b)
{
return length(a-b);
}
// 读取图像文件
matrix<rgb_pixel> load_image(const string& filename)
{
matrix<rgb_pixel> img;
load_image(img, filename);
return img;
}
int main(int argc, char** argv)
{
// 读取两张图像文件
matrix<rgb_pixel> img1 = load_image("face1.jpg");
matrix<rgb_pixel> img2 = load_image("face2.jpg");
// 检测人脸并提取人脸特征
std::vector<matrix<rgb_pixel>> faces1;
std::vector<matrix<rgb_pixel>> faces2;
for (auto face : detector(img1))
{
auto shape = sp(img1, face);
matrix<rgb_pixel> face_chip;
extract_image_chip(img1, get_face_chip_details(shape,150,0.25), face_chip);
faces1.push_back(move(face_chip));
}
for (auto face : detector(img2))
{
auto shape = sp(img2, face);
matrix<rgb_pixel> face_chip;
extract_image_chip(img2, get_face_chip_details(shape,150,0.25), face_chip);
faces2.push_back(move(face_chip));
}
if (faces1.empty() || faces2.empty())
{
cout << "No faces detected." << endl;
return 0;
}
// 计算人脸特征的平均值
matrix<float,0,1> face1 = mean(mat(net(faces1)));
matrix<float,0,1> face2 = mean(mat(net(faces2)));
// 计算两张人脸的相似度
double distance = face_distance(face1, face2);
cout << "Distance between faces: " << distance << endl;
return 0;
}
```
这段代码中,我们使用了 dlib 中的 frontal_face_detector 进行人脸检测,使用了 shape_predictor 进行人脸关键点检测,使用了 anet_type 进行人脸特征提取。其中,anet_type 已经被废弃,我们使用了 dlib 中的 mat 函数将人脸特征提取器的输出转换为一个矩阵,并使用 mean 函数计算了多张人脸特征的平均值,从而得到了两张人脸的相似度。