人脸检测作为人脸识别最重要的第一步,检测速度和精度的好坏直接影响到最后识别的质量。本文使用了最好的开源算法,速度达到125FPS,精度达到90%以上。

1、LibFaceDetection

实时性(125fps),配置简单,不再赘述安装过程。
人脸检测源码

#include <iostream>
#include <opencv2/opencv.hpp>

#include "Monochrome.h"
#include "DlibRecognition.h"
#include "FaceDB.h"
#include "util.h"


int main(int argc, char* argv[])
{
	//opencv捕获摄像头
	cv::VideoCapture capture(cv::CAP_DSHOW);
	if (!capture.isOpened()) {
		std::cerr << "Can not open video from camera!" << std::endl;
		return -1;
	}

	//设置摄像头分辨率、帧率
	capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
	capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
	capture.set(CV_CAP_PROP_FPS, 30);

	//计时接口
	TimeWatcher* timewatcher = new TimeWatcher();

	//单色接口,并设置设置人脸大小范围
	Monochrome* monochrome = new Monochrome();
	monochrome->Configure(1.2f, 2, 30, 240);
	int KeyValue = 0;
	try {
		do {
			cv::Mat image;
			capture.read(image);
			if (image.rows <= 0 || image.cols <= 0) {
				std::cerr << "Camera did not grap  the image!" << std::endl;
				continue;
			}
			std::cout << "Resolution is: " << image.cols << "x"
				<< image.rows << std::endl;
			cv::Mat gray;
			cvtColor(image, gray, cv::COLOR_BGR2GRAY);

			//开始计时
			timewatcher->startWatch();

			//人脸检测
			monochrome->Init();
			monochrome->SetImage(gray.ptr(0), gray.cols, gray.rows, 8);
			int face_num = monochrome->ProcessFace();
			std::vector<cv::Rect> faces = monochrome->GetFace();
					
			//结束计时并显示图像
			float total_time = timewatcher->stopWatch();
			monochrome->ShowImage(image, total_time, name);

			KeyValue = cvWaitKey(10);
			if (KeyValue == int('q')) {//按键'q'可以退出程序
				break;
			}

		} while (1);
	}

	catch (std::exception& e)
	{
		std::cout << "\nexception thrown!" << std::endl;
		std::cout << e.what() << std::endl;
	}
	return 0;
}

2、DLibFaceDetection

Dlib人脸检测速度较慢(10fps),不能实时,有兴趣的可以尝试。
人脸检测

人脸识别系统开发(四)人脸识别

3、最新算法更新

最新超轻量级通用人脸检测模型

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐