opencv人臉識(shí)別-開源庫(kù)
開源庫(kù)的簡(jiǎn)介
- 這個(gè)開源庫(kù)是深圳大學(xué)的一個(gè)教授(于仕琪)寫的季俩,其中比較有名的就是維護(hù)著opencv中文站,翻譯了learning opencv等書籍聊闯;(https://github.com/ShiqiYu/libfacedetection)
- 其實(shí)想自己寫一點(diǎn)的麦向,但是又不想更改原創(chuàng)老師的本意子檀,就以于老師的為準(zhǔn)吧(A fast binary library for face detection and face landmark detection in images. The face detection speed can reach 1500FPS. )是不是很變態(tài)的數(shù)字1500fps;
導(dǎo)入的步奏
新建一個(gè)工程+源文件/添加/現(xiàn)有項(xiàng)+example code陶衅;
點(diǎn)擊屬性+/C/C++ +/常規(guī)+/附加包含目錄/+導(dǎo)入的include路徑名屡立;D:\code\opencv\libfacedetection-master\libfacedetection-master\include
屬性+/鏈接器+/輸入 + /附加依賴項(xiàng) libfacedetect.lib 、libfacedetect-x64.lib
此時(shí)就可以運(yùn)行了搀军,會(huì)出現(xiàn)錯(cuò)誤膨俐,但同時(shí)也會(huì)生成debug文件,將那兩個(gè)dll文件拷貝到這個(gè)文件夾下面 libfacedetect.dll 和 libfacedetect-x64.dll
會(huì)出現(xiàn)此類問題錯(cuò)誤 error LNK1104: 無法打開文件“l(fā)ibfacedetect.lib” D:\code\opencv\facetest-test\facetest-test\LINK facetest-test
解決方案: 屬性+/鏈接器+/常規(guī) + /啟用增量鏈接 改為是 并添加/附加庫(kù)目錄 +D:\code\opencv\libfacedetection-master\libfacedetection-master\lib
代碼的修改
- 由于樣例代碼是做單張圖片的識(shí)別并標(biāo)注的罩句,若是想要實(shí)時(shí)的識(shí)別必須改為攝像頭實(shí)時(shí)獲取到的圖像焚刺,
- 由于代碼中涉及到了四種方法,可以分別注釋的方式來看運(yùn)行的速度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);
}