人脸检测的三种提速尝试(转载)

mark一下,感谢作者分享!
https://blog.csdn.net/zmdsjtu/article/details/53924866


之前写过一篇博客测试出Dlib自带的人脸检测模块速率很慢,在博客里有提及更换其他人脸检测模块或者对其进行速率优化。


惯例先放最好的结果,可以看出来已经可以优化到10ms而不怎么掉帧了……





回到之前的不优化的效果:载入模型先不谈,人脸检测需要花费100ms左右,特征点定位只需要3.5ms,故而为了提高帧率,人脸检测模块的更换或者优化是很有必要的。



1.Opencv人脸检测


首先试下传闻中效果很差但速率很快的Opencv人脸检测模块:


模块非常简单,导入模型,识别……//这里的模型放在Opencv的etc目录下。


  1. #include “opencv2\opencv.hpp”  
  2. #include <iostream>  
  3. using namespace std;  
  4. using namespace cv;  
  5.   
  6. int main()  
  7. {  
  8.     string xmlPath = ”C:\\opencv_310\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml”;  
  9.     CascadeClassifier ccf;  
  10.     cv::VideoCapture cap(0);  
  11.     ccf.load(xmlPath);  
  12.     while (waitKey(30) != 27) {  
  13.         Mat img;  
  14.         cap >> img;  
  15.         vector<Rect> faces;  
  16.         Mat gray;  
  17.         cvtColor(img, gray, CV_BGR2GRAY);  
  18.         equalizeHist(gray, gray);  
  19.         ccf.detectMultiScale(gray, faces, 1.1, 3, 0, Size(10, 10), Size(100, 100)); //检测人脸  
  20.         for (vector<Rect>::const_iterator iter = faces.begin(); iter != faces.end(); iter++)  
  21.         {  
  22.             rectangle(img, *iter, Scalar(0, 0, 255), 2, 8); //画出脸部矩形  
  23.         }  
  24.         imshow(”faces”, img);  
  25.     }  
  26. }  
#include "opencv2\opencv.hpp"




include <iostream>

using namespace std;
using namespace cv;

int main()
{
string xmlPath = "C:\opencv_310\build\etc\haarcascades\haarcascade_frontalface_default.xml";
CascadeClassifier ccf;
cv::VideoCapture cap(0);
ccf.load(xmlPath);
while (waitKey(30) != 27) {
Mat img;
cap >> img;
vector<Rect> faces;
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
equalizeHist(gray, gray);
ccf.detectMultiScale(gray, faces, 1.1, 3, 0, Size(10, 10), Size(100, 100)); //检测人脸
for (vector<Rect>::const_iterator iter = faces.begin(); iter != faces.end(); iter++)
{
rectangle(img, *iter, Scalar(0, 0, 255), 2, 8); //画出脸部矩形
}
imshow("faces", img);
}
}


这段代码在实验室这种光照不是非常均匀的场景下真的真的名副其实的非常差………………

大概效果就是下面这样:



//这种情况下也尝试过其他几个Opencv自带的人脸检测模型,效果都非常差,有兴趣的童鞋可以用上面那张图片再试试(毕竟脸还在)


2.于仕琪 人脸检测

总体效果还不错,有四种模型,可以根据具体任务以及测试进行……


下载地址:https://github.com/ShiqiYu/libfacedetection


有空再来写一篇配置的博客(欢迎督促我),总体和Opencv类似,dll和lib对了就能正常跑了,融入到之前Dlib人脸检测的程序中发现,人脸检测的时间大约为20ms,较之于之前的


和Dlib的detector相比,准确率几乎一致(正负六十度左右)





核心代码:

  1. cv::Mat gray;  
  2. cvtColor(temp, gray, CV_BGR2GRAY);  
  3. int * pResults = NULL;  
  4. unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);  
  5.   
  6. pResults = facedetect_frontal(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,  
  7.     1.2f, 2, 48);  
  8. printf(”%d faces detected.\n”, (pResults ? *pResults : 0));  
  9. for (int i = 0; i < (pResults ? *pResults : 0); i++)  
  10. {  
  11.     short * p = ((short*)(pResults + 1)) + 6 * i;  
  12.     int x = p[0];  
  13.     int y = p[1];  
  14.     int w = p[2];  
  15.     int h = p[3];  
  16.     int neighbors = p[4];  
  17.     printf(”face_rect=[%d, %d, %d, %d], neighbors=%d\n”, x, y, w, h, neighbors);  
  18. }  
         cv::Mat gray;
            cvtColor(temp, gray, CV_BGR2GRAY);
            int * pResults = NULL;
            unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);

            pResults = facedetect_frontal(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
                1.2f, 2, 48);
            printf("%d faces detected.\n", (pResults ? *pResults : 0));
            for (int i = 0; i < (pResults ? *pResults : 0); i++)
            {
                short * p = ((short*)(pResults + 1)) + 6 * i;
                int x = p[0];
                int y = p[1];
                int w = p[2];
                int h = p[3];
                int neighbors = p[4];
                printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n", x, y, w, h, neighbors);
            }



3.图片缩放(以Dlib为例)


通过图片的缩放,能极大程度降低人脸检测所需的时间


核心代码:


  1. cv::Mat temp2;    
  2. cv::resize(temp, temp2,cv::Size(640/倍数,480/倍数));  
  3. 时间.开始();  
  4. cv_image<bgr_pixel> cimg2(temp2);  
  5. std::vector<rectangle> faces = detector(cimg2);  
  6.   
  7. for (unsigned long i = 0; i < faces.size(); ++i) {  
  8.       faces[i].set_top(faces[i].top()*倍数);  
  9.    faces[i].set_bottom(faces[i].bottom() * 倍数);  
  10.    faces[i].set_left(faces[i].left()*倍数);  
  11.    faces[i].set_right(faces[i].right()*倍数);  
  12. }  
  13.   
  14. 时间.结束();  
            cv::Mat temp2;  
            cv::resize(temp, temp2,cv::Size(640/倍数,480/倍数));
            时间.开始();
            cv_image<bgr_pixel> cimg2(temp2);
            std::vector<rectangle> faces = detector(cimg2);

            for (unsigned long i = 0; i < faces.size(); ++i) {
               faces[i].set_top(faces[i].top()*倍数);
               faces[i].set_bottom(faces[i].bottom() * 倍数);
               faces[i].set_left(faces[i].left()*倍数);
               faces[i].set_right(faces[i].right()*倍数);
            }

            时间.结束();


这里将长宽缩放成原来的四分之一时可以发现一帧图只需要4ms……



效率比之前有所降低(但肯定有无穷多的办法弥补),大概掉帧20%左右,三倍的时候10%,两倍基本没区别。


尝试差不多就先这么多啦,之后有想法会继续补充。


祝图像处理愉快~







            </div>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值