美文网首页
opencv人脸识别-开源库

opencv人脸识别-开源库

作者: lemonCode | 来源:发表于2017-05-18 10:47 被阅读427次

    opencv人脸识别-开源库

    开源库的简介

    1. 这个开源库是深圳大学的一个教授(于仕琪)写的,其中比较有名的就是维护着opencv中文站,翻译了learning opencv等书籍;(https://github.com/ShiqiYu/libfacedetection)
    2. 其实想自己写一点的,但是又不想更改原创老师的本意,就以于老师的为准吧(A fast binary library for face detection and face landmark detection in images. The face detection speed can reach 1500FPS. )是不是很变态的数字1500fps;

    导入的步奏

    1. 新建一个工程+源文件/添加/现有项+example code;

    2. 点击属性+/C/C++ +/常规+/附加包含目录/+导入的include路径名;D:\code\opencv\libfacedetection-master\libfacedetection-master\include

    3. 属性+/链接器+/输入 + /附加依赖项 libfacedetect.lib 、libfacedetect-x64.lib

    4. 此时就可以运行了,会出现错误,但同时也会生成debug文件,将那两个dll文件拷贝到这个文件夹下面 libfacedetect.dll 和 libfacedetect-x64.dll

    5. 会出现此类问题错误 error LNK1104: 无法打开文件“libfacedetect.lib” D:\code\opencv\facetest-test\facetest-test\LINK facetest-test

    6. 解决方案: 属性+/链接器+/常规 + /启用增量链接 改为是 并添加/附加库目录 +D:\code\opencv\libfacedetection-master\libfacedetection-master\lib

    代码的修改

    1. 由于样例代码是做单张图片的识别并标注的,若是想要实时的识别必须改为摄像头实时获取到的图像,
    2. 由于代码中涉及到了四种方法,可以分别注释的方式来看运行的速度fps;

    直接上代码

    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    #include "facedetect-dll.h"
    
    //#pragma comment(lib,"libfacedetect.lib")
    #pragma comment(lib,"libfacedetect-x64.lib")
    
    //define the buffer size. Do not change the size!
    #define DETECT_BUFFER_SIZE 0x20000
    using namespace cv;
    //unsigned char * pBuffer1=NULL;
    //unsigned char * pBuffer2 = NULL;
    //unsigned char * pBuffer3 = NULL;
    unsigned char * pBuffer4 = NULL;
    
    
    void  detectAndDraw(Mat image);
    int main(int argc, char* argv[])
    {
     /*   if(argc != 2)
    {
        printf("Usage: %s <image_file_name>\n", argv[0]);
        return -1;
    }*/
    
    cv::VideoCapture camera(CV_CAP_ANY);
    if (!camera.isOpened())
        return -1;
    
    
    
    //load an image and convert it to gray (single-channel)
    //Mat image = imread(argv[1]); 
    Mat image;
    
    while (true){
    
        if (!camera.read(image))break;
            detectAndDraw(image);
            int c = waitKey(20);
            if ((char)c == 27)break;
    }
    
    
    
    
    //cvDestroyWindow("result");
    camera.release();
    
    //release the buffer
    //free(pBuffer1);
    //free(pBuffer2);
    //free(pBuffer3);
    free(pBuffer4);
    return 0;
    }
    
    
    void  detectAndDraw(Mat image){
    Mat gray;
    cvtColor(image, gray, CV_BGR2GRAY);
    
    
    int * pResults = NULL;
    //pBuffer is used in the detection functions.
    //If you call functions in multiple threads, please create one buffer for each thread!
    int doLandmark = 1;
    
    //if (pBuffer1)
    //{
    //  //fprintf(stderr, "Can not alloc buffer.\n");
    //  //return -1;
    //}
    //else{
    //  pBuffer1 = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
    //}
    
    
    
    ///////////////////////////////////////////
    // frontal face detection / 68 landmark 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(pBuffer1, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
    //  1.2f, 2, 48, 0, doLandmark);
    
    //printf("%d faces detected.\n", (pResults ? *pResults : 0));
    //Mat result_frontal = 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, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
    //  if (doLandmark)
    //  {
    //      for (int j = 0; j < 68; j++)
    //          circle(result_frontal, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
    //  }
    //}
    //imshow("Results_frontal", result_frontal);
    
    
    /////////////////////////////////////////////
    //// frontal face detection designed for video surveillance / 68 landmark detection
    //// it can detect faces with bad illumination.
    ////////////////////////////////////////////
    ////!!! The input image must be a gray one (single-channel)
    ////!!! DO NOT RELEASE pResults !!!
    
    //if (pBuffer2)
    //{
    //  //fprintf(stderr, "Can not alloc buffer.\n");
    //  //return -1;
    //}
    //else{
    //   pBuffer2 = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
    //}
    //pResults = facedetect_frontal_surveillance(pBuffer2, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
    //  1.2f, 2, 48, 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));
    //  }
    //}
    //imshow("Results_frontal_surveillance", result_frontal_surveillance);
    
    
    /////////////////////////////////////////////
    //// multiview face detection / 68 landmark detection
    //// it can detect side view faces, but slower than facedetect_frontal().
    ////////////////////////////////////////////
    ////!!! The input image must be a gray one (single-channel)
    ////!!! DO NOT RELEASE pResults !!!
    
    //if (pBuffer3)
    //{
    //  //fprintf(stderr, "Can not alloc buffer.\n");
    //  //return -1;
    //}
    //else{
    //   pBuffer3 = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
    //}
    //pResults = facedetect_multiview(pBuffer3, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
    //  1.2f, 2, 48, 0, doLandmark);
    
    //printf("%d faces detected.\n", (pResults ? *pResults : 0));
    //Mat result_multiview = 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_multiview, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
    //  if (doLandmark)
    //  {
    //      for (int j = 0; j < 68; j++)
    //          circle(result_multiview, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
    //  }
    //}
    //imshow("Results_multiview", result_multiview);
    
    
    /////////////////////////////////////////////
    //// reinforced multiview face detection / 68 landmark detection
    //// it can detect side view faces, better but slower than facedetect_multiview().
    ////////////////////////////////////////////
    ////!!! The input image must be a gray one (single-channel)
    ////!!! DO NOT RELEASE pResults !!!
    
    if (pBuffer4)
    {
        //fprintf(stderr, "Can not alloc buffer.\n");
        //return -1;
    }
    else{
        pBuffer4 = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
    }
    pResults = facedetect_multiview_reinforce(pBuffer4, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step,
        1.2f, 3, 48, 0, doLandmark);
    
    printf("%d faces detected.\n", (pResults ? *pResults : 0));
    Mat result_multiview_reinforce = 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_multiview_reinforce, Rect(x, y, w, h), Scalar(0, 255, 0), 2);
        if (doLandmark)
        {
            for (int j = 0; j < 68; j++)
                circle(result_multiview_reinforce, Point((int)p[6 + 2 * j], (int)p[6 + 2 * j + 1]), 1, Scalar(0, 255, 0));
        }
    }
    imshow("Results_multiview_reinforce", result_multiview_reinforce);
    
    }
    

    相关文章

      网友评论

          本文标题:opencv人脸识别-开源库

          本文链接:https://www.haomeiwen.com/subject/qxcsxxtx.html