opencv人脸识别
opencv人脸识别:
首先我们来简单的讲一下人脸识别流程:这里采用的是vs2017+opencv3.3扩展库
1、进行人脸检测
2、识别器训练与分类
3、人脸检测完和识别器进行预测
4、圈出人脸并在人脸上面显示人名
注:这里用的库比较多是opencv扩展库中的,官方的库会报错(因为没有相应的EigenFaceRecognizer类模板无法进行识别器的训练分类) 可以参考上一篇博客如何编译opencv扩展库 还要注意的是不同版本的识别器创建的写法会不同,最好百度查一下api(这个也是搞了我蛮久的)
Code:
//face_recog_from_video.cpp 定义控制台应用程序的入口点。
#include "pch.h" //预编译 不同版本不同
#include "opencv2/opencv.hpp"
#include "opencv2/face.hpp"
#include <stdio.h>
#include<iostream>
using namespace std;
using namespace cv;
using namespace cv::face;
/** Function Headers */
void detectAndDisplay(Mat frame);
/** Global variables */
String face_cascade_name = "G:\\opencv\\opencv+\\opencvmake\\install\\etc\\haarcascades\\haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
String window_name = "Capture - Face Recognition";
string face1_path = "D:\\xue.jpg";//1
string face2_path = "D:\\zxy.jpg";//2
string face3_path = "D:\\zly.jpg";//3
string face4_path = "D:\\my.jpg";//我->4号
Ptr<EigenFaceRecognizer> modelPCA = EigenFaceRecognizer::create();// 创建特征脸模型 EigenFaceRecognizer
/** @function main */
int main(int argc, const char** argv)
{
vector<Mat> images;
images.push_back(imread(face1_path, CV_LOAD_IMAGE_GRAYSCALE));
images.push_back(imread(face2_path, CV_LOAD_IMAGE_GRAYSCALE));
images.push_back(imread(face3_path, CV_LOAD_IMAGE_GRAYSCALE));
images.push_back(imread(face4_path, CV_LOAD_IMAGE_GRAYSCALE));
vector<int> labels(4);
for (int i = 1;i <= 4;i++)
labels[i - 1] = i;
//训练
modelPCA->train(images, labels);
VideoCapture capture;
Mat frame;
//-- 1. Load the cascades
if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading face cascade\n"); return -1; };
//-- 2. Read the video stream
capture.open(0); //打开摄像头
if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }
while (capture.read(frame)) //读取帧
{
if (frame.empty())
{
printf(" --(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay(frame);
if (waitKey(10) == 'k') { break; } // escape
}
return 0;
}
/** @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY); //BGR 转化为灰度图
equalizeHist(frame_gray, frame_gray); //直方图均衡化
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(60, 60));
for (size_t i = 0; i < faces.size(); i++)
{
Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2); // 人脸中心坐标
ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0); // 椭圆
Mat faceROI = frame_gray(faces[i]);
Mat face_test;
// 调整大小为92*112
resize(faceROI, face_test, Size(92, 112));
imwrite("lisa.pgm", face_test);
double confidence;
int predictPCA;
modelPCA->predict(face_test, predictPCA, confidence);
cout << "the predict result is " << predictPCA << endl << "confidence is " << confidence << endl;
if (predictPCA == 4)
{
putText(frame, "Hello lzw", Point(faces[i].x, faces[i].y), FONT_HERSHEY_SIMPLEX, 1.5, Scalar(0, 0, 255), 2);
}
}
//-- Show what you got
imshow(window_name, frame);
}
哈哈哈~~~图片被做了一点处理,大致就是这样子啦
参考博客:https://blog.csdn.net/u012679707/article/details/80410094
这里还是有个问题,图片的识别器训练与分类需要在代码中实现,如果图片数目比较多的话 工作量就会比较大而且也比较耗时。
下面是用csv文件的形式实现识别器的训练与分类 可以很方便的处理图片数目多的训练
https://blog.csdn.net/u012679707/article/details/80397012
还没有评论,来说两句吧...