如何快糙好猛的使用Shiqi.Yu老師的公開人臉檢測庫(附源碼)

前言

本次編寫所用的庫爲於仕祺老師免費提供的人臉檢測庫。真心好用,識別率和識別速度完全不是Opencv自帶的程序能夠比擬的。將其配合Opencv的EigenFace算法,基本上可以形成一個小型的畢業設計。(我是學機械的啊喂!!)

準備工作

1、下載在GitHub上的人臉檢測庫。我不提供百度雲,只提供網址:https://github.com/ShiqiYu/libfacedetection
2、配置好Opencv。

配置人臉檢測庫

1、新建一個MFC程序。
這裏寫圖片描述
2、添加Opencv的屬性表。(即配置Opencv)
這裏寫圖片描述
3、新建一個屬性表,命名爲libfacedetect。需要注意的是,libfacedetect只可用Win32平臺。在VC++目錄添加libfacedetect_master中include文件夾和lib庫的位置。
這裏寫圖片描述
4、在鏈接器中添加附加依賴項。
這裏寫圖片描述

試編寫

1、向MFC中添加一個picture控件,一個button控件,兩個Static Text控件。而後向其中一個Static Text添加變量。
這裏寫圖片描述
2、雙擊button控件,進行代碼頁面,編寫代碼。
添加頭文件:

 #include <opencv.hpp>
 #include "facedetect-dll.h"
 #pragma comment(lib,"libfacedetect.lib")
using namespace cv;

增添一個函數:

void detectAndDisplay(Mat frame)
{
        Mat gray;
        cvtColor(frame, gray, CV_BGR2GRAY);
        int * pResults = NULL;
        pResults = facedetect_frontal_tmp((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step,
            1.2f, 5, 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];

            printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n", x, y, w, h, neighbors);
            Point left(x, y);
            Point right(x + w, y + h);
            rectangle(frame, left, right, Scalar(230, 255, 0), 4);

        }
        imshow("ss", frame);
    }

在按鈕事件中填寫代碼:

VideoCapture cap(0);
    Mat frame;
    while (1)
    {
        //load an image and convert it to gray (single-channel)
        //Mat gray = imread("lena.png");//it is necessary that must have CV_LOAD_IMAGE_GRAYSCALE
        //cvtColor(gray, gray, CV_BGR2GRAY);//CV_LOAD_IMAGE_GRAYSCALE IS SAME AS CV_BGR2GRAY
        cap >> frame;
        if (!frame.empty())
        {
            detectAndDisplay(frame);
        }
        int c = waitKey(10);
        if ((char)c == 'c') { break; }
    }

(非本人照片)
可以看到,已經檢測成功,不過沒有在控件上顯示。
這裏寫圖片描述

可以通過添加來讓其顯示到框上。

namedWindow("view", WINDOW_AUTOSIZE);
    HWND hWnd = (HWND)cvGetWindowHandle("view");
    HWND hParent = ::GetParent(hWnd);
    ::SetParent(hWnd, GetDlgItem(ID_FACE)->m_hWnd);

這裏寫圖片描述

我們來看看這個函數逆天的檢測時間:增添檢測時間的代碼:

t = (double)cvGetTickCount();
t = (double)cvGetTickCount() - t;
detect_time = t / 1000 / ((double)cvGetTickFrequency()*1000.);
UpdateData(FALSE);

這裏寫圖片描述
於老師最近有一篇文章中寫到,他的這個人臉檢測庫是基於LBP與Boost相結合的。所以如果我們選取了足夠多,足夠好的樣本,參數設置正確的話,識別效果從理論上也能遠遠高於Opencv自帶的。此外就是在最近做項目的過程中,發現人臉檢測這一端還是不能用深度學習的方法,其一是DL目前還沒有能夠很方面移植的硬件,其二是如果在雲端進行的話,傳輸速度將會對識別效率造成影響。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章