深大於老師的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、測試
至於速度和準確度,你們測試一下就知道了。