libfacedetection的使用
github地址
该项目已于2021年9月20日更新为3.0
现在网上大部分仍是过去版本,不便于使用,因此记录一下
libfacedetection的编译
在该版本下不再需要那些链接库之类的东西,作者已经在不断优化,我们只需要编译项目得到facedetection_export.h这个文件即可。
使用cmake进行编译
我在这里使用了cmake的gui进行编译
选择自己合适的文件夹即可
在文件编译完成后即可得到facedetection_export.h这个头文件
demo的使用
添加项目
在VS中随意新建一个项目
将libfacedetection-master中的.cpp文件和.h文件添加到项目中
还有facedetection_export.h这个头文件
项目结构大体如下
使用demo
作者已经为我们提供了demo在example文件夹下
只需要将其添加到项目中即可
由于在使用过程中可能需要用户自定义一些图片等内容,因此将部分参数进行修改(在测试中我所使用的是camera的demo,由于我太菜,不知道main的argc该怎么使用,直接在代码中将cap.open(0),打开电脑的摄像头)
#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() //原代码 int main(int argc, char* argv[])
{
/*
if (argc != 2)
{
printf("Usage: %s <camera index>\n", argv[0]);
return -1;
}
*/
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;
}
VideoCapture cap;
Mat im;
// 原代码形式
/*
if( isdigit(argv[1][0]))
{
cap.open(argv[1][0]-'0');
if(! cap.isOpened())
{
cerr << "Cannot open the camera." << endl;
return 0;
}
*/
// 改了一下 直接用电脑自带的摄像头
cap.open(0);
if (!cap.isOpened())
{
cerr << "Cannot open the camera." << endl;
return 0;
}
if (cap.isOpened())
{
while (true)
{
cap >> 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);
//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;
}
运行即可