Dlib库中提供了正脸人脸关键点检测的接口,这里参考dlib/examples/face_landmark_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸关键点检测的测试代码,测试代码如下:

/* reference: dlib/examples/face_landmark_detection_ex.cpp
  This program shows how to find frontal human faces in an image and
  estimate their pose.  The pose takes the form of 68 landmarks.  These are
  points on the face such as the corners of the mouth, along the eyebrows, on
  the eyes, and so forth
*/
int test_face_landmark()
{
	// download: http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
	const std::string shape_predictor_68_face_landmarks = "E:/GitCode/Face_Test/src/dlib/data/shape_predictor_68_face_landmarks.dat";

	// We need a face detector.  We will use this to get bounding boxes for
	// each face in an image.
	dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();

	// And we also need a shape_predictor.  This will predict face
	// landmark positions given an image and face bounding box
	dlib::shape_predictor sp;
	dlib::deserialize(shape_predictor_68_face_landmarks) >> sp;

	std::vector<std::string> images{ "1.jpg", "2.jpg", "3.jpg", "4.jpeg", "5.jpeg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg",
		"11.jpeg", "12.jpg", "13.jpeg", "14.jpg", "15.jpeg", "16.jpg", "17.jpg", "18.jpg", "19.jpg", "20.jpg" };
	std::vector<int> count_faces{ 1, 2, 6, 0, 1, 1, 1, 2, 1, 1,
		1, 1, 1, 1, 1, 1, 1, 0, 8, 2 };

	std::string path_images{ "E:/GitCode/Face_Test/testdata/" };

	if (images.size() != count_faces.size()) {
		fprintf(stderr, "their size that images and count_faces are mismatch\n");
		return -1;
	}

	for (int i = 0; i < images.size(); i++) {
		cv::Mat matSrc = cv::imread(path_images + images[i], 1);
		if (matSrc.empty()) {
			fprintf(stderr, "read image error: %s\n", images[i].c_str());
			return -1;
		}

		dlib::array2d<unsigned char> img;
		dlib::load_image(img, path_images + images[i]);
		// 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 it can find in the image.
		std::vector<dlib::rectangle> dets = detector(img);
		fprintf(stderr, "detect face count: %d, actual face count: %d\n", dets.size(), count_faces[i]);

		// Now we will go ask the shape_predictor to tell us the pose of
		// each face we detected.
		std::vector<dlib::full_object_detection> shapes;
		for (unsigned long j = 0; j < dets.size(); ++j) {
			dlib::full_object_detection shape = sp(img, dets[j]);
			fprintf(stderr, "landmark num: %d\n", shape.num_parts());
			dlib::rectangle rect = shape.get_rect();
			fprintf(stderr, "rect: left = %d, top = %d, width = %d, height = %d\n", rect.left() / 2, rect.top() / 2, rect.width() / 2, rect.height() / 2);
			cv::rectangle(matSrc, cv::Rect(rect.left() / 2, rect.top() / 2, rect.width() / 2, rect.height() / 2), cv::Scalar(0, 255, 0), 2);

			for (int pt = 0; pt < shape.num_parts(); pt++) {
				cv::circle(matSrc, cv::Point(shape.part(pt).x() / 2, shape.part(pt).y() / 2), 1, cv::Scalar(0, 0, 255), 2);
			}
		}

		std::string save_result = path_images + "_" + images[i];
		cv::imwrite(save_result, matSrc);
	}

	int width = 200;
	int height = 200;
	cv::Mat dst(height * 5, width * 4, CV_8UC3);
	for (int i = 0; i < images.size(); i++) {
		std::string input_image = path_images + "_" + images[i];
		cv::Mat src = cv::imread(input_image, 1);
		if (src.empty()) {
			fprintf(stderr, "read image error: %s\n", images[i].c_str());
			return -1;
		}

		cv::resize(src, src, cv::Size(width, height), 0, 0, 4);
		int x = (i * width) % (width * 4);
		int y = (i / 4) * height;
		cv::Mat part = dst(cv::Rect(x, y, width, height));
		src.copyTo(part);
	}
	std::string output_image = path_images + "result.png";
	cv::imwrite(output_image, dst);

	return 0;
}

执行结果如下图:

  人脸关键点检测结果如下:

Dlib库中实现正脸人脸关键点(landmark)检测的测试代码的更多相关文章

  1. Dlib库中实现正脸人脸检测的测试代码

    Dlib库中提供了正脸人脸检测的接口,这里参考dlib/examples/face_detection_ex.cpp中的代码,通过调用Dlib中的接口,实现正脸人脸检测的测试代码,测试代码如下: #i ...

  2. 【opencv基础】opencv和dlib库中rectangle类型之间的转换

    前言 最近使用dlib库的同时也会用到opencv,特别是由于对dlib库的画图函数不熟悉,都想着转换到opencv进行show.本文介绍一下两种开源库中rectangle类型之间的转换. 类型说明 ...

  3. 使用dlib基于CNN(卷积神经网络)的人脸检测器来检测人脸

    基于机器学习CNN方法来检测人脸比之前介绍的效率要慢很多 需要先下载一个训练好的模型数据: 地址点击下载 // dlib_cnn_facedetect.cpp: 定义控制台应用程序的入口点. // # ...

  4. 机器学习进阶-人脸关键点检测 1.dlib.get_frontal_face_detector(构建人脸框位置检测器) 2.dlib.shape_predictor(绘制人脸关键点检测器) 3.cv2.convexHull(获得凸包位置信息)

    1.dlib.get_frontal_face_detector()  # 获得人脸框位置的检测器, detector(gray, 1) gray表示灰度图, 2.dlib.shape_predict ...

  5. AFLW如何获取你想要的21点人脸关键点数据

    目前人脸检测和人脸的关键点的数据库根据关键点个数:5,20,21,29,68等.https://blog.csdn.net/XZZPPP/article/details/74939823该网页详细列出 ...

  6. C++ chrono 库中的 steady_clock 和 system_clock

    C++11 中提供了一个计时的标准库 <chrono>; 里面有三种时钟 clock: steady_clock, system_clock 和 high_resolution_clock ...

  7. python实现人脸关键部位检测(附源码)

    人脸特征提取 本文主要使用dlib库中的人脸特征识别功能. dlib库使用68个特征点标注出人脸特征,通过对应序列的特征点,获得对应的脸部特征.下图展示了68个特征点.比如我们要提 取眼睛特征,获取3 ...

  8. Opencv与dlib联合进行人脸关键点检测与识别

    前言 依赖库:opencv 2.4.9 /dlib 19.0/libfacedetection 本篇不记录如何配置,重点在实现上.使用libfacedetection实现人脸区域检测,联合dlib标记 ...

  9. dlib库检测人脸使用方法与简单的疲劳检测应用

    简介: dlib库是一个很经典的用于图像处理的开源库,shape_predictor_68_face_landmarks.dat是一个用于人脸68个关键点检测的dat模型库,使用这个模型库可以很方便地 ...

随机推荐

  1. 铁乐学Python_Day35_Socket模块3和hmac模块

    验证客户端链接的合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂, 那么可以利用hmac+加盐的方式来实现. 例1:简单的服务端如下 #!/usr/bin/env ...

  2. SQL server数据库的部署

    一.实验目标 1.安装一台SQL  SERVER(第一台),然后克隆再一台(第二台),一共两台,修改两台的主机和IP地址. 2.使用注册的方式,用第二台远程连接第一台 二.实验步骤 1)先打开一台Wi ...

  3. Logstash和Flume-NG Syslog接收小测试

    目前在大规模日志处理平台中常见的日志采集器可以采用Logstash或Flume.这两种日志采集器架构设计理念基本相似,都采用采集-过滤处理-输出的方式.下面对这两种采集器Syslog接收性能做个简单测 ...

  4. 奇怪的.strip(alir) #()里面有东西 待问老师........

    #关于strips = "alirrijgbskbbbbbar"s1 = s.strip("alir") # strip 去空格,strip(sth),括号里有 ...

  5. #006 dependencies和devDependencies的区别

    dependencies 和 devDependencies 区别 在 npm 中的 package.json ,有两种插件的依赖包配置形式。 dependencies 和 devDependenci ...

  6. Memcahce和Redis比较

    一.Memcache 1.     memecache 把数据全部存在内存之中,断电后会挂掉,数据不能超过内存大小redis有部份存在硬盘上,这样能保证数据的持久性. 2.      Memcache ...

  7. SOJ 4583 动态规划之分组背包

    Description Sidney想去Gandtom家玩.但Sidney家和Gandtom家之间是高低不平.坑坑洼洼的土路.所以他需要用他的背包装几袋稀的泥,在路上铺平一些干的土,使路变成平整的泥土 ...

  8. [IDE123] Intellij Idea 快捷键

    Ctrl+Shift+N,可以快速打开文件 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...

  9. spring boot集成dubbo

    spring-boot-start-dubbo spring-boot-start-dubbo,让你可以使用spring-boot的方式开发dubbo程序.使dubbo开发变得如此简单. 如何使用 1 ...

  10. new的三种形态

    C++语言一直被认为是复杂编程语言中的杰出代表之一,不仅仅是因为其繁缛的语法规则,还因为其晦涩的术语.下面要讲的就是你的老熟人—new: 它是一个内存管理的操作符,能够从堆中划分一块区域,自动调用构造 ...