我是靠谱客的博主 稳重花卷,最近开发中收集的这篇文章主要介绍opencv人脸识别 C++(附视频链接),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

之前想做一个好玩的人脸识别。就学习opencv这个开源工具,下面的是具体的代码实现。

贴一个哔哩哔哩演示的视频opencv人脸识别演示


faceidentify.hpp

//
//
faceidentify.hpp
//
abilitytpratice
//
//
Created by Mr_zhou on 24.5.21.
//
#ifndef faceidentify_hpp
#define faceidentify_hpp
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace cv;
using namespace cv::face;
using namespace std;
class faceidentify
{
public:
void saveimage();
public:
void identify();
private:
// 级联器
cv::CascadeClassifier faceCascade;
cv::CascadeClassifier eyeCascade;
string facexml = "/opt/homebrew/Cellar/opencv/4.5.1_3/share/opencv4/haarcascades/haarcascade_frontalface_alt2.xml";
string eyexml = "/opt/homebrew/Cellar/opencv/4.5.1_3/share/opencv4/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
string savefacexml = "/opt/homebrew/Cellar/opencv/4.5.1_3/share/opencv4/haarcascades/haarcascade_frontalface_alt_tree.xml";
};
#endif /* faceidentify_hpp */

faceidentify.cpp

//
//
faceidentify.cpp
//
abilitytpratice
//
//
Created by Mr_zhou on 24.5.21.
//
#include "faceidentify.hpp"
void faceidentify::saveimage()
{
// open camera
VideoCapture capture(0);
if(!capture.isOpened())
{
cout << "camera open failed" << endl;
return;
}
cout << "camera open success!" << endl;
// 加载检测人脸的xml文件
if(!faceCascade.load(savefacexml))
{
cout << "savefacexml load error!" << endl;
return;
}
cout << "savefacexml load success!" << endl;
Mat frame,img;
//显示实际大小
vector<Rect> faces;
int count = 0;
while(capture.read(frame))
{
// 防止左右颠倒
flip(frame, frame, 1);
// 检测人脸
faceCascade.detectMultiScale(frame,faces,1.1,3,0,Size(50,50),Size(500,500));
for(int i = 0;i<faces.size();i++)
{
Mat dst;
resize(frame(faces[i]), dst, Size(100,100));
if(count % 2 == 0)
{
imwrite(format("/Users/mr_zhou/Desktop/tupian/face_%d.JPG",count),frame(faces[i]));
}
rectangle(frame, faces[i], Scalar(0,0,255), 2,8);
}// end for循环
imshow("camera-demo", frame);
char c = waitKey(10);
if(c == 27)
{
break;
}
count++;
}// end while
// 释放空间
capture.release();
}
void faceidentify::identify()
{
ifstream file;
file.open("/Users/mr_zhou/Desktop/picture/myimage.txt",ifstream::in);
if(!file.is_open())
{
cout << "open file filed!" << endl;
}
// 读取文件并保存
string line;
string path;
string classlabel;
char separator = ';';
vector<cv::Mat> images;
vector<int> labels;
Mat tmpimage;
while(getline(file,line))
{
stringstream liness(line);
getline(liness, path, separator);
getline(liness,classlabel);
// 二者都不为空
if(!path.empty() && !classlabel.empty())
{
resize(cv::imread(path,0), tmpimage, Size(294,294));
images.push_back(tmpimage);
labels.push_back(atoi(classlabel.c_str()));
}
}
// end while
if(images.size() < 1 || labels.size() < 1)
{
cout << "invalid path" << endl;
return ;
}
int height = images[0].rows;
int width = images[0].cols;
cout << "height:" << height << "width:" << width << endl;
cv::Mat testSample = images[images.size()-1];
int testLable = labels[labels.size()-1];
images.pop_back();
labels.pop_back();
// 使用EigenFace获取特征脸
Ptr<FisherFaceRecognizer> model = FisherFaceRecognizer::create();
model->train(images, labels);
// 获取特征脸
int predictLabel = model->predict(testSample);
// 返回识别到的
cout << "actual label:" << testLable << "predict label:" << predictLabel << endl;
if(!faceCascade.load(savefacexml))
{
cout << "load savefacexml filed!" << endl;
}
// 打开摄像头
VideoCapture cap(0);
if(!cap.isOpened())
{
cout << "camera open failed!" << endl;
return;
}
cout << "camera open failed!" << endl;
Mat frame;
vector<Rect> faces;
Mat dst;
while(cap.read(frame))
{
// 颠倒图像
flip(frame,frame,1);
// 设置显示视频中的信息尺寸
faceCascade.detectMultiScale(frame, faces,1.1,3,0,Size(80,80),Size(500,500));
for(int i = 0; i < faces.size();i++)
{
Mat roi = frame(faces[i]);
// 设置灰度图像,防止颜色不一致
cvtColor(roi, dst, COLOR_BGR2GRAY);
// 设置大小,与窗口一致
resize(dst,testSample,testSample.size());
// 保存识别的结果
int label = model->predict(testSample);
// 使用红色的框框起来
rectangle(frame, faces[i], Scalar(0,0,255), 3,8,0);
string text;
if(label == 17)
{
text = "wenliang";
}
else if(label == 18)
{
text = "shengju";
}
else if(label == 19)
{
text = "qifei";
}
else
{
text = "no know!";
}
putText(frame, text, Point(faces[i].x,faces[i].y-10),FONT_HERSHEY_PLAIN,
2.0,Scalar(255,0,0),3);
}
imshow("face-recognition", frame);
if(waitKey(50) == 27)
{
break;
}
}
// end while
}

main.cpp

//
//
main.cpp
//
abilitytpratice
//
//
Created by Mr_zhou on 24.5.21.
//
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
#include "faceidentify.hpp"
// 主函数
int main(int argc, const char * argv[]) {
// insert code here...
faceidentify fi;
// 保存人脸图像
fi.saveimage();
// 人脸识别
fi.identify();
waitKey(0);
std::cout << "Hello, World!n";
return 0;
}

1.按照自己安装opencv的路径填写face_xml

2.保存图片的路径按照自己保存的文件名

最后

以上就是稳重花卷为你收集整理的opencv人脸识别 C++(附视频链接)的全部内容,希望文章能够帮你解决opencv人脸识别 C++(附视频链接)所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部