开发环境:Win10&VS2015
识别效果:打开笔记本镜头,使用Dlib实时检测出68个关键点,并用一条线指出人脸朝向:
实现代码:
#include <opencv2/opencv.hpp>
#include <vector>
#include <dlib/opencv.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing.h>
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
try
{
cv::VideoCapture cvCap(0);
if (!cvCap.isOpened())
{
std::cout << "Can not open the camera." << std::endl;
return 1;
}
// load face detection and pose estimation models.
dlib::frontal_face_detector dlibSvmFaceDetector;
dlib::shape_predictor dlibSpFaceLandmark;
std::vector<dlib::rectangle> dlibRectsFaces;
std::vector<dlib::full_object_detection> dlibDetsShapes;
cv::Mat cvImgFrame;
cv::Mat cvImgFrameGray;
clock_t clkBegin;
clock_t clkEnd;
dlibSvmFaceDetector = dlib::get_frontal_face_detector();
dlib::deserialize("./shape_predictor_68_face_landmarks.dat") >> dlibSpFaceLandmark;
while (true)
{
if (!cvCap.read(cvImgFrame))
{
break;
}
clkBegin = clock();
cv::flip(cvImgFrame, cvImgFrame, 1);
cv::cvtColor