• 利用 QT 完成一个人脸识别系统,完成登录操作


    1.配置文件

    1. # Project created by QtCreator 2023-09-22T10:34:23
    2. #
    3. #-------------------------------------------------
    4. QT += core gui
    5. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    6. TARGET = project
    7. TEMPLATE = app
    8. SOURCES += main.cpp\
    9. widget.cpp
    10. HEADERS += widget.h
    11. FORMS += widget.ui
    12. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include
    13. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv
    14. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv2
    15. LIBS += D:/opencv/opencv3.4-qt-intall/install/x86/mingw/lib/libopencv_*.a

    2.头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. using namespace cv;
    17. using namespace cv::face;
    18. using namespace std;
    19. namespace Ui {
    20. class Widget;
    21. }
    22. class Widget : public QWidget
    23. {
    24. Q_OBJECT
    25. public:
    26. explicit Widget(QWidget *parent = 0);
    27. ~Widget();
    28. private slots:
    29. void on_openCameraBtn_clicked();
    30. void on_closeCameraBtn_clicked();
    31. void on_studyBtn_clicked();
    32. private:
    33. Ui::Widget *ui;
    34. /***************功能模块1:摄像头的获取并展示****************/
    35. VideoCapture v; //视频流对象
    36. Mat src; //获取摄像头原图
    37. Mat rgb; //存放rgb图
    38. Mat gray; //灰度图
    39. Mat dst; //均衡化图
    40. CascadeClassifier c; //级联分类器
    41. vector faces; //人脸矩形框容器
    42. int camera_id; //摄像头的定时器
    43. void timerEvent(QTimerEvent *e); //重写定时器事件处理函数
    44. /****************功能模块2:人脸录入操作*******************/
    45. int study_id; //人脸录入的定时器
    46. Ptr recognizer; //人脸识别器的指针
    47. vector studyFaces; //人脸学习的数组
    48. vector<int> studyLabs; //人脸的标签数组
    49. int flag; //标记是否正在录入人脸
    50. int count; //记录学习次数
    51. /****************功能模块3:人脸检测***********************/
    52. int check_id; //人脸检测的定时器
    53. };
    54. #endif // WIDGET_H

    3.源文件

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. ui->loginBtn->setEnabled(false); //将登录按钮设置成不可用状态
    9. ui->closeCameraBtn->setEnabled(false); //关闭摄像头按钮禁用
    10. //给级联分类器装载人脸分类模型
    11. if(!c.load("D:\\opencv\\resources\\haarcascade_frontalface_alt2.xml"))
    12. {
    13. QMessageBox::information(this,"失败","人脸分类模型下载失败");
    14. return;
    15. }
    16. //创建一个人脸识别器对象
    17. QFile file("D:\\opencv\\resources\\myface.xml");
    18. if(file.exists())
    19. {
    20. //表明人脸识别模型存在,直接下载即可
    21. recognizer = LBPHFaceRecognizer::load("D:\\opencv\\resources\\myface.xml");
    22. }else
    23. {
    24. //人脸模型不存在,需要创建一个
    25. recognizer = LBPHFaceRecognizer::create();
    26. }
    27. //当系统启动时,就要启动人脸检测的定时器
    28. check_id = this->startTimer(2000); //每隔2秒检测一次
    29. flag = 0; //表明刚开始时处于检测过程
    30. recognizer->setThreshold(70); //设置可信度,当检测的可信度低于100时,表明识别成功
    31. }
    32. Widget::~Widget()
    33. {
    34. delete ui;
    35. }
    36. //打开摄像头按钮对应的槽函数
    37. void Widget::on_openCameraBtn_clicked()
    38. {
    39. //打开摄像头
    40. if(!v.open(0))
    41. {
    42. QMessageBox::information(this,"失败","摄像头打开失败");
    43. return;
    44. }
    45. //启动定时器,每隔20毫秒,将摄像头中内容展示到ui界面的lab中
    46. camera_id = this->startTimer(20);
    47. //将该按钮设置成不可用状态
    48. ui->openCameraBtn->setEnabled(false);
    49. ui->closeCameraBtn->setEnabled(true);
    50. }
    51. //关闭摄像头按钮对应的槽函数
    52. void Widget::on_closeCameraBtn_clicked()
    53. {
    54. //关闭定时器
    55. this->killTimer(camera_id);
    56. //将启动按钮设置成可用状态
    57. ui->openCameraBtn->setEnabled(true);
    58. ui->closeCameraBtn->setEnabled(false);
    59. ui->faceLab->clear();
    60. //关闭摄像头
    61. v.release();
    62. }
    63. //重写的定时器事件处理函数
    64. void Widget::timerEvent(QTimerEvent *e)
    65. {
    66. //判断是哪个定时器到位
    67. if(e->timerId() == camera_id)
    68. {
    69. //1、从摄像头中读取一张图像
    70. v.read(src);
    71. //2、翻转
    72. flip(src,src, 1);
    73. //3、重新设置大小
    74. cv::resize(src,src,Size(300,300));
    75. //4、转换为rgb图
    76. cvtColor(src,rgb,CV_BGR2RGB);
    77. //5、灰度处理
    78. cvtColor(rgb, gray, CV_BGR2GRAY);
    79. //6、均衡化处理
    80. equalizeHist(gray,dst);
    81. //7、使用级联分类器找到人脸矩形框
    82. c.detectMultiScale(dst, faces);
    83. //8、将人脸矩形框绘制到rgb图上
    84. for(quint32 i=0; isize(); i++)
    85. {
    86. rectangle(rgb, faces[i], Scalar(255,0,0), 2);
    87. }
    88. //9、通过使用Mat类型的rgb图,构造一个QT能够识别的图像
    89. QImage img(rgb.data, rgb.cols, rgb.rows, rgb.cols*rgb.channels(), QImage::Format_RGB888);
    90. //10、将qimage图转换为qpixmap图展示到UI界面
    91. ui->faceLab->setPixmap(QPixmap::fromImage(img));
    92. }
    93. //判断人脸录入的定时器是否到位
    94. if(e->timerId() == study_id)
    95. {
    96. qDebug()<<"正在录入,请稍后...";
    97. //定义容器,存放摄像头中矩形框框起来的人脸区域
    98. Mat face = src(faces[0]);
    99. //将人脸重新设置尺寸
    100. cv::resize(face,face,Size(100,100));
    101. //灰度处理
    102. cvtColor(face,face,CV_BGR2GRAY);
    103. //均衡化处理
    104. equalizeHist(face,face);
    105. //将处理好的人脸图像放入学习容器中
    106. studyFaces.push_back(face);
    107. studyLabs.push_back(1);
    108. count++;
    109. if(count==60) //判断是否已经完成学习
    110. {
    111. //更新人脸识别模型
    112. //函数原型:virtual void update(InputArrayOfArrays src, InputArray labels);
    113. //功能:将给定的图像模型转换为数据模型
    114. //参数1:图像数组
    115. //参数2:标签数组
    116. recognizer->update(studyFaces,studyLabs);
    117. //将人脸数据模型保存到本地磁盘文件中
    118. recognizer->save("D:\\opencv\\resources\\myface.xml");
    119. //后续操作
    120. this->killTimer(study_id); //关闭定时器
    121. ui->studyBtn->setEnabled(true); //按钮设置成可用状态
    122. studyFaces.clear(); //清空容器
    123. studyLabs.clear();
    124. flag = 0; //设置flag为0,表明可以继续监测人脸
    125. count = 0;
    126. QMessageBox::information(this,"成功", "录入成功");
    127. }
    128. }
    129. //判断人脸检测定时器是否到位
    130. if(e->timerId() == check_id)
    131. {
    132. //判断是否能进行检测
    133. if(flag == 0)
    134. {
    135. if(faces.empty() || recognizer.empty())return;
    136. qDebug()<<"正在寻找人脸";
    137. QFile file("D:\\opencv\\resources\\myface.xml"); //人脸模型存在
    138. if(file.exists())
    139. {
    140. //1、获取摄像头中人脸区域
    141. Mat face = src(faces[0]);
    142. //2、重新设置大小
    143. cv::resize(face,face,Size(100,100));
    144. //3、灰度处理
    145. cvtColor(face,face, CV_BGR2GRAY);
    146. //4、均衡化处理
    147. equalizeHist(face,face);
    148. //5、准备变量接受预测后的结果
    149. int lab = -1;
    150. double conf = 0.0;
    151. //6、人脸预测
    152. recognizer->predict(face, lab, conf);
    153. qDebug()<<"lab: "<" conf: "<
    154. //7、判断预测的结果
    155. if(lab != -1)
    156. {
    157. //预测成功,给定按钮的权限
    158. ui->loginBtn->setEnabled(true);
    159. }
    160. }
    161. }
    162. }
    163. }
    164. //人脸录入按钮对应的槽函数
    165. void Widget::on_studyBtn_clicked()
    166. {
    167. qDebug()<<"开始录入....";
    168. study_id = this->startTimer(50); //每隔50毫秒学习一次
    169. count =0; //计数器清零
    170. ui->studyBtn->setEnabled(false); //按钮不能使用
    171. flag = 1; //表明正在录入
    172. }

    4.主函数

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

  • 相关阅读:
    【前端版】分布式医疗云平台【登陆页面修改、页面 title 修改、登陆接口准备说明、把前端和后端统一使用 git 管理、启动前端 VUE 项目、用户登陆】(十七)
    React知识点系列(7)-每天10个小知识
    【从0到1设计一个网关】性能优化---缓存
    软件测试中功能测试流程
    节点加密技术:保障数据传输安全的新利器
    他因“上帝粒子”获诺奖,却火速搬到乡下:它毁了我的生活
    数据挖掘-支持向量机(SVM)+代码实现
    在 Vue.js 中,使用 watch 监听data变量如:对象属性/data变量
    Python工程师Java之路(v)Socket极简代码
    《JAVA程序设计》教学上机实验报告
  • 原文地址:https://blog.csdn.net/2301_78047404/article/details/133183798