vs2015+opencv3.3+libfacedetectcnn 人脸识别

    xiaoxiao2022-07-02  104

    1. Libfacedetect

    libfacedetection库是深圳大学的于仕琪老师发布的开源人脸检测库,相比于OpenCV自带的CascadeClassifier人脸检测,无论在速度上还是精度上,都有巨大的优势,是目前已知开源库中最好用的一款。

    【于老师的技术共享:怎么把人脸检测的速度做到极致】https://mp.weixin.qq.com/s?__biz=MzA3NDU3MTc1Ng==&mid=2651165778&idx=1&sn=2f2d8f6b7a11d381a4290a20817b46a2

    【在GitHub上下载libfacedetection的最新库文件】https://github.com/ShiqiYu/libfacedetection 

    2.  创建vs工程:

    拷贝.h ,cpp文件至工程目录下:如图所示:

    根据提示:

    设置-O2:

    为避免报错error:“/O2”和“/RTC1”命令行选项不兼容,进行如下设置:

    【The optimization setting Maximize Speed (/O2) is incompatible to the Basic Runtime Checks setting of Code Generation. To change the settings for Basic Runtime Checks: Solution -> Properties -> Configuration Properties -> C/C++ -> Code Generation -> Basic Runtime Checks Set it to "Default", and it should work.】

    3. 代码执行

     在libfacedetectcnn-example.cpp中,

    修改main函数,读取本地图片进行人脸识别:

    int main(int argc, char* argv[]) { int goodFrameCount = 0; int frameNumber = 30; string calibImagePath = "..\\data\\"; //pBuffer is used in the detection functions. //If you call functions in multiple threads, please create one buffer for each thread! unsigned char * pBuffer1 = (unsigned char *)malloc(DETECT_BUFFER_SIZE); if (!pBuffer1) { fprintf(stderr, "Can not alloc buffer.\n"); return -1; } unsigned char * pBuffer2 = (unsigned char *)malloc(DETECT_BUFFER_SIZE); if (!pBuffer2) { fprintf(stderr, "Can not alloc buffer.\n"); return -1; } while (goodFrameCount < frameNumber) { char filename[100]; /*读取左边的图像*/ sprintf_s(filename, "leftd.jpg", goodFrameCount + 1); Mat img1= imread(calibImagePath + filename, 1); /*读取右边的图像*/ sprintf_s(filename, "rightd.jpg", goodFrameCount + 1); Mat img2= imread(calibImagePath + filename, 1); goodFrameCount++; int * pResults1 = NULL; int * pResults2 = NULL; /// // CNN face detection // Best detection rate // //!!! The input image must be a BGR one (three-channel) instead of RGB //!!! DO NOT RELEASE pResults !!! pResults1 = facedetect_cnn(pBuffer1, (unsigned char*)(img1.ptr(0)), img1.cols, img1.rows, (int)img1.step); //printf("%d faces detected.\n", (pResults1 ? *pResults1 : 0)); Mat result_cnn1 = img1.clone(); //print the detection results for (int i = 0; i < (pResults1 ? *pResults1 : 0); i++) { short * p = ((short*)(pResults1 + 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_cnn1, Rect(x, y, w, h), Scalar(0, 255, 0), 2); } pResults2 = facedetect_cnn(pBuffer2, (unsigned char*)(img2.ptr(0)), img2.cols, img2.rows, (int)img2.step); //printf("%d faces detected.\n", (pResults2 ? *pResults2 : 0)); Mat result_cnn2 = img2.clone(); //print the detection results for (int i = 0; i < (pResults2 ? *pResults2 : 0); i++) { short * p = ((short*)(pResults2 + 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_cnn2, Rect(x, y, w, h), Scalar(0, 255, 0), 2); } cv::Mat disPlay = cv::Mat::zeros(img1.rows, img1.cols * 2, CV_8UC3); cv::Rect xxRect = cv::Rect(0, 0, img1.cols, img1.rows); cv::Rect yyRect = cv::Rect(img1.cols, 0, img1.cols, img1.rows); disPlay(xxRect) = disPlay(xxRect) | result_cnn1; disPlay(yyRect) = disPlay(yyRect) | result_cnn2; printf("%d,%d\n", (pResults1 ? *pResults1 : 0), (pResults2 ? *pResults2 : 0)); } //release the buffer free(pBuffer1); free(pBuffer2); return 0; }

     

    最新回复(0)