Qt功能优化:Qt人脸识别

Posted 灵彧universe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt功能优化:Qt人脸识别相关的知识,希望对你有一定的参考价值。

Qt功能优化:Qt人脸识别

@TOC


一、效果图

如图1所示,为我用Qt所做的一个简易的人脸识别代码,点击识别即可自动识别出照片中的人脸。

二、使用步骤

项目架构:

1. .pro部分

#-------------------------------------------------
#
# Project created by QtCreator 2021-08-01T20:37:50
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = OpencvFace
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \\
        main.cpp \\
        mainwindow.cpp

HEADERS += \\
        mainwindow.h

FORMS += \\
        mainwindow.ui
INCLUDEPATH += D:\\TYUT\\C-yunding\\ProgramApp\\C++\\Qt\\OpenCV_3.4.3-Build\\install\\include
LIBS += D:\\TYUT\\C-yunding\\ProgramApp\\C++\\Qt\\OpenCV_3.4.3-Build\\install\\x86\\mingw\\bin\\libopencv_*.dll

2. .h部分

#include <QMainWindow>
#include "opencv2/opencv.hpp"
#include <vector>
using namespace cv;
using namespace std;

namespace Ui
class MainWindow;

class MainWindow : public QMainWindow

Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void initMainWindow();
void imgProc();
void imgShow();

private slots:
void on_detectPushButton_clicked();

private:
Ui::MainWindow *ui;
Mat myImg;
QImage myQImg;
;

#endif // MAINWINDOW_H

## 3. .cpp部分
> 代码如下:
```c
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    ui->setupUi(this);
    initMainWindow();


MainWindow::~MainWindow()

    delete ui;


void MainWindow::initMainWindow()

    QString imgPath = "ly.jpg";//你要识别的图片,所在位置需与pro文件在同级目录
    Mat imgData = imread(imgPath.toLatin1().data());
    cvtColor(imgData,imgData,COLOR_BGR2RGB);
    myImg = imgData;
    myQImg = QImage((const unsigned char*)(imgData.data),imgData.cols,imgData.rows,QImage::Format_RGB888);
    imgShow();


void MainWindow::imgShow()

    ui->viewLabel->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->viewLabel->size(),Qt::KeepAspectRatio)));


void MainWindow::imgProc()

    CascadeClassifier face_detector;
    CascadeClassifier eyes_detector;
    string fDetectorPath = "haarcascade_frontalface_alt.xml";
    face_detector.load(fDetectorPath);
    string eDetectorPath = "haarcascade_eye_tree_eyeglasses.xml";
    eyes_detector.load(eDetectorPath);
    vector<Rect> faces;
    Mat imgSrc = myImg;
    Mat imgGray;
    cvtColor(imgSrc,imgGray,CV_RGB2GRAY);
    equalizeHist(imgGray,imgGray);
    face_detector.detectMultiScale(imgGray,faces,1.1,2,0 | CV_HAAR_SCALE_IMAGE,Size(30,30));
    for(int i=0; i<faces.size(); i++)
    
        Point center(faces[i].x + faces[i].width * 0.5,faces[i].y + faces[i].height * 0.5);
        ellipse(imgSrc,center,Size(faces[i].width * 0.5,faces[i].height * 0.5),0,0,360,Scalar(255,0,255),4,8,0);
        Mat faceROI = imgGray(faces[i]);
        vector<Rect> eyes;
        eyes_detector.detectMultiScale(faceROI,eyes,1.1,2,0 | CV_HAAR_SCALE_IMAGE,Size(30,30));
        for(int j=0; j<eyes.size(); j++)
        
            Point center(faces[i].x + eyes[j].x + eyes[j].width * 0.5,faces[i].y + eyes[j].y + eyes[j].height * 0.5);
            int radius = cvRound((eyes[j].width + eyes[i].height) * 0.25);
            circle(imgSrc,center,radius,Scalar(255,0,0),4,8,0);
        
        Mat imgDst = imgSrc;
        myQImg = QImage((const unsigned char*)(imgDst.data),imgDst.cols,imgDst.rows,QImage::Format_RGB888);
        imgShow();
    

void MainWindow::on_detectPushButton_clicked()

    imgProc();


4. .ui部分:


---
# 总结
以上是部分Qt关于功能优化的部分,希望通过这些文章能够使大家的Qt软件更加美观和完美 !!!

希望能帮助到大家,感谢大家支持~( ̄▽ ̄~)~

以上是关于Qt功能优化:Qt人脸识别的主要内容,如果未能解决你的问题,请参考以下文章

Qt编写百度离线版人脸识别+比对+活体检测

基于QT和OpenCV的人脸识别系统

基于QT和OpenCV的人脸检测识别系统

基于QT和OpenCV的人脸检测识别系统

Qt音视频开发42-人脸识别客户端

基于QT设计的人脸考勤打卡签到系统