LBPH算法
在这里插入代码片
读取并保存人脸数据
#include "pch.h"
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
string haar_face_datapath = "E:/opencv/opencv3.4/opencv/build/etc/haarcascades/haarcascade_frontalface_alt_tree.xml";
int main(int argc, char** argv)
{
VideoCapture capture(0);
if (!capture.isOpened())
{
cout << "could not open the camera..." << endl;
return -1;
}
Size S = Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH), (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT));
int fps = capture.get(CV_CAP_PROP_FPS);
CascadeClassifier faceDetector;
faceDetector.load(haar_face_datapath);
Mat frame;
namedWindow("camera-demo", CV_WINDOW_NORMAL);
vector<Rect> faces;
int count = 0;
while (capture.read(frame))
{
flip(frame, frame, 1);
faceDetector.detectMultiScale(frame, faces, 1.01, 1, 0, Size(100, 120), Size(380, 400));
for (int i = 0; i < faces.size(); i++)
{
if (count % 10 == 0)
{
//将图像保存为同一个大小
Mat dst;
resize(frame(faces[i]), dst, Size(100, 100));
//保存图像
imwrite(format("D:/face/face_%d.jpg", count), dst);
}
rectangle(frame, faces[i], Scalar(0, 0, 255), 2, 8, 0);
}
imshow("camera_demo", frame);
char c = waitKey(10);
if (c == 27)
{
break;
}
count++;
}
capture.release();
waitKey(0);
return 0;
}
capture.release();
waitKey(0);
return 0;
}
训练形成平均脸文件