软件开发网站建设科技有限公司,工信部网站备案号查询,厦门网页建站申请比较好,crm做的比较好的公司一百二十一、基于QT的OpenCV库实现人脸识别功能
121.1 UI 界面 登录按钮现在没啥实际作用#xff0c;因为没加功能#xff0c;可以添加在识别成功后运行的功能代码
121.2 思路
显示人脸#xff1a; 通过 VideoCapture 这个类下面的 open() 方法打开摄像头#xff0c;对…一百二十一、基于QT的OpenCV库实现人脸识别功能
121.1 UI 界面 登录按钮现在没啥实际作用因为没加功能可以添加在识别成功后运行的功能代码
121.2 思路
显示人脸 通过 VideoCapture 这个类下面的 open() 方法打开摄像头对摄像头读取到的图像帧进行处理。调整人脸图像尺寸将人脸图像放到矩形框容器中再将这个图像显示到UI界面上录入人脸 加载或者创建级联分类器文件有就是加载没有就是创建。对当前图形依次进行灰度处理、均衡化处理然后放到容器里面每张人脸对应的标签也放进去虽然都是1可以自己设定往容器里面放多少张图像帧。当放完之后就可以转化为人脸模型了存完人脸模型就可以选择进行验证即人脸检测识别。顺手将人脸容器和标签容器清空。人脸识别 打开人脸模型文件打开成功后将当前视频帧依次进行灰度处理和均衡化处理然后根据设置的人脸可信度进行识别识别成功后就提示识别成功。
121.3 代码
main.cpp
#include faceidentification.h
#include QApplicationint main(int argc, char *argv[])
{QApplication a(argc, argv);faceIdentification w;w.show();return a.exec();
}faceidentification.h
#ifndef FACEIDENTIFICATION_H
#define FACEIDENTIFICATION_H#include QWidget#include opencv2/opencv.hpp
#include iostream
#include math.h
#include opencv2/face.hpp
#include vector
#include map
#include QMessageBox
#include QDebug
#include QFile
#include QTextStream
#include QDateTime
#include QTimerEvent
#include QtSerialPort/QtSerialPort
#include QtSerialPort/QSerialPortInfo
using namespace cv;
using namespace cv::face;
using namespace std;namespace Ui {
class faceIdentification;
}class faceIdentification : public QWidget
{Q_OBJECTpublic:explicit faceIdentification(QWidget *parent 0);~faceIdentification();signals:// 加载配置文件与保存人脸模型文件 的信号void load_CascadeClassifier_and_create_face_file_SIGNAL();public slots:// 加载配置文件与人脸模型文件 的槽函数声明void load_CascadeClassifier_and_create_face_file_SLOT();private slots:// 打开摄像头对应的槽函数声明void on_openBtn_clicked();// 关闭摄像头对应的槽函数声明void on_closeBtn_clicked();// 开始录入对应的槽函数声明void on_inputBtn_clicked();private:Ui::faceIdentification *ui;// 摄像头相关成员// 视频对象VideoCapture v;// 图像容器存放图片帧Mat src; // 摄像头直接摄入的图像Mat gray; // 转换成的灰度图Mat dst; // 灰度图转换成的灰度直方图Mat rgb; // 摄像头摄入的BGR转换的RGB图像// 级联分类器对象CascadeClassifier c;// 人脸矩形框容器vectorRect faces;// 重写的定时器超时事件处理函数void timerEvent(QTimerEvent *event) override;// 打开摄像头的定时器int camer_timer_id;/*** 人脸录入的相关成员 ***/// 人脸录入的定时器int study_timer_id;// 人脸识别器指针PtrLBPHFaceRecognizer recognizer;// 存储人脸图像帧的容器vectorMat study_faces;// 存放人脸图像帧对应的标签vectorint study_labs;// 保存人脸图像帧的次数int count 0;// 用于控制是人脸录入还是人脸检测bool input_flag false;bool check_flag false;// 人脸检测的定时器int check_timer_id;
};#endif // FACEIDENTIFICATION_Hfaceidentification.cpp
#include faceidentification.h
#include ui_faceidentification.hfaceIdentification::faceIdentification(QWidget *parent) :QWidget(parent),ui(new Ui::faceIdentification)
{ui-setupUi(this);// 将自定义的 加载配置文件与保存人脸模型文件 的信号与槽函数进行连接connect(this, faceIdentification::load_CascadeClassifier_and_create_face_file_SIGNAL,this,faceIdentification::load_CascadeClassifier_and_create_face_file_SLOT);// 初始化UI界面ui-closeBtn-setEnabled(false);ui-inputBtn-setEnabled(false);ui-loginBtn-setEnabled(false);
}faceIdentification::~faceIdentification()
{delete ui;
}// 加载配置文件与人脸模型文件 的槽函数实现
void faceIdentification::load_CascadeClassifier_and_create_face_file_SLOT()
{// 加载级联分类器配置文件if(!c.load(D:\\opencv\\heads\\haarcascade_frontalface_alt.xml)){QMessageBox::warning(this,警告,级联分类器加载失败);return ;}// 人脸模型文件QFile faceFile(D:\\opencv\\heads\\my_face.xml);// 如果人脸模型存在,则加载;若不存在,则创建if(faceFile.exists()){recognizer LBPHFaceRecognizer::loadLBPHFaceRecognizer(D:\\opencv\\heads\\my_face.xml);}else{recognizer LBPHFaceRecognizer::create();}
}// 打开摄像头对应的槽函数实现
void faceIdentification::on_openBtn_clicked()
{// 打开摄像头if(!v.open(0)){QMessageBox::warning(this,警告,摄像头打开失败);return ;}// 发射 加载配置文件与保存人脸模型文件 的信号emit load_CascadeClassifier_and_create_face_file_SIGNAL();// 启动计时器,用于在UI界面上显示人脸camer_timer_id startTimer(20);// 更改UI按钮可用性ui-closeBtn-setEnabled(true);ui-inputBtn-setEnabled(true);ui-openBtn-setEnabled(false);// 启动人脸检测定时器check_timer_id startTimer(1000);// 表示只进行人脸检测check_flag true;// 设置人脸检测可信度,低于100表示检测成功recognizer-setThreshold(100);
}// 关闭摄像头对应的槽函数实现
void faceIdentification::on_closeBtn_clicked()
{// 关闭摄像头v.release();// 关闭定时器killTimer(camer_timer_id);// 更改UI按钮可用性ui-closeBtn-setEnabled(false);ui-inputBtn-setEnabled(false);ui-openBtn-setEnabled(true);ui-label-clear();}// 人脸录入按钮对应的槽函数实现
void faceIdentification::on_inputBtn_clicked()
{// 进行人脸录入,而不是识别input_flag true;check_flag false;// 初始化人脸录入次数count 0;// 启动人脸录入定时器study_timer_id startTimer(50);// 将人脸录入按钮设置为不可用状态ui-inputBtn-setEnabled(false);
}// 定时器超时事件处理函数
void faceIdentification::timerEvent(QTimerEvent *event)
{// 打开摄像头的定时器超时if(camer_timer_id event-timerId()){// 从摄像头中读取图像放到src中v.read(src);// 将读取进来的镜像图像进行翻转flip(src, src, 1);// 将opencv的 BGR 图像转换为 RGB 图像cvtColor(src, rgb, CV_BGR2RGB);// 调整 RGB 图像的尺寸,使其能够正好放进UI界面的图像框中cv::resize(rgb, rgb, Size(ui-label-width(), ui-label-height()));// 将 RGB 图像转换为灰度图,再转换为灰度直方图(均衡化处理)cvtColor(rgb, gray, CV_BGR2GRAY);equalizeHist(gray, dst);// 将图像上的人脸放到矩形框容器中c.detectMultiScale(dst, faces);// 将矩形框绘制到人脸上for(int i0; ifaces.size(); i){rectangle(rgb, faces[i], Scalar(255, 0, 0), 2);}// 定义一个QImage对象,用于将图像放到UI界面上QImage img(rgb.data, rgb.cols, rgb.rows, rgb.cols*rgb.channels(), QImage::Format_RGB888);ui-label-setPixmap(QPixmap::fromImage(img));}// 人脸录入定时器超时if(study_timer_id event-timerId()){qDebug() 正在录入,请正对摄像头;if(input_flag){// 存储当前图像Mat face;// 将从视频中读取来的图像存起来face src(faces[0]);// 灰度处理cvtColor(face, face, CV_BGR2GRAY);// 灰度直方图(均衡化处理)equalizeHist(face, face);// 保存起来,放到容器中study_faces.push_back(face);study_labs.push_back(1);count;// 存了五十张if(50 count){// 将五十张人脸转换为数据模型存起来recognizer-update(study_faces, study_labs);recognizer-save(D:\\opencv\\heads\\my_face.xml);QMessageBox::information(this, 提示, 人脸录入成功);// 可以进行人脸检测,而不是录入了check_flag true;input_flag false;// 清空容器及相关变量,以待下次使用study_faces.clear();study_labs.clear();count 0;// 关闭人脸录入定时器killTimer(study_timer_id);}}}// 人脸检测识别定时器超时if(check_timer_id event-timerId()){qDebug() 正在检测;if(check_flag){QFile faceFile(D:\\opencv\\heads\\my_face.xml);if(faceFile.exists()){// 将视频中的一帧人脸图像放到face中Mat face;face src(faces[0]);// 灰度处理cvtColor(face, face, CV_BGR2GRAY);// 均衡化处理equalizeHist(face, face);// 假设匹配后的结果int lab -1;double conf 0.0;// 如果匹配成功,则 lab 不再是 -1;若匹配失败,则 lab 还是 -1recognizer-predict(face, lab, conf);if(lab ! -1){// 匹配成功,给出提示,并关闭人脸检测定时器QMessageBox::information(this, 提示, 欢迎回来);ui-loginBtn-setEnabled(true);killTimer(check_timer_id);}}}}
}