于仕琪libfacedetection WIN10 VS2015

该博客介绍如何利用libfacedetection库在C++项目中进行人脸识别。通过下载代码,配置OpenCV,设置优化选项,以及修改源代码,实现实时摄像头人脸检测。代码示例展示了一个读取摄像头视频流,检测人脸并显示检测结果的程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里是运行速度 (i7-6700)
在这里插入图片描述

https://github.com/ShiqiYu/libfacedetection
首先下载代码压缩包,解压

在这里插入图片描述
打开src
在这里插入图片描述
将这几个文件复制到你的项目文件夹中
在这里插入图片描述
在项目中以添加现有项的方式将这四个文件添加到项目中
在这里插入图片描述
右键项目打开项目属性,并选择Release – x64
在这里插入图片描述
首先配置opencv,请自行查阅其他人的博客,有很多人都有详细的配置步骤,这里跳过
选择c/c++ 优化选择使速度最大化(/O2)
在这里插入图片描述

在这里插入图片描述
所有选项OpenMp支持 --是
在这里插入图片描述
在facedetectcnn.h下对代码进行修改
在这里插入图片描述
修改之后,在自己的项目下创建新的cpp把以下代码复制进去(于老师给的例子,摄像头检测人脸,并把调用摄像头的代码进行了一点点更改)

/*
By downloading, copying, installing or using the software you agree to this license.
If you do not agree to this license, do not download, install,
copy or use the software.


License Agreement For libfacedetection
(3-clause BSD License)

Copyright (c) 2018-2020, Shiqi Yu, all rights reserved.
shiqi.yu@gmail.com

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall copyright holders or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "facedetectcnn.h"

//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000
using namespace cv;

int main()
{
	VideoCapture capture(0);
	Mat face;
	int * pResults = NULL;
	//pBuffer is used in the detection functions.
	//If you call functions in multiple threads, please create one buffer for each thread!
	unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
	if (!pBuffer)
	{
		fprintf(stderr, "Can not alloc buffer.\n");
		return -1;
	}

	Mat im;

	if (!capture.isOpened())
	{
		puts("dont open video.");
		system("pause");
		return -1;
	}

	if (capture.isOpened())
	{
		while (true)
		{
			capture >> im;
			//cout << "Image size: " << im.rows << "X" << im.cols << endl;
			Mat image = im.clone();

			///
			// CNN face detection 
			// Best detection rate
			//
			//!!! The input image must be a BGR one (three-channel) instead of RGB
			//!!! DO NOT RELEASE pResults !!!
			TickMeter cvtm;
			cvtm.start();

			pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);

			cvtm.stop();
			printf("time = %gms\n", cvtm.getTimeMilli());

			printf("%d faces detected.\n", (pResults ? *pResults : 0));
			Mat result_image = image.clone();
			//print the detection results
			for (int i = 0; i < (pResults ? *pResults : 0); i++)
			{
				short * p = ((short*)(pResults + 1)) + 142 * i;
				int confidence = p[0];
				int x = p[1];
				int y = p[2];
				int w = p[3];
				int h = p[4];

				//show the score of the face. Its range is [0-100]
				char sScore[256];
				snprintf(sScore, 256, "%d", confidence);
				cv::putText(result_image, sScore, cv::Point(x, y - 3), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 255, 0), 1);

				//draw face rectangle
				rectangle(result_image, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
				//draw five face landmarks in different colors
				cv::circle(result_image, cv::Point(p[5], p[5 + 1]), 1, cv::Scalar(255, 0, 0), 2);
				cv::circle(result_image, cv::Point(p[5 + 2], p[5 + 3]), 1, cv::Scalar(0, 0, 255), 2);
				cv::circle(result_image, cv::Point(p[5 + 4], p[5 + 5]), 1, cv::Scalar(0, 255, 0), 2);
				cv::circle(result_image, cv::Point(p[5 + 6], p[5 + 7]), 1, cv::Scalar(255, 0, 255), 2);
				cv::circle(result_image, cv::Point(p[5 + 8], p[5 + 9]), 1, cv::Scalar(0, 255, 255), 2);
				if (confidence == 99)
					face = result_image(Rect(x, y, w, h));
				//print the result
				printf("face %d: confidence=%d, [%d, %d, %d, %d] (%d,%d) (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n",
					i, confidence, x, y, w, h,
					p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14]);
			}
			imshow("result", result_image);

			if ((cv::waitKey(2) & 0xFF) == 'q')
				break;
		}
	}




	//release the buffer
	free(pBuffer);

	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值