前言

最近已经很少看CSDN了。这一年多准备考研,基本上怕是不会再怎么上了。以前有一个http://blog.csdn.net/mr_curry/article/details/51804072 如何快糙好猛的使用Shiqi.Yu老师的公开人脸检测库(附源码)的BLOG,因为于老师的库已经更新了,所以重新写一下吧。
PS:这个库越来越强了,已经可以做人脸关键点检测了。关键点检测可以用于矫正人脸,再也不要用慢的要死的dlib啦~~

配置

五张图带你解决问题:(X64,Debug)
这里写图片描述
这里写图片描述
这里写图片描述

然后你需要把opencv的属性表也引进来:
这里写图片描述

两个方法,加系统变量或者放到和exe同一个文件夹下。加了系统变量后重启一次才生效,所以这里就直接放咯
这里写图片描述

代码

我们直接用FDDB上评测效果最好的函数:facedetect_multiview_reinforce
这里写图片描述

#include <opencv.hpp>
#include <facedetect-dll.h>
using namespace cv;
using namespace std;

//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000

int main()
{
    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;
    }
    Mat src = imread("img.jpg");
    Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY);
    int doLandmark = 1;// do landmark detection
    pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
        1.2f, 2, 48, 0, doLandmark);
    //print the detection results
    for (int i = 0; i < (pResults ? *pResults : 0); i++)
    {
        short * p = ((short*)(pResults + 1)) + 142 * i;
        rectangle(src, Rect(p[0], p[1], p[2], p[3]), Scalar(0, 255, 0), 2);
        if (doLandmark)
        {
            for (int j = 0; j < 68; j++)
                circle(src, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 0, 255),2);
        }
    }
    imshow("Show", src);
    waitKey(0);
}

效果还是很赞:
这里写图片描述

视频流中的人脸检测代码就是用VideoCapture解析为Mat然后循环检测啊:

#include <opencv.hpp>
#include <facedetect-dll.h>
using namespace cv;
using namespace std;

//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000

int main()
{
    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;
    }
    int doLandmark = 1;// do landmark detection
    VideoCapture cap(0);
    if (!cap.isOpened()){
        cout << "Please check your USB camera's interface num." << endl;
        return 0;
    }
    Mat src;
    while (true)
    {
        cap >> src;
        if (!src.empty()){
            Mat gray;
            cvtColor(src, gray, CV_BGR2GRAY);
            pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
                1.2f, 2, 48, 0, 1);
            for (int i = 0; i < (pResults ? *pResults : 0); i++)
            {
                short * p = ((short*)(pResults + 1)) + 142 * i;
                rectangle(src, Rect(p[0], p[1], p[2], p[3]), Scalar(0, 255, 0), 2);
                if (doLandmark)
                {
                    for (int j = 0; j < 68; j++)
                        circle(src, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 0, 255), 2);
                }
            }
            imshow("Show", src);
            waitKey(1);
        }

    }
}
posted on 2017-03-25 20:20  未雨愁眸  阅读(466)  评论(0编辑  收藏  举报