效果
注意:Dlib检测人脸在Release版耗时与CPU有关,本人I7 10代约100ms左右。建议人脸检测可以考虑使用Yolov5进行,之后将检测到的人脸输入给Dlib做特征或其他。
代码
#include <iostream>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#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_transforms.h>
#include <dlib/opencv.h>
#include "opencv2/opencv.hpp"
void line_one_face_detections(cv::Mat &img, std::vector<dlib::full_object_detection> fs)
{
int i, j;
for (j = 0; j < fs.size(); j++)
{
cv::Point p1, p2;
for (i = 0; i < 67; i++)
{
//if (i != 48 && i != 64 && i != 38 && i != 43 && i != 29) { continue; }
// 下巴到脸颊 0 ~ 16
//左边眉毛 17 ~ 21
//右边眉毛 21 ~ 26
//鼻梁 27 ~ 30
//鼻孔 31 ~ 35
//左眼 36 ~ 41
//右眼 42 ~ 47
//嘴唇外圈 48 ~ 59
//嘴唇内圈 59 ~ 67
switch (i)
{
case 16:
case 21:
case 26:
case 30:
case 35:
case 41:
case 47:
case 59:
i++;
break;
default:
break;
}
p1.x = fs[j].part(i).x();
p1.y = fs[j].part(i).y();
p2.x = fs[j].part(i + 1).x();
p2.y = fs[j].part(i + 1).y();
cv::line(img, p1, p2, cv::Scalar(0, 0, 255), 2, 4, 0);
cv::circle(img, p1, 1, cv::Scalar(0, 0, 255), 3, 4, 0);
// char str[25] = { 0 };
// itoa(i, str, 10);
// cv::putText(img, str, cv::Point(p1.x, p1.y), 1, 1, cv::Scalar(0, 0, 255), 1, 4, 0);
}
}
}
int main()
{
clock_t start_t, end_t;
//cv::VideoCapture vc(0);
cv::VideoCapture vc("./test.mp4");
if (vc.isOpened())
{
// 加载dlib的人脸检测器
dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
// 加载人脸形状探测器
dlib::shape_predictor sp;
dlib::deserialize("./Face/shape_predictor_68_face_landmarks.dat") >> sp;
// 循环操作
cv::Mat SrcMat, Mat;
while (1)
{
// 读取一帧图像
vc >> SrcMat;
if (SrcMat.empty()) { break; }
// 每5帧做一次,因为dlib人脸检测耗时约100ms(i7-10750H的CPU下测试)
static unsigned short rFrameRate = 0;
if (++rFrameRate <= 6) { continue; }rFrameRate = 0;
// 提取灰度图
cv::cvtColor(SrcMat, Mat, cv::COLOR_BGR2GRAY);
// Mat转化为dlib的matrix
dlib::array2d<dlib::bgr_pixel> dimg;
dlib::assign_image(dimg, dlib::cv_image<uchar>(Mat));
// 获取一系列人脸所在区域
start_t = (double)clock();
std::vector<dlib::rectangle> dets = detector(dimg);
end_t = (double)clock();
int64_t curTime = 1000 * (end_t - start_t) / (double)CLOCKS_PER_SEC;
std::cout << "total ms:" << curTime;
std::cout << "\tNumber of faces detected: " << dets.size() << std::endl;
if (dets.size() > 0)
{
//获取人脸特征点分布
std::vector<dlib::full_object_detection> shapes;
for (int i = 0; i < dets.size(); i++)
{
dlib::full_object_detection shape = sp(dimg, dets[i]); //获取指定一个区域的人脸形状
shapes.push_back(shape);
}
//指出每个检测到的人脸的位置
for (int i = 0; i < dets.size(); i++)
{
//画出人脸所在区域
cv::Rect r;
r.x = dets[i].left();
r.y = dets[i].top();
r.width = dets[i].width();
r.height = dets[i].height();
cv::rectangle(SrcMat, r, cv::Scalar(0,255, 0, 0), 3, 1, 0);
}
// 特征绘制
line_one_face_detections(SrcMat, shapes);
}
// 刷新图片
cv::resize(SrcMat, SrcMat, cv::Size(480, 320));
cv::imshow("Mat", SrcMat);
cv::waitKey(1);
}
vc.release();
}
system("pause");
return 0;
}
关注
笔者 - jxd