⼈脸检测库libfacedetection使⽤⽅法
libfacedetection介绍
libfacedetection是⼀个开源的⼈脸检测库,使⽤C编写,将模型⽂件转化为C的静态变量,不依赖外部第三⽅库,使⽤时可以直接把源代码拷到⾃⼰的⼯程,也可以使⽤动态库(so)/静态库(a)的⽅式来调⽤,使⽤还是很⽅便的。
这⾥介绍基于该库的动态链接库编译及调⽤的demo.
2.下载及编译
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]);
pty())
{
fprintf(stderr, "Can not load the image file %s.\n", argv[1]);
return -1;
}
int * pResults = NULL;
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
rectangle函数opencvpResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), ls, ws, (int)image.step);
printf("%d faces detected.\n", (pResults ? *pResults : 0));
Mat result_cnn = image.clone();
//print the detection results
for(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 buffer
free(pBuffer);
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。