前言
最近已經很少看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);
}
}
}