• QT实现人脸识别


    QT实现人脸识别

    Face.pro文件:

    1. QT += core gui
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include
    21. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv
    22. INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv2
    23. LIBS += D:/opencv/opencv3.4-qt-intall/install/x86/mingw/lib/libopencv_*.a
    24. # Default rules for deployment.
    25. qnx: target.path = /tmp/$${TARGET}/bin
    26. else: unix:!android: target.path = /opt/$${TARGET}/bin
    27. !isEmpty(target.path): INSTALLS += target

     widget.h文件:

    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. #include
    17. #include
    18. #include
    19. using namespace cv;
    20. using namespace cv::face;
    21. using namespace std;
    22. QT_BEGIN_NAMESPACE
    23. namespace Ui { class Widget; }
    24. QT_END_NAMESPACE
    25. class Widget : public QWidget
    26. {
    27. Q_OBJECT
    28. public:
    29. Widget(QWidget *parent = nullptr);
    30. ~Widget();
    31. private slots:
    32. void on_openBtn_clicked();
    33. void on_closeBtn_clicked();
    34. void on_faceBtn_clicked();
    35. private:
    36. Ui::Widget *ui;
    37. //=========摄像头相关成员的设置=========
    38. VideoCapture v; //摄像头容器
    39. Mat src; //存放 原图 的容器
    40. Mat gray; //存放 灰度图 的容器
    41. Mat dst; //存放 直方图 的容器
    42. Mat rgb; //存放 rgb图 的容器
    43. CascadeClassifier c; //定义一个级联分类器容器
    44. vector faces; // 定义一个数组存放人脸矩形框
    45. int camera_id; //打开摄像头的定时器id
    46. void timerEvent(QTimerEvent *e); //定时器事件 重写函数事件声明
    47. //=========人脸录入相关成员的设置=========
    48. Ptr recognizer; //人脸 识别器指针
    49. vector study_faces; //定义一个存放录入人脸的数组
    50. vector<int> study_lables; //定义一个存放人脸对应的标签数组
    51. int count; //记录录入人脸的次数
    52. int flag; //用来区别是人脸录入还是人脸识别
    53. int face_id; // 人脸录入定时器id
    54. //=========人脸检测相关成员的设置=========
    55. int check_id; //
    56. };
    57. #endif // WIDGET_H

    widget.cpp文件:

    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->weChatBtn->setEnabled(false);
    9. //打开系统摄像头
    10. if(!v.open(0))
    11. {
    12. QMessageBox::information(this, "", "打开系统摄像头失败!");
    13. return;
    14. }
    15. //配置 级联分类器
    16. if(!c.load("D:\\opencv\\image\\haarcascade_frontalface_alt.xml"))
    17. {
    18. QMessageBox::information(this, "", "配置级联分类器失败!");
    19. return;
    20. }
    21. //判断是否录入过人脸
    22. QFile file("D:\\opencv\\image\\my_face.xml");
    23. //判断文件是否存在
    24. if(file.exists())
    25. {
    26. //表示之前录入过人脸 则下载文件
    27. recognizer = LBPHFaceRecognizer::load("D:\\opencv\\image\\my_face.xml");
    28. }else {
    29. //表示之前没有录入过人脸
    30. recognizer = LBPHFaceRecognizer::create();
    31. }
    32. //启动一个人脸检测定时器
    33. check_id = startTimer(3000);
    34. flag = 1; //可以人脸检测
    35. recognizer->setThreshold(100);
    36. }
    37. Widget::~Widget()
    38. {
    39. delete ui;
    40. }
    41. //打开摄像头按钮对应的 槽函数
    42. void Widget::on_openBtn_clicked()
    43. {
    44. //启动一个定时器
    45. camera_id = startTimer(30);
    46. }
    47. //定时器重写的功能函数
    48. void Widget::timerEvent(QTimerEvent *e)
    49. {
    50. //判断是否是 摄像头定时器 超时
    51. if(e->timerId() == camera_id)
    52. {
    53. //读取系统摄像头中的图像
    54. v.read(src);
    55. //图像反转
    56. flip(src, src, 1);
    57. //将 bgr 转换成 rgb
    58. cvtColor(src, rgb, CV_BGR2RGB);
    59. //将图像重新设置大小 适应lab
    60. cv::resize(rgb, rgb, Size(301,251));
    61. //灰度处理
    62. cvtColor(rgb, gray, CV_RGB2GRAY);
    63. //均衡化处理
    64. equalizeHist(gray, dst);
    65. //锁定人脸矩形框位置
    66. c.detectMultiScale(dst, faces);
    67. //将矩形框绘制到人脸上
    68. for(uint i=0; isize(); i++)
    69. {
    70. rectangle(rgb, faces[i], Scalar(255,0,0),2);
    71. }
    72. //将图像放入 lab 中
    73. QImage img(rgb.data, rgb.cols, rgb.rows, rgb.cols*rgb.channels(), QImage::Format_RGB888);
    74. ui->label->setPixmap(QPixmap::fromImage(img));
    75. }
    76. //判断是否是 人脸录入定时器 超时
    77. if(e->timerId() == face_id)
    78. {
    79. if(0 == flag)
    80. {
    81. qDebug() << "人脸录入中,请正视摄像头!";
    82. Mat face = src(faces[0]); //将摄像头当前的一帧图像上的一个人脸给face
    83. //灰度处理
    84. cvtColor(face, face, CV_BGR2GRAY);
    85. //均衡化处理
    86. equalizeHist(face, face);
    87. //将人脸放入数组中
    88. study_faces.push_back(face);
    89. study_lables.push_back(1);
    90. count++;
    91. if(50 == count)
    92. {
    93. //将图像模型转换成数据模型
    94. recognizer->update(study_faces, study_lables);
    95. recognizer->save("D:\\opencv\\image\\my_face.xml");
    96. QMessageBox::information(this, "", "录入人脸成功!");
    97. //关闭定时器
    98. killTimer(face_id);
    99. flag = 1; //表示可以人脸识别
    100. study_faces.clear(); //将存放人脸数组清空,方便下次录入
    101. study_lables.clear();
    102. }
    103. }
    104. }
    105. //判断是否是 人脸检测定时器 超时
    106. if(e->timerId() == check_id)
    107. {
    108. if(1 == flag)
    109. {
    110. QFile file("D:\\opencv\\image\\my_face.xml");
    111. if(file.exists())
    112. {
    113. if(recognizer.empty() || faces.empty())
    114. {
    115. return;
    116. }
    117. Mat face = src(faces[0]);
    118. //灰度处理
    119. cvtColor(face, face, CV_BGR2GRAY);
    120. //
    121. equalizeHist(face, face);
    122. int lab = -1;
    123. double cof = 0.0;
    124. recognizer->predict(face, lab, cof);
    125. //根据 lab 判断是否识别成功
    126. if(lab != -1)
    127. {
    128. QMessageBox::information(this, "", "人脸识别成功!");
    129. ui->weChatBtn->setEnabled(true);
    130. killTimer(check_id);
    131. }
    132. }
    133. }
    134. }
    135. }
    136. //关闭摄像头按钮对应的槽函数
    137. void Widget::on_closeBtn_clicked()
    138. {
    139. killTimer(camera_id);
    140. }
    141. //录入人脸按钮对应的槽函数
    142. void Widget::on_faceBtn_clicked()
    143. {
    144. count = 0; // 将录入人脸的次数设置为0
    145. flag = 0; //表示只能做人脸录入,不能做人脸检测
    146. face_id = startTimer(50);
    147. }

    main.c文件:

    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. }

     ui布局:

     

  • 相关阅读:
    ZGC的流程图
    【机器学习】集成学习:使用scikitLearn中的BaggingClassifier实现bagging和pasting策略
    【解包裹】基于Matlab实现多聚类相位展开算法
    Docker+consul实现容器服务的发现和更新
    算法ppt练习题(给黄成个大逼兜)
    HDLbits: Edgedetect
    lammps提取和保存data文件中力场参数的技巧
    <c++> &引用概念 | 引用用法 | 引用与指针区别
    一、爬虫基本概念
    解决端口占用
  • 原文地址:https://blog.csdn.net/qq_46068832/article/details/139899761