一个快速的人脸检测库

这是我见过和使用过的最快的人脸检测库,而且还能检测到人脸转动的角度。
在此声明!本博客旨在学习研究交流之用,如果要用于商业目的,请直接联系于诗琪老师,获得授权!如有法律问题,本人概不负责!
于诗琪老师网页连接:http://www.opencv.org.cn/forum.php?mod=viewthread&tid=34155
在于老师提供的代码里有一个例程,我只是对例程做了个简单的中文注释!

/* The MIT License (MIT) Copyright (c) 2015 Shiqi Yu [email protected] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include <opencv.hpp>

#include "facedetect-dll.h"
#pragma comment(lib,"libfacedetect.lib")

using namespace cv;

int main(int argc, char* argv[])
{
    //load an image and convert it to gray (single-channel)
    //加载衣服图片并转换成灰度图
    Mat gray = imread("lena.png", CV_LOAD_IMAGE_GRAYSCALE); 
    if(gray.empty())//如果转换成灰度图没有关系
    {
        fprintf(stderr, "Can not load the image file.\n");
        return -1;
    }

    int * pResults = NULL; 

    ///////////////////////////////////////////
    // frontal face detection 
    // it's fast, but cannot detect side view faces 检测正脸是快速的但是不能检测侧脸
    //////////////////////////////////////////
    //!!! The input image must be a gray one (single-channel) 输入图像必须是灰度图,单通道
    //!!! DO NOT RELEASE pResults !!! 不要释放指针
    pResults = facedetect_frontal((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step,
                                               1.2f, 2,  24);//facedetect_frontal 检测正脸,返回一个box (x,y,w,h)
    printf("%d frontal faces detected.\n", (pResults ? *pResults : 0));
    //print the detection results
    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);
    }

    ///////////////////////////////////////////
    // multiview face detection 
    // it can detection side view faces, but slower than the frontal face detection.
    // 多脸检测,能检测侧脸,但是比检测正脸慢,能检测人脸转动的角度
    //////////////////////////////////////////
    //!!! The input image must be a gray one (single-channel)
    //!!! DO NOT RELEASE pResults !!!
    pResults = facedetect_multiview((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step,
                                               1.2f, 4, 24);
    printf("%d faces detected.\n", (pResults ? *pResults : 0));

    //print the detection results
    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];
        int angle = p[5];//***能返回人脸转动角度

        printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);
    }


    ///////////////////////////////////////////
    // reinforced multiview face detection 
    // it can detection side view faces, but slower than the frontal face detection.
    //加强版,多脸检测
    //////////////////////////////////////////
    //!!! The input image must be a gray one (single-channel)
    //!!! DO NOT RELEASE pResults !!!
    pResults = facedetect_multiview_reinforce((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step,
                                               1.2f, 4, 24);
    printf("%d faces detected.\n", (pResults ? *pResults : 0));

    //print the detection results
    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];
        int angle = p[5];//返回一个角度

        printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);
    }

    return 0;
}

你可能感兴趣的:(人脸检测)