libfacedetection介绍
libfacedetection是一个开源的人脸检测库,使用C编写,将模型文件转化为C的静态变量,不依赖外部第三方库,使用时可以直接把源代码拷到自己的工程,也可以使用动态库(so)/静态库(a)的方式来调用,使用还是很方便的。
这里介绍基于该库的动态链接库编译及调用的demo.
2.下载及编译

git clone https://github.com/ShiqiYu/libfacedetection.git

cmake生成makefile,再make生成动态库和静态库
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/libfacedetection -G "Unix Makefiles" ..

cmake之后会生成Makefile

查看动态库的依赖

进入example目录
由于linux没有可视化窗口,因此将imshow注释掉,再通过imwrite将结果保存下来
g++ libfacedetectcnn-example.cpp -o test `pkg-config --cflags --libs opencv` -I ../src/ -L ../build/ -lfacedetection
结果如下

3.代码分析
输入bgr格式数据,调用facedetect_cnn函数即可返回检测结果,再使用rectangle函数将矩阵画上去。
#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 argc, char* argv[])
{if(argc != 2){printf("Usage: %s <image_file_name>\n", argv[0]);return -1;}//load an image and convert it to gray (single-channel)Mat image = imread(argv[1]); if(image.empty()){fprintf(stderr, "Can not load the image file %s.\n", argv[1]);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;}///// CNN face detection // Best detection rate////!!! The input image must be a BGR one (three-channel) instead of RGB//!!! DO NOT RELEASE pResults !!!pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step);printf("%d faces detected.\n", (pResults ? *pResults : 0));Mat result_cnn = image.clone();//print the detection resultsfor(int i = 0; i < (pResults ? *pResults : 0); i++){short * p = ((short*)(pResults+1))+142*i;int x = p[0];int y = p[1];int w = p[2];int h = p[3];int confidence = p[4];int angle = p[5];printf("face_rect=[%d, %d, %d, %d], confidence=%d, angle=%d\n", x,y,w,h,confidence, angle);rectangle(result_cnn, Rect(x, y, w, h), Scalar(0, 255, 0), 2);}imshow("result_cnn", result_cnn);waitKey();//release the bufferfree(pBuffer);return 0;
}
参考
[1] https://github.com/ShiqiYu/libfacedetection




![[深度学习] Python人脸识别库Deepface使用教程](https://img-blog.csdnimg.cn/img_convert/8b624f3fc366bb67c3633e23813e368b.png)
![[深度学习] Python人脸识别库face_recognition使用教程](https://img-blog.csdnimg.cn/img_convert/3ec970274e38e9daa167b45c8f8b3274.png)








