人脸识别系统开发(9) -- Dlib人脸比对

这里的人脸识别准确的说是人脸比对,特征点比对。opencv是基于机器学习的,需要使用素材进行训练,不符合该系统的要求。所以这里使用dlib来实现。

从dlib官方网站http://dlib.net/下载源码,使用cmake生成visual stuido工程,然后编译。

编译dlib可能需要安装mkl,mkl下载地址:https://pan.baidu.com/s/1qYHriKs 密码:wl6z

使用dlib进行人脸图片比对的大致流程如下(代码摘要):

#include 
#include 
#include 
#include 
#include 
#include 
#include 

template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual = add_prev11, tag1>>;

template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = add_prev22, 2, 2, 2, skip12, tag1>>>>>;

template <int N, template <typename> class BN, int stride, typename SUBNET>
using block = BN3, 3, 1, 1, relu3, 3, stride, stride, SUBNET>>>>>;

template <int N, typename SUBNET> using ares = relu>;
template <int N, typename SUBNET> using ares_down = relu>;

template <typename SUBNET> using alevel0 = ares_down<256, SUBNET>;
template <typename SUBNET> using alevel1 = ares<256, ares<256, ares_down<256, SUBNET>>>;
template <typename SUBNET> using alevel2 = ares<128, ares<128, ares_down<128, SUBNET>>>;
template <typename SUBNET> using alevel3 = ares<64, ares<64, ares<64, ares_down<64, SUBNET>>>>;
template <typename SUBNET> using alevel4 = ares<32, ares<32, ares<32, SUBNET>>>;

using anet_type = loss_metric128, avg_pool_everything<
    alevel0<
    alevel1<
    alevel2<
    alevel3<
    alevel4<
    max_pool<3, 3, 2, 2, relu32, 7, 7, 2, 2,
    input_rgb_image_sized<150>
    >>>>>>>>>>>>;

// 声明anet_type
anet_type net;

// 使用dlib_face_recognition_resnet_model_v1.data初始化anet_type
//
QString strDataPath = QCoreApplication::applicationDirPath() + "/data/dlib_face_recognition_resnet_model_v1.dat";
try {
    deserialize(cpp4j::Utf8ToAnsi(strDataPath.toStdString())) >> net;
}
catch (serialization_error &e) {
    qDebug() << "DlibRecognize deserialize failed: " << strDataPath;
    m_bInit = false;
}

// 这里将需要对比的2个cv::Mat存储到文件中
//
QString str = QCoreApplication::applicationDirPath() + "/HFR_FACE1.jpg";
std::string strTmpFace1 = cpp4j::Utf8ToAnsi(str.toStdString());

str = QCoreApplication::applicationDirPath() + "/HFR_FACE2.jpg";
std::string strTmpFace2 = cpp4j::Utf8ToAnsi(str.toStdString());

// 使2个图片的尺寸相同
// opencv获取到的人脸的长宽始终1:1的,所以伸缩图片不会变形。
//
cv::Size faceSize = cv::Size(150, 150);
cv::resize(m_Face1, m_Face1, faceSize);
cv::resize(m_Face2, m_Face2, faceSize);

cv::imwrite(strTmpFace1, m_Face1);
cv::imwrite(strTmpFace2, m_Face2);

matrix img1;
matrix img2;
load_image(img1, strTmpFace1);
load_image(img2, strTmpFace2);

std::vector> faces;
faces.push_back(img1);
faces.push_back(img2);

std::vectorfloat, 0, 1>> face_descriptors = net(faces);

// 比对结果是一个距离值
float f = length(face_descriptors[0] - face_descriptors[1]);
qDebug() << "DlibRecognize VALUE: " << f;

完整代码见DlibRecognize.cpp文件.
项目地址:https://gitee.com/china_jeffery/HFR_OpenSource

你可能感兴趣的:(☆,Qt,☆,人脸识别,人脸识别系统开发)