您的位置:首页 > 其它

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

2018-01-11 15:36 681 查看
这里的人脸识别准确的说是人脸比对,特征点比对。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;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
[/code]

完整代码见
DlibRecognize.cpp
文件.

项目地址:https://gitee.com/china_jeffery/HFR_OpenSource
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  人脸识别