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

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

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

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

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

#include <dlib/dnn.h>
#include <dlib/gui_widgets.h>
#include <dlib/clustering.h>
#include <dlib/string.h>
#include <dlib/image_io.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/opencv.h>

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

template <template <int, template<typename>class, int, typename> class block, int N, template<typename>class BN, typename SUBNET>
using residual_down = add_prev2<avg_pool<2, 2, 2, 2, skip1<tag2<block<N, BN, 2, tag1<SUBNET>>>>>>;

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

template <int N, typename SUBNET> using ares = relu<residual<block, N, affine, SUBNET>>;
template <int N, typename SUBNET> using ares_down = relu<residual_down<block, N, affine, SUBNET>>;

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_metric<fc_no_bias<128, avg_pool_everything<
    alevel0<
    alevel1<
    alevel2<
    alevel3<
    alevel4<
    max_pool<3, 3, 2, 2, relu<affine<con<32, 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<rgb_pixel> img1;
matrix<rgb_pixel> img2;
load_image(img1, strTmpFace1);
load_image(img2, strTmpFace2);

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

std::vector<matrix<float, 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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值