目录
1、源码下载
2、编译
3、构建工程
4、个人总结
运行总结:
与CascadeClassifier级联分类器 人脸检测 对比:
1、源码下载
直接从github上克隆项目仓库。
git clone https://github.com/ShiqiYu/libfacedetection.git
2、编译
这个项目使用了cmake脚本,先生成makefile。
cmake -DENABLE_NEON=OFF -DCMAKE_BUILD_TYPE=RELEASE .
输出:
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
BUILD_VERSION:v0.0.3
-- Found OpenMP_C: -fopenmp (found version "4.5")
-- Found OpenMP_CXX: -fopenmp (found version "4.5")
-- Found OpenMP: TRUE (found version "4.5")
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
CXX_FLAGS: -O3 -mavx2 -mfma -fopenmp
LINKER_FLAGS:
AVX512 = OFF
AVX2 = ON
NEON = OFF
OpenMP = TRUE
DEMO = OFF
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
NABLE_NEON
-- Build files have been written to: /home/jason/file/libfacedetection-master
执行上面的命令成功后,执行下面语句进行编译
make -j4
输出:
[ 25%] Building CXX object CMakeFiles/facedetection.dir/src/facedetectcnn-data.cpp.o
[ 50%] Building CXX object CMakeFiles/facedetection.dir/src/facedetectcnn-model.cpp.o
[ 75%] Building CXX object CMakeFiles/facedetection.dir/src/facedetectcnn.cpp.o
[100%] Linking CXX static library libfacedetection.a
[100%] Built target facedetection
如此,得到了两个重要的文件:
3、构建工程
除了上面得到的facedetection_export.h文件,还需要src目录下的四个文件:
以及examples目录下的detect-image.cpp(你也可以用detect-camera.cpp),改名为main.cpp
稍微修改main.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include "facedetectcnn.h"
//define the buffer size. Do not change the size!
//0x9000 = 1024 * (16 * 2 + 4), detect 1024 face at most
#define DETECT_BUFFER_SIZE 0x9000
using namespace cv;
using namespace std;
int main()
{
//load an image and convert it to gray (single-channel)
string img_path = "/home/jason/file/02-use-libfacedetection/cnnresult.png";
Mat image = imread(img_path);
if(image.empty())
{
printf("Can not load the image file %s.\n", img_path.c_str());
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 !!!
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)) + 16*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);
waitKey();
//release the buffer
free(pBuffer);
return 0;
}
构建方式设为release:
结果:
time = 687.227ms
39 faces detected.
face 0: confidence=93, [864, 419, 105, 135] (888,470) (940,467) (912,496) (895,518) (939,516)
face 1: confidence=93, [77, 170, 70, 92] (102,205) (134,206) (121,224) (103,238) (131,238)
face 2: confidence=93, [535, 599, 105, 137] (562,654) (613,652) (589,677) (566,699) (612,698)
face 3: confidence=92, [867, 278, 66, 87] (885,312) (914,311) (900,329) (888,343) (914,341)
face 4: confidence=92, [636, 374, 80, 106] (659,414) (694,414) (677,436) (661,453) (692,453)
face 5: confidence=92, [731, 288, 79, 100] (755,324) (791,324) (774,344) (757,361) (789,361)
face 6: confidence=92, [1168, 233, 76, 101] (1190,265) (1223,265) (1207,287) (1192,303) (1222,303)
face 7: confidence=92, [259, 292, 74, 97] (277,331) (314,331) (295,352) (280,363) (311,365)
face 8: confidence=92, [386, 512, 88, 110] (411,558) (454,557) (433,581) (412,595) (449,596)
face 9: confidence=92, [653, 116, 63, 80] (669,145) (697,144) (681,162) (671,175) (695,174)
face 10: confidence=91, [149, 233, 64, 85] (165,266) (196,266) (181,285) (170,298) (195,297)
face 11: confidence=91, [1034, 326, 91, 112] (1055,363) (1094,362) (1072,384) (1061,405) (1092,403)
face 12: confidence=91, [310, 134, 58, 73] (330,160) (355,161) (344,176) (332,187) (353,188)
face 13: confidence=91, [968, 120, 59, 76] (985,146) (1013,147) (1000,162) (986,173) (1011,175)
face 14: confidence=91, [431, 271, 62, 81] (449,300) (479,300) (466,317) (451,330) (477,330)
face 15: confidence=90, [472, 148, 68, 90] (491,182) (524,182) (507,201) (493,213) (520,215)
face 16: confidence=90, [1063, 55, 55, 73] (1075,80) (1098,80) (1085,95) (1078,107) (1099,108)
face 17: confidence=90, [3, 171, 56, 77] (16,198) (42,198) (29,214) (18,227) (41,227)
face 18: confidence=90, [771, 84, 60, 75] (792,111) (818,111) (807,127) (793,139) (815,139)
face 19: confidence=88, [317, 13, 41, 53] (327,35) (345,35) (336,46) (330,55) (344,54)
face 20: confidence=88, [606, 46, 52, 65] (619,70) (644,70) (631,83) (621,92) (641,94)
face 21: confidence=88, [48, 73, 47, 58] (62,93) (84,93) (73,105) (63,112) (81,114)
face 22: confidence=88, [545, 54, 54, 71] (562,81) (586,81) (576,96) (565,106) (584,107)
face 23: confidence=87, [259, 57, 48, 62] (275,81) (295,81) (286,94) (276,104) (292,104)
face 24: confidence=85, [222, 60, 45, 61] (239,84) (250,82) (248,95) (244,106) (252,105)
face 25: confidence=85, [718, 215, 72, 79] (737,248) (767,247) (753,267) (744,275) (766,277)
face 26: confidence=80, [1098, 46, 53, 73] (1123,76) (1130,76) (1129,90) (1128,103) (1130,103)
face 27: confidence=78, [147, 167, 46, 60] (157,191) (172,191) (161,203) (159,212) (171,215)
face 28: confidence=78, [699, 2, 46, 43] (711,15) (733,13) (723,25) (716,32) (732,33)
face 29: confidence=74, [450, 1, 50, 41] (472,6) (491,5) (486,17) (474,27) (489,28)
face 30: confidence=63, [1194, 38, 52, 60] (1211,58) (1236,59) (1223,70) (1211,79) (1230,81)
face 31: confidence=62, [832, 86, 42, 58] (844,106) (859,106) (851,118) (847,126) (860,129)
face 32: confidence=48, [1149, 0, 35, 28] (1162,5) (1175,5) (1169,13) (1164,18) (1173,21)
face 33: confidence=48, [810, 0, 42, 40] (825,7) (841,7) (834,18) (827,26) (841,28)
face 34: confidence=45, [1021, 92, 40, 52] (1032,111) (1045,112) (1037,122) (1035,129) (1044,131)
face 35: confidence=42, [571, 0, 38, 26] (583,-3) (598,-2) (590,2) (583,12) (594,11)
face 36: confidence=30, [114, 0, 36, 25] (121,-1) (135,0) (125,4) (123,13) (134,13)
face 37: confidence=28, [0, 28, 30, 58] (5,50) (14,50) (10,60) (9,69) (16,71)
face 38: confidence=21, [889, 0, 20, 17] (896,0) (904,2) (900,5) (897,12) (902,10)
4、个人总结
运行总结:
- 从图片中检测即使构建方式为Release,依然是600多ms耗时,感觉有点慢
- 使用摄像头检测,构建方式未Realease,检测耗时150ms左右,有点延迟
- 不过该说不说,这张图片后面被虚化的人脸都检测出来了,确实检测效果还不错
- 另外图片检测的话,检测框我怎么看都有两个,从源码中暂时又看不出什么问题。可是github上挂的结果图不是这样的啊,有大佬知道怎么回事吗?
- 而摄像头检测则很正常,只有一个检测框
与CascadeClassifier级联分类器 人脸检测 对比:
- 效果好很多,侧脸也可以识别出来,只要露出眼睛嘴巴鼻子
- 存在一定误检,但是可以控制输出阈值的方式调整
存在的疑问:
编译生成的静态库应该有用?
参考:
libfacedetection库的配置及基本使用——内涵(cmake编译libfacedetection库)_龙龙就是龙龙的博客-CSDN博客
https://www.cnblogs.com/oloroso/p/10716214.html