今天再接再厉运行了人脸的landmark,进行了人脸对齐:配置环境什么的我就不说了,在我上一篇博客:http://blog.youkuaiyun.com/qq_22764813/article/details/53053232中有介绍。
注意:有时候新建工程,出不来以下这一栏(如下图所示):
解决的方法,就是先加入source.cpp,这个点好像在人脸检测中有遗漏,如果你的库都加载好了,但是就是运行有bug,此时可能就是你没有加入source文件。在我的工程中我是按下图方式加载的:
如上图所示,就可以了,也不用担心找不到相应的“C\C++”了。
下面是加入命名参数 的图示:
具体操作:在http://download.youkuaiyun.com/detail/sunmc1204953974/9289921上下载model,解压,然后将文件的绝对路径加入到命名参数当中,然后空格,加入相应的图片文件名的绝度路径。这样就可以执行了。
下面是我运行使用的人脸68个标注点的代码:
#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<iostream>
using namespace dlib;
using namespace std;
int main(int argc, char** argv)
{
//这个例子需要一系列的形状模型和一系列的图片
try
{
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;
}
//需要一个人脸检测器
frontal_face_detector detector = get_frontal_face_detector();
//也需要一个形状预测器,这是一个工具用来预测给定的图片和脸边界框的标记点的位置。
//这里我们仅仅从shape_predictor_68_face_landmarks.dat文件加载模型
shape_predictor sp;//定义个shape_predictor类的实例
deserialize(argv[1]) >> sp;
image_window win, win_faces;
//循环所有图片
cout << "processing image " << argv[2] << endl;
array2d<rgb_pixel> img;//注意变量类型 rgb_pixel 三通道彩色图像
load_image(img, argv[2]);
// Make the image larger so we can detect small faces.
pyramid_up(img);
std::vector<rectangle> dets = detector(img);//检测人脸,获得边界框
cout << "Number of faces detected: " << dets.size() << endl;//检测到人脸的数量
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;//获得第一个点的坐标,注意第一个点是从0开始的
//cout << "pixel position of second part: " << shape.part(1) << endl;//获得第二个点的坐标
/*自己改写,打印出全部68个点*/
for (int i = 1; i < 69; i++)
{
cout << "第 " << i << " 个点的坐标: " << shape.part(i - 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);
}
//**** 显示结果
win.clear_overlay();
win.set_image(img);
win.add_overlay(render_face_detections(shapes));
//****我们也能提取每张剪裁后的人脸的副本,旋转和缩放到一个标准尺寸
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();
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
运行后的效果图:
结果分析:这个比我之前训练的FPS 3000效果好太多了,都有点不敢相信。But 人脸检测的速度依然很慢,所以接下来会试着想办法改进实验的速度,特别是人脸检测这一块。
文章参考:http://blog.youkuaiyun.com/sunshine_in_moon/article/details/50150435