结合opencv配置dlib的人脸关键点定位算法

部署环境如下:

WIN7+VS2015+dlib19.4+opencv249

示例代码如下:

#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>
#include "opencv2/core/core.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/imgproc/imgproc.hpp" 
#include <dlib/opencv.h>
//#include <dlib/opencv/to_open_cv.h>

using namespace dlib;
using namespace std;
using namespace cv;
string int2str(int num) {
	stringstream ss;
	ss << num;
	string str;
	ss >> str;
	return str;
}

int main(int argc, char** argv)
{
	
		// 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 == 9999)
		{
			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;
			system("pause");
			return 0;
		}

		// 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;
		deserialize("E:\\Workspace\\CppWorkspace2015\\DlibTest\\src\\shape_predictor_68_face_landmarks.dat") >> sp;


		


		string filename = "E:\\TestData\\Vedio\\vedio_480P.mp4";//打开的视频文件  
		cv::VideoCapture capture(filename);
		
		Mat frame;
		
			while (capture.read(frame))
			{
				cout << "图像大小为:" << frame.rows << "*" << frame.cols << "\t\t";
				dlib::array2d<bgr_pixel> temp;
				dlib::assign_image(temp, dlib::cv_image<bgr_pixel>(frame));

				long start_det = getTickCount();
				std::vector<dlib::rectangle> dets = detector(temp);
				long end_det = getTickCount();
				cout << "人脸检测用时:" << (end_det - start_det) << "\t";
				std::vector<full_object_detection> shapes;
				for (unsigned long j = 0; j < dets.size(); ++j)
				{
					long start_landmark = getTickCount();
					full_object_detection shape = sp(temp, dets[j]);
					long end_landmark = getTickCount();
					cout << "关键点定位用时" << (end_landmark - start_landmark) << endl;
					for (unsigned long i = 0; i<shape.num_parts(); i++)
					{
						point pt = shape.part(i);
						int x = pt.x();
						int y = pt.y();

						cv::line(frame, Point(pt.x(), pt.y()), Point(pt.x(), pt.y()), Scalar(0, 0, 255), 5);
						//cv::imshow("img", frame);
					}
					
				}
				cv::imshow("img", frame);
				waitKey(1);
				//cout << "detection..." << endl;
			}
	return 0;
}

其中要注意的是:把opencv::mat格式转换为dlib::cv_image<bgr_pixel>然后再转换为dlib::array2d<bgr_pixel>是尝试了多种方法后最终确定的方法.


时间性能如下:


图像大小如图所示,当前算法支持最小80X80人脸检测,性能如上所示,可以看出人脸检测的时间要远远大于关键点定位的耗时时长。在dlib的人脸检测算法用时较长,而关键点定位算法的模型较大(90M左右),但速度却是非常快的,已经有人将该算法移植到手机上实现。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值