深大于老师的libfacedetection检测算法快速高效,准确率相当高,世界排名第五,最小可检测人脸12×12像素。关键是前两天开源了,于是我简单的看了一下,是自己用c++手敲的cnn代码,真心佩服。
该代码可以在windows linux arm android平台等只要支持c++编译环境中运行,下面是windows和树莓派(arm)平台运行的各项参数:
下面我在ubuntu16.04 下编译和运行了一下,效果非常好,感兴趣的朋友可以试一试,IDE用的是QT。
1、下载源码
git clone https://github.com/ShiqiYu/libfacedetection.git
2、qt5编译
(1)配置.pro文件
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
libfacedetectcnn-example.cpp \
facedetectcnn-int8data.cpp \
facedetectcnn-floatdata.cpp \
facedetectcnn-model.cpp \
facedetectcnn.cpp
HEADERS += \
facedetectcnn.h
#system
INCLUDEPATH += /usr/local/lib \
/usr/lib/x86_64-linux-gnu
LIBS += -L/usr/local/lib
#opencv 其实在/usr/include就可以不要添加
INCLUDEPATH += /usr/include \
/usr/include/opencv \
/usr/include/opencv2/
LIBS += -L /usr/lib/libopencv_*.so
QMAKE_CXXFLAGS_RELEASE += -O3 # Release -O3
QMAKE_CXXFLAGS_RELEASE += -march=native
(2)修改libfacedetectcnn-example.cpp(我是用摄像头测试)
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "facedetectcnn.h"
#include "time.h"
//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
//usb camera
Mat image;
VideoCapture cam(0);
while(1){
cam >> image;
if(image.empty())
{
fprintf(stderr, "Can not open camera.\n");
return -1;
}
resize(image,image,Size(320,240));
time_t start = clock();
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 RGB one (three-channel)
//!!! 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 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 neighbors = p[4];
int angle = p[5];
printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);
cv::rectangle(result_cnn, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
}
time_t end = clock();
//show in the screen
imshow("result_cnn", result_cnn);
//release the buffer
free(pBuffer);
//calculate running time
double total_time = (double)(end-start)/CLOCKS_PER_SEC*1000;
cout<<"total_time:"<<total_time<<"ms"<<endl;
waitKey(1);
}
return 0;
}
3、测试
至于速度和准确度,你们测试一下就知道了。