基于摄像头
// libfacedec.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdafx.h"
#include //加载OPENCV库所需的头文件
#include //加载libfacedetection库所需的头文件
#include
#include
#include
#include
using namespace std;
using namespace cv;
//提示音文件库
#pragma comment(lib, "winmm.lib")
//定义一个缓冲区,大小为:0x20000
#define DETECT_BUFFER_SIZE 0x20000
int _tmain(int argc, _TCHAR* argv[])
{
Mat src;
int scale = 2;
int * pResults = NULL;
// 向系统申请分配指定size个字节的内存空间
unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
if (!pBuffer)
{
fprintf(stderr, "Can not alloc buffer.\n");
return -1;
}
VideoCapture cap(0);//加载USB摄像头
if (!cap.isOpened())
{
cout <<"Please check your USB camera's interface num." < return -1;
}
while (true)
{
cap >> src;
resize(src, src, Size(src.cols / scale, src.rows / scale), 1);
time_t t = time(0);
/*char tmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A", localtime(&t));
Point pt(20, 25);
Scalar color = CV_RGB(0, 255, 255);
putText(src, tmp, pt, CV_FONT_HERSHEY_DUPLEX, 0.5f, color);*/
double t1 = 0;
t1 = (double)cvGetTickCount();//用来计算算法执行时间
if (!src.empty())
{
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);//图像灰度处理
//加载人脸检测库(libfacedetection),用来检测人脸。参数说明:pBuffer:用于存储人脸检测结果的缓冲存储器,大小为0x20000字节!gray.ptr(0):
//输入图像必须是单通道的灰度图像gray.cols:灰度图像的列。gray.rows:灰度图像的行。gray.step:灰度图像的深度。
if (1)
{
pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
//pResults = facedetect_multiview(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
//加载人脸检测库(libfacedetection)后,会通过68个点来描述人脸面部特征,还可以得到人脸框的信息Rect(p[0], p[1],p[2], p[3])左上角坐标
//:(p[0],p[1])、宽度:p[2]、高度:p3、邻近成员:p[4]、人脸偏转角度:p[5],这些数据都会对后面的检测有帮助。
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 );
// 绘制人脸特征点68个
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 );
}
}
}
resize(src, src, Size(src.cols*scale, src.rows*scale), 1);
imshow("测试视频", src);
waitKey(1);
}
t1 = (double)cvGetTickCount() - t1;//相减为算法执行的时间
printf("detection time = %g ms\n", t1 / ((double)cvGetTickFrequency()*1000.));
if (waitKey(1) == 27) //wait for 'esc' key press for 10 ms. If 'esc' key is pressed, break loop
{
cout <<"video paused!, press q to quit, any other key to continue" < if (waitKey(0) == 'q')
{
cout <<"terminated by user" < break;
}
}
}
return 0;
}
基于图片的
// libfacedec.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdafx.h"
#include //加载OPENCV库所需的头文件
#include //加载libfacedetection库所需的头文件
#include
#include
#include
#include
using namespace std;
using namespace cv;
//提示音文件库
#pragma comment(lib, "winmm.lib")
//定义一个缓冲区,大小为:0x20000
#define DETECT_BUFFER_SIZE 0x20000
int _tmain(int argc, _TCHAR* argv[])
{
int scale = 2;
int * pResults = NULL;
// 向系统申请分配指定size个字节的内存空间
unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
if (!pBuffer)
{
fprintf(stderr, "Can not alloc buffer.\n");
return -1;
}
//VideoCapture cap(0);//加载USB摄像头
/*if (!cap.isOpened())
{
cout <<"Please check your USB camera's interface num." < return -1;
}*/
Mat src = imread("D://vvoo//14.jpg");
//cap >> src;
//resize(src, src, Size(src.cols / scale, src.rows / scale), 1);
time_t t = time(0);
/*char tmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A", localtime(&t));
Point pt(20, 25);
Scalar color = CV_RGB(0, 255, 255);
putText(src, tmp, pt, CV_FONT_HERSHEY_DUPLEX, 0.5f, color);*/
if (!src.empty())
{
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);//图像灰度处理
//加载人脸检测库(libfacedetection),用来检测人脸。参数说明:pBuffer:用于存储人脸检测结果的缓冲存储器,大小为0x20000字节!gray.ptr(0):
//输入图像必须是单通道的灰度图像gray.cols:灰度图像的列。gray.rows:灰度图像的行。gray.step:灰度图像的深度。
if (1)
{
pResults = facedetect_multiview_reinforce(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
//pResults = facedetect_multiview(pBuffer, (unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1);
//加载人脸检测库(libfacedetection)后,会通过68个点来描述人脸面部特征,还可以得到人脸框的信息Rect(p[0], p[1],p[2], p[3])左上角坐标
//:(p[0],p[1])、宽度:p[2]、高度:p3、邻近成员:p[4]、人脸偏转角度:p[5],这些数据都会对后面的检测有帮助。
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 );
// 绘制人脸特征点68个
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);
}
}
}
resize(src, src, Size(src.cols*scale, src.rows*scale), 1);
imshow("测试视频", src);
imwrite("D://vvoo//122.jpg", src);
waitKey(0);
}
return 0;
}