//@zmdsjtu@163.com
//2016-12-4
//http://blog.csdn.net/zmdsjtu/article/details/53454071
#include <dlib/opencv.h>
#include <opencv2/opencv.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>
using namespace dlib;
using namespace std;
//using namespace std;//与dlib空间命名冲突,不能使用.....
int main()
{
try
{
//cv::VideoCapture cap(0);//打开摄像头
cv::VideoCapture cap("C://Users//74205//Desktop//test.mp4");//打开视频文件
if (!cap.isOpened())
{
cerr << "Unable to connect to camera" << endl;
cout << "can't read the video or the image" << endl;
return 1;
}
//image_window win;
// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
// Grab and process frames until the main window is closed by the user.
while (cv::waitKey(30) != 27)
{
// Grab a frame
cv::Mat temp;
cap >> temp;
cv_image<bgr_pixel> cimg(temp);
// Detect faces
std::vector<rectangle> faces = detector(cimg);
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
if (!shapes.empty()) {
for (int i = 0; i < 68; i++) {
circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
putText(temp, to_string(i), cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0), 1, 4);
// shapes[0].part(i).x();//68个
}
}
//Display it all on the screen
imshow("Dlib特征点", temp);
}
}
catch (serialization_error& e)
{
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch (exception& e)
{
cout << e.what() << endl;
}
}
//#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 <dlib/image_io.h>
//#include <dlib/opencv.h>
//#include <iostream>
//#include<vector>
//#include<math.h>
//#include <opencv2/opencv.hpp>
//using namespace dlib;
//using namespace std;
//
//std::vector<string> getTrainimg(string str){
// cv::Directory dir;
// string path1 = "C://Users//74205//Desktop//rawImage//" + str;
// string exten1 = "*.jpg";//JAFFE .jpg ,CK .bmp
// bool addPath1 = true;
// std::vector<string> filenames = dir.GetListFiles(path1, exten1, addPath1);
//
// return filenames;
//}
//std::vector<string> getTesting(string str){
// cv::Directory dir;
// string path1 = "C://Users//74205//Desktop//rawImage//" + str;
// string exten1 = "*.jpg";//JAFFE .jpg ,CK .bmp
// bool addPath1 = true;
// std::vector<string> filenames = dir.GetListFiles(path1, exten1, addPath1);
//
// return filenames;
//}
//int main(int argc, char** argv)
//{
// try
// {
// // This example takes in a shape model file and then a list of images to
// // process. We will take these filenames in as command line arguments.
// // Dlib comes with example images in the examples/faces folder so give
// // those as arguments to this program.
// //if (argc == 1)
// //{
// // cout << "Call this program like this:" << endl;
// // cout << "./face_landmark_detection_ex shape_predictor_68_face_landmarks.dat faces/*.jpg" << endl;
// // cout << "\nYou can get the shape_predictor_68_face_landmarks.dat file from:\n";
// // cout << "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
// // return 0;
// //}
// //std::cout << "argc:" << argc << std::endl;
// // We need a face detector. We will use this to get bounding boxes for
// // each face in an image.
// frontal_face_detector detector = get_frontal_face_detector();
// // And we also need a shape_predictor. This is the tool that will predict face
// // landmark positions given an image and face bounding box. Here we are just
// // loading the model from the shape_predictor_68_face_landmarks.dat file you gave
// // as a command line argument.
// shape_predictor sp;
// // std::cout << "argv[1]:" << argv[1] << std::endl;
// deserialize("shape_predictor_68_face_landmarks.dat") >> sp;
//
// image_window win;// win_faces;
// // Loop over all the images provided on the command line.
// std::vector<string> filenames=getTrainimg("happy");
// int filesize = filenames.size();
// const char *p;
// for (int i = 0; i < filesize; ++i)
// {
// array2d<bgr_pixel> img;
// p = filenames[i].c_str();//string->char *
// load_image(img, p);
// // Make the image larger so we can detect small faces.
// pyramid_up(img);
//
// // Now tell the face detector to give us a list of bounding boxes
// // around all the faces in the image.
// std::vector<rectangle> dets = detector(img);
// cout << "Number of faces detected: " << dets.size() << endl;
//
// // Now we will go ask the shape_predictor to tell us the pose of
// // each face we detected.
// std::vector<full_object_detection> shapes;
// for (unsigned long j = 0; j < dets.size(); ++j)
// {
// full_object_detection shape = sp(img, dets[j]);
// cout << "number of parts: " << shape.num_parts() << endl;
// cout << "pixel position of first part: " << shape.part(0) << endl;
// cout << "pixel position of second part: " << shape.part(1) << endl;
// // You get the idea, you can get all the face part locations if
// // you want them. Here we just store them in shapes so we can
// // put them on the screen.
// shapes.push_back(shape);
// cv::Mat temp = dlib::toMat(img);
//
// for (int k = 0; k < 68; ++k){
// circle(temp, cvPoint(shapes[j].part(k).x(), shapes[j].part(k).y()), 3, cv::Scalar(0, 0, 255), -1);//j代表检测到的第几个人脸
// putText(temp, to_string(k), cvPoint(shapes[j].part(k).x(), shapes[j].part(k).y()), CV_FONT_HERSHEY_PLAIN, 1, cv::Scalar(255, 0, 0), 1, 4);//k代表68个特征点
// //cout << "第" << k << "点坐标(" << shapes[j].part(k).x() << "," << shapes[j].part(k).y() << ")" << endl;
// }
// }
//
// // Now let's view our face poses on the screen.
// win.clear_overlay();
// string newFilename = "C://Users//74205//Desktop//detection//";
// newFilename.append(to_string(i));
// newFilename.append(".jpg");
// const char *file = newFilename.data();
// cv::Mat temp = dlib::toMat(img);//将img图像进行保存,转化成mat类型的对象
// //将处理好的图像进行由mat形式转化为IplImage形式
// IplImage *save;
// save = &IplImage(temp);
// cvSaveImage(file, save);
// win.set_image(img);//显示在屏幕上
// //win.add_overlay(render_face_detections(shapes));
//
// We can also extract copies of each face that are cropped, rotated upright,
// and scaled to a standard size as shown here:
// //dlib::array<array2d<rgb_pixel> > face_chips;
// //extract_image_chips(img, get_face_chip_details(shapes), face_chips);
// //win_faces.set_image(tile_images(face_chips));
// cout << "Hit enter to process the next image..." << endl;
// cin.get();//不需要按enter,处理下一张
// }
// }
// catch (exception& e)
// {
// cout << "\nexception thrown!" << endl;
// cout << e.what() << endl;
// }
// system("pause");
//}