//设置人脸的标记点
#include <dlib/opencv.h>
#include <opencv2/highgui/highgui.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <opencv2/imgproc/imgproc.hpp>
// #include <iostream>
// #include <opencv2/objdetect/objdetect.hpp>
// #include <fstream>
// #include <stdlib.h>
// #include <opencv2/opencv.hpp>
// #include <locale>
// #include <string>
// #include <vector>
using namespace dlib;
using namespace std;
//using namespace cv;
int main() {
try {
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "Unable to connect to camera" << endl;
return 1;
}
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pos_modle; //将文件中的模型放置再pos_modle中
deserialize("/home/xxp/Desktop/face/shape_predictor_68_face_landmarks.dat") >> pos_modle;
while (cv::waitKey(30)!=27)
{
//获取一帧
cv::Mat temp; //将摄像头获取的当前帧图片放入到中间文件中
cap >> temp; //将其转化为RGB像素图片
cv_image<bgr_pixel> cimg(temp); //开始进行脸部识别
//std::vector<rectangle> faces = detector(cimg); //检测人脸
std::vector<rectangle> faces = detector(cimg);
std::vector<full_object_detection> shapes; //发现每一个脸的pos估计
unsigned faceNumber=faces.size(); //将所有脸的区域放入集合之中
for (unsigned i = 0; i < faceNumber; i++)
shapes.push_back(pos_modle(cimg, faces[i])); //第i个人的关键点。
if (!shapes.empty()) {
int faceNumber = shapes.size();
for (int j = 0; j < faceNumber; j++)
{
for (int i = 0; i < 68; i++)
{
cv::circle(temp, cvPoint(shapes[j].part(i).x(), shapes[j].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
//cv::putText(temp,to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN,1, cv::Scalar(0, 0, 255));
}
}
}
imshow("Dlib标记", temp);
}
}
catch (serialization_error &e) {
cout << "You need dlib's default face landmarking file to run this example.(你需要添加landmark的bat文件,才可以跑这个实例)" << endl;
cout << endl << e.what() << endl;
}
catch(exception &e){
cout << e.what() << endl;
}
}