opencv dnn 和dlib人脸检测
时间: 2023-08-14 15:06:37 浏览: 59
OpenCV DNN和Dlib都可以用于人脸检测,但它们的实现方式略有不同。
OpenCV DNN是基于深度学习的方法,使用神经网络模型进行人脸检测。在OpenCV DNN中,可以使用基于Caffe、TensorFlow和Darknet等深度学习框架训练的模型进行人脸检测。这些模型通常具有更高的准确性和更快的检测速度。
Dlib则是基于传统的机器学习方法进行人脸检测的。它使用了一种叫做HOG特征的算法来检测人脸。在Dlib中,可以使用训练好的分类器进行人脸检测。虽然Dlib的准确性较低,但它对于一些特殊场景下的人脸检测效果不错。
因此,选择使用哪种方法进行人脸检测,要根据具体的应用场景和需求来进行选择。
相关问题
c++ dlib + opencv + facenat 进行人脸比对
人脸比对可以通过dlib和OpenCV库来实现。首先,你需要使用OpenCV库来加载图像并检测人脸。然后,你可以使用dlib库中的人脸识别模型来计算人脸的128位向量,这个向量可以表示人脸的独特特征。最后,你可以使用facenet模型将这些特征向量进行比对,以判断两个人脸是否相同。
以下是一个基本的示例代码:
```c++
#include <dlib/opencv.h>
#include <dlib/image_processing.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/dnn.h>
#include <opencv2/opencv.hpp>
using namespace dlib;
using namespace std;
// 加载facenet模型
net_type net = dlib::deserialize("models/facenet.dat");
// 计算人脸特征向量
matrix<float,0,1> get_face_descriptor(cv::Mat img, frontal_face_detector detector, shape_predictor sp) {
cv_image<bgr_pixel> cimg(img);
std::vector<rectangle> faces = detector(cimg);
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i) {
shapes.push_back(sp(cimg, faces[i]));
}
matrix<rgb_pixel> face_chip;
extract_image_chip(cimg, get_face_chip_details(shapes[0]), face_chip);
matrix<float,0,1> face_descriptor = net(face_chip);
return face_descriptor;
}
// 计算两个人脸特征向量的距离
double face_distance(matrix<float,0,1> &face_descriptor1, matrix<float,0,1> &face_descriptor2) {
return length(face_descriptor1 - face_descriptor2);
}
int main(int argc, char** argv) {
// 加载人脸检测模型和特征点定位模型
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor sp;
deserialize("models/shape_predictor_68_face_landmarks.dat") >> sp;
// 加载两张需要比对的人脸图片
cv::Mat img1 = cv::imread(argv[1]);
cv::Mat img2 = cv::imread(argv[2]);
// 计算两个人脸的特征向量并计算距离
matrix<float,0,1> face_descriptor1 = get_face_descriptor(img1, detector, sp);
matrix<float,0,1> face_descriptor2 = get_face_descriptor(img2, detector, sp);
double distance = face_distance(face_descriptor1, face_descriptor2);
// 输出结果
cout << "Distance: " << distance << endl;
return 0;
}
```
你需要将上面的代码保存为一个cpp文件,并且需要下载facenet模型和dlib的人脸检测模型和特征点定位模型。你可以在dlib的官方网站上下载这些模型,然后将它们放在一个名为models的文件夹中。
c++ dlib + opencv + facenat 进行人脸比对 程序实现
人脸比对是指将两张人脸图像进行比较,判断它们是否是同一个人。利用dlib、opencv和facenet可以实现人脸比对的功能。
首先,需要用dlib进行人脸检测和对齐,再利用opencv将人脸图像转换为facenet所需的格式,最后使用facenet进行人脸特征提取和比对。
以下是一个简单的c++程序,实现了两张人脸图像的比对:
```c++
#include <iostream>
#include <dlib/opencv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "dlib/image_processing/frontal_face_detector.h"
#include "dlib/image_processing.h"
#include "dlib/dnn.h"
using namespace std;
using namespace dlib;
// 人脸对齐
void align_face(cv::Mat& img, full_object_detection& shape)
{
// 人脸对齐点
const int left_eye_points[6] = {36, 37, 38, 39, 40, 41};
const int right_eye_points[6] = {42, 43, 44, 45, 46, 47};
// 获取左右眼平均坐标
cv::Point2d left_eye(0, 0);
cv::Point2d right_eye(0, 0);
for (int i = 0; i < 6; i++) {
left_eye.x += shape.part(left_eye_points[i]).x();
left_eye.y += shape.part(left_eye_points[i]).y();
right_eye.x += shape.part(right_eye_points[i]).x();
right_eye.y += shape.part(right_eye_points[i]).y();
}
left_eye.x /= 6;
left_eye.y /= 6;
right_eye.x /= 6;
right_eye.y /= 6;
// 计算旋转角度
double angle = atan2(right_eye.y - left_eye.y, right_eye.x - left_eye.x) * 180 / M_PI;
// 计算缩放比例
double scale = 1.0 / (right_eye.x - left_eye.x);
// 构造仿射变换矩阵
cv::Mat rot_mat = cv::getRotationMatrix2D(left_eye, angle, scale);
cv::Mat warp_dst;
cv::warpAffine(img, warp_dst, rot_mat, img.size());
// 截取对齐后的人脸
int x = left_eye.x - (right_eye.x - left_eye.x);
int y = left_eye.y - (right_eye.y - left_eye.y) / 2;
int w = right_eye.x - left_eye.x + (right_eye.x - left_eye.x);
int h = right_eye.y - left_eye.y + (right_eye.y - left_eye.y) / 2;
cv::Mat aligned_face = warp_dst(cv::Rect(x, y, w, h));
img = aligned_face.clone();
}
// 人脸比对
double face_compare(cv::Mat& img1, cv::Mat& img2)
{
// 加载人脸检测器和关键点检测器
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor sp;
deserialize("shape_predictor_68_face_landmarks.dat") >> sp;
// 检测人脸并对齐
std::vector<rectangle> faces1 = detector(img1);
std::vector<rectangle> faces2 = detector(img2);
if (faces1.empty() || faces2.empty()) {
return -1.0;
}
full_object_detection shape1 = sp(img1, faces1[0]);
full_object_detection shape2 = sp(img2, faces2[0]);
align_face(img1, shape1);
align_face(img2, shape2);
// 转换图像格式
matrix<rgb_pixel> face1;
matrix<rgb_pixel> face2;
assign_image(face1, cv_image<rgb_pixel>(img1));
assign_image(face2, cv_image<rgb_pixel>(img2));
// 加载facenet模型
anet_type net;
deserialize("dlib_face_recognition_resnet_model_v1.dat") >> net;
// 提取人脸特征向量
std::vector<matrix<float,0,1>> face1_feats = net(face1);
std::vector<matrix<float,0,1>> face2_feats = net(face2);
// 计算欧氏距离
double distance = length(face1_feats[0] - face2_feats[0]);
return distance;
}
int main()
{
// 加载图片
cv::Mat img1 = cv::imread("face1.jpg");
cv::Mat img2 = cv::imread("face2.jpg");
// 人脸比对
double distance = face_compare(img1, img2);
// 输出结果
if (distance >= 0) {
cout << "distance: " << distance << endl;
}
else {
cout << "no face detected" << endl;
}
return 0;
}
```
其中,`align_face`函数用于人脸对齐,`face_compare`函数用于人脸比对。两个函数都使用了dlib和opencv库。
在这个程序中,我们使用了预训练好的人脸检测器、关键点检测器和facenet模型,它们都可以在dlib官网上下载。如果你想实现更高效的人脸比对,可以考虑使用GPU加速。