libfacedetection

libfacedetection测试

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <facedetect-dll.h>


//define the buffer size. Do not change the size!
//定义缓冲区大小 不要改变大小!
#define DETECT_BUFFER_SIZE 0x20000
using namespace cv;

unsigned char * pBuffer;

void facedetect_frontal_surveillance1(Mat image);

int main()
{
    VideoCapture video1(0);
    Mat image;
    while (1)
    {
        pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
        if (!pBuffer)
        {
            fprintf(stderr, "Can not alloc buffer.\n");
            return -1;
        }

        //load an image and convert it to gray (single-channel)
        //加载图像并将其转换为灰色(单通道)
        //Mat image = imread("E:\\libfacedetection\\libfacedetection_install\\images\\keliamoniz1.jpg");
        video1.read(image);
        //Mat image = imread("E:\\libfacedetection\\libfacedetection_install\\images\\1.jpg");
        if (image.empty())
        {
            return -1;
        }
        facedetect_frontal_surveillance1(image);

        //waitKey();
        char c1 = waitKey(10);
        if (c1 == 27)
        {
            break;
        }

        free(pBuffer);
    }
    
    
    //release the buffer
    

    return 0;
}


void facedetect_frontal_surveillance1(Mat image)
{
    int w1 = image.cols;
    int h1 = image.rows;

    Mat gray;
    cvtColor(image, gray, CV_BGR2GRAY);

    int min_obj_width = 8;
    float scale1 = 1.1f;
    int min_neightbors1 = 2;

    int * pResults = NULL;
    //pBuffer is used in the detection functions.
    //pBuffer指针用于检测函数。
    //If you call functions in multiple threads, please create one buffer for each thread!
    //如果您在多个线程中调用函数,请为每个线程创建一个缓冲区!
    
    int doLandmark = 2;

    ///////////////////////////////////////////
    // frontal face detection designed for video surveillance / 68 landmark detection
    //正面人脸检测专为视频监控/ 68标志性检测而设计
    // it can detect faces with bad illumination.
    //它可以检测到不良照明的面部。
    //////////////////////////////////////////
    //!!! The input image must be a gray one (single-channel)
    //!!! DO NOT RELEASE pResults !!!
    pResults = facedetect_frontal_surveillance(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
        scale1, min_neightbors1, min_obj_width, 0, doLandmark);
    //printf("%d faces detected.\n", (pResults ? *pResults : 0));
    Mat result_frontal_surveillance = image.clone();;
    //print the detection results
    for (int i = 0; i < (pResults ? *pResults : 0); i++)
    {
        short * p = ((short*)(pResults + 1)) + 142 * 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);
        rectangle(result_frontal_surveillance, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
        if (doLandmark)
        {
            for (int j = 0; j < 68; j++)
                circle(result_frontal_surveillance, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
        }
    }

    if (w1>1600)
    {
        resize(result_frontal_surveillance, result_frontal_surveillance, Size(w1 / 4, h1 / 4));
    }


    imshow("Results_frontal_surveillance", result_frontal_surveillance);

}

 

posted @ 2019-10-07 22:19  西北逍遥  阅读(457)  评论(0编辑  收藏  举报