Qt实战:基于Qt的简易人脸识别功能

简介: 基于Qt的简易人脸识别功能

Qt实战:基于Qt的简易人脸识别功能


一、效果图

如图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部分

代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_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部分

代码如下:
#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();
}


总结

以上是部分Qt关于功能优化的部分,希望能帮助到大家,感谢大家支持~( ̄▽ ̄~)~

相关文章
|
28天前
|
算法 计算机视觉
基于qt的opencv实时图像处理框架FastCvLearn实战
本文介绍了一个基于Qt的OpenCV实时图像处理框架FastCvLearn,通过手撕代码的方式详细讲解了如何实现实时人脸马赛克等功能,并提供了结果展示和基础知识回顾。
基于qt的opencv实时图像处理框架FastCvLearn实战
|
5月前
|
开发框架 Java 编译器
【Qt 元对象系统 01 】深入探索Qt的元对象系统:核心地位、功能与构成
【Qt 元对象系统 01 】深入探索Qt的元对象系统:核心地位、功能与构成
185 1
|
5月前
|
存储 搜索推荐 人机交互
Qt鼠标事件全面解析:从基础到实战
Qt鼠标事件全面解析:从基础到实战
1066 0
|
5月前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
199 7
|
28天前
|
文字识别 计算机视觉 开发者
基于QT的OCR和opencv融合框架FastOCRLearn实战
本文介绍了在Qt环境下结合OpenCV库构建OCR识别系统的实战方法,通过FastOCRLearn项目,读者可以学习Tesseract OCR的编译配置和在Windows平台下的实践步骤,文章提供了技术资源链接,帮助开发者理解并实现OCR技术。
基于QT的OCR和opencv融合框架FastOCRLearn实战
|
3月前
|
存储 算法 C++
【Qt应用开发】复刻经典:基于Qt实现Windows风格计算器(加减乘除、删除、归零功能全解析)
在Qt中,"栈"的概念主要体现在两个层面:一是程序设计中的数据结构——栈(Stack),二是用户界面管理中的QStackedWidget控件。下面我将分别简要介绍这两个方面:
130 4
|
28天前
|
计算机视觉
基于QT的opencv插件框架qtCvFrameLearn实战
这篇文章详细介绍了如何基于Qt框架开发一个名为qtCvFrameLearn的OpenCV插件,包括项目配置、插件加载、Qt与OpenCV图像转换,以及通过各个插件学习OpenCV函数的使用,如仿射变换、卡通效果、腐蚀、旋转和锐化等。
33 10
|
4月前
|
机器学习/深度学习 人工智能 监控
人脸识别技术发展历史、技术全解和实战应用
人脸识别技术发展历史、技术全解和实战应用
197 1
Qt (QFileDialog&QColorDialog&QFontDialog) 对话框实战
Qt (QFileDialog&QColorDialog&QFontDialog) 对话框实战
Qt (QFileDialog&QColorDialog&QFontDialog) 对话框实战
|
3月前
|
机器学习/深度学习 人工智能 计算机视觉
好的资源-----打卡机+Arm+Qt+OpenCV嵌入式项目-基于人脸识别的考勤系统-----B站神经网络与深度学习,商城
好的资源-----打卡机+Arm+Qt+OpenCV嵌入式项目-基于人脸识别的考勤系统-----B站神经网络与深度学习,商城