我是靠谱客的博主 动人小鸽子,最近开发中收集的这篇文章主要介绍利用opencv自带库进行人脸检测与识别,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

opencv3.2+contrib 利用自带人脸检测库检测与识别人脸

头文件face-recognize.h:

#pragma once
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
#include <iostream>
#include <string>

using namespace cv;
using namespace cv::face;
using namespace std;

class face_recognizer {
	string base_dir;
	string index_filename;
	string recognizer_filename;
	string face_detector_filename;

private:
	Ptr<LBPHFaceRecognizer> model;

public:
	face_recognizer();

	~face_recognizer();

	Ptr<LBPHFaceRecognizer> getModel() {
		return model;
	}

	int face_train();
	int face_detect(Mat & src, Mat & dst);
	int face_add(Ptr<LBPHFaceRecognizer> model, Mat image, string name, int label);
	int face_recognize(string srcDir);

};

C++文件:

#include "face-recognize.h"


face_recognizer::face_recognizer() {
	base_dir = "C:/develop/idress/model_face_recognize/";
	index_filename = base_dir + "data/image_index.csv";
	recognizer_filename = base_dir + "data/faceModel.xml";
	face_detector_filename = base_dir + "data/haarcascade_frontalface_alt.xml";
	//model = createLBPHFaceRecognizer(1, 8, 8, 8, 50.0);
	model = createLBPHFaceRecognizer();
	model->load(recognizer_filename);
	
}

face_recognizer::~face_recognizer(){}

int face_recognizer::face_recognize(string srcDir) {

	//int new_face_id = 5;

	// 训练出自己的级联分类器
	//face_train();

	//创建识别器
	//Ptr<LBPHFaceRecognizer> model = createLBPHFaceRecognizer(1, 8, 8, 8, 50.0);
	//Ptr<LBPHFaceRecognizer> model = createLBPHFaceRecognizer();

	//加载已有的分类器文件
	//model->load(recognizer_filename);

	Ptr<LBPHFaceRecognizer> model = this->getModel();

	//准备待检测的人脸图片
	Mat src, dst,gray;
	src = imread(srcDir);
	if (face_detect(src, dst) < 0) {
		printf(" no facen");
		return -1;
	}
	cvtColor(dst, gray, COLOR_BGR2GRAY);

	//识别
	int predictedLabel = -1;
	double predicted_confidence = 0.0;
	model->predict(gray, predictedLabel, predicted_confidence);

	if (predictedLabel < 0 ) {   //或predicted_confidence>50
		//未识别用户,返回最近似的人脸
		//face_add(model, src, to_string(new_face_id), new_face_id);

	}
	else {
		printf(" predict label :  %dn", predictedLabel);
	}
	
	waitKey(0);
	return 0;

}


int face_recognizer::face_detect(Mat & src, Mat & dst) {
	Mat gray;
	//imshow("src image", src);
	cvtColor(src, gray, COLOR_BGR2GRAY);
	equalizeHist(gray, gray);

	vector<Rect> faces;
	CascadeClassifier face_detector;

	if (!face_detector.load(face_detector_filename)) {
		printf("could not load face feature data...n");
		return -1;
	}

	face_detector.detectMultiScale(gray, faces, 1.3, 5, 0, Size(24, 24));

	if (faces.size() == 0) {
		return -1;
	}
	for (size_t t = 0; t < faces.size(); t++) {
		//Mat dst;
		rectangle(src, faces[static_cast<int>(t)], Scalar(0, 0, 255), 2, 8, 0);
		resize(src(faces[t]), dst, Size(128, 128));
		//imwrite("C:/develop/idress/model_face_recognize/face_store/" + name, dst);
	}

	//imshow("detect faces", dst);
	//waitKey(0);
	return 0;
}


int face_recognizer::face_train() {
	ifstream file(index_filename.c_str(), ifstream::in);
	if (!file) {
		printf("could not load file correctly...n");
		return -1;
	}

	string line, path, classlabel;
	vector<Mat> images;
	vector<int> labels;
	char separator = ';';
	while (getline(file, line)) {
		stringstream liness(line);
		getline(liness, path, separator);
		getline(liness, classlabel);
		if (!path.empty() && !classlabel.empty()) {
			images.push_back(imread(path, 0));
			labels.push_back(atoi(classlabel.c_str()));
		}
	}

	if (images.size() < 1 || labels.size() < 1) {
		printf("invalid image path...n");
		return -1;
	}

	// train it
	//Ptr<BasicFaceRecognizer> model = createEigenFaceRecognizer();
	Ptr<LBPHFaceRecognizer> model = createLBPHFaceRecognizer();

	model->train(images, labels);
	model->save(recognizer_filename);

	return 0;
}


int face_recognizer::face_add(Ptr<LBPHFaceRecognizer> model, Mat image, string name, int label) {

	//更新识别器,保存分类器文件
	vector<Mat> images;
	vector<int> labels;
	Mat grayImage;
	cvtColor(image, grayImage, CV_BGR2GRAY);
	images.push_back(grayImage);
	labels.push_back(label);
	model->update(images, labels);
	model->save(recognizer_filename);

	//将图片保存至人脸库,并更新标签列表
	ofstream ofile;
	ofile.open(index_filename, ios::app);
	//ofile << face_store_dir + name << ".PNG;" << label << endl;

	//imwrite(face_store_dir + name+".PNG", image);


	ofile.close();
	return 0;
}

 

最后

以上就是动人小鸽子为你收集整理的利用opencv自带库进行人脸检测与识别的全部内容,希望文章能够帮你解决利用opencv自带库进行人脸检测与识别所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部