在现有的人脸识别程序上进行细微修改,因为电脑没有摄像头,因此将原先的从摄像头读入图片改为从文件批量读入,主程序修改如下:
#include "cvlib.hpp"
#include "FaceDetect.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string.h>
using namespace std;
using namespace cv;
int count = 1;
int j = 0;
int main( void )
{
/* VideoCapture cap( 0 ); //获取默认摄像头
if( !cap.isOpened() )
{
std::cout<< "can not open your camera!"<< std::endl;
return -1;
}
*/
CascadeClassifier cascade;//级联分类器,一个识别人脸,一个识别人眼
cascade.load("haarcascade_frontalface_alt.xml");//前脸(OPENCV已训练好的数据,存放在XML里面)
for (j = 73; j < 100;j++)
{
string num_img;
stringstream ss;
ss << j;
ss >> num_img;
num_img = "D:\\C++_file\\FaceRegnition\\FaceRegnition\\image\\lwc\\" + num_img + ".jpg";
bool stop = false;
Mat frame;//存放摄像头获取的图像
frame = imread(num_img);
FaceDetect FD(cascade);
cvNamedWindow("image");
//while( !stop )//持续读取摄像头图像
//{
//cap>> frame;//获取摄像头的图像
imshow("image",frame);
FD.Run(frame, 2);
waitKey(10);
//}
}
return 0;
}
加粗的注释部分为源代码,即使用摄像头读取图片的代码。
在该程序中,文件夹中的图片命名格式有一定的要求:必须命名成1.jpg,2.jpg等。