欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dlib人脸检测

程序员文章站 2024-03-19 16:46:58
...

一、dlib

Dlib是一个机器学习的C++库,包含了许多机器学习常用的算法。下载:http://www.dlib.net/ml.html       dlib人脸检测


二、安转qt,设置环境变量,用qt自带的mingw工具编译dlib库

工具路径:

dlib人脸检测

设置环境变量:

dlib人脸检测

三、dlib

下载:http://dlib.net/files/

dlib人脸检测

               解压,并新建安装目录dlib

dlib人脸检测


四、cmake

下载:https://cmake.org/download/

           dlib人脸检测

           

             使用cmake生成Makefile:

             dlib人脸检测

Configured点击两次后,点击Generate,在安转目录下生成Makefile:

dlib人脸检测

五、编译

          进入安转目录下,编译:

dlib人脸检测

完成编译:

dlib人脸检测

六、安装dlib

dlib人脸检测

安装完成:

dlib人脸检测

七、测试

参考G:\dlib-19.7\examples\face_detection_ex.cpp例子,新建qt工程:

dlib人脸检测

编译运行:

dlib人脸检测

八、源码

.pro

QT       +=coregui
QMAKE_CXXFLAGS+=-std=c++0x 
greaterThan(QT_MAJOR_VERSION,4):QT+=widgets 
TARGET=dlibTest
TEMPLATE=app 
SOURCES+=main.cpp\
    mainwindow.cpp 
HEADERS  +=mainwindow.h 
FORMS  +=mainwindow.ui 
LIBS+=$$PWD/dlib/lib/libdlib.a 
INCLUDEPATH+=$$PWD/dlib

mainwindow.cpp:

#include"mainwindow.h"
#include"ui_mainwindow.h"
#include<dlib/image_processing/frontal_face_detector.h>
#include<dlib/gui_widgets.h>
#include<dlib/image_io.h>
#include<dlib/image_processing.h>
#include<iostream> 
#include<QDebug>
#include<QFileDialog>
#include<QString>
#include<QTextCodec>
#include<QImage>
#include<QPainter>
usingnamespacedlib;
MainWindow::MainWindow(QWidget*parent):
    QMainWindow(parent),
    ui(newUi::MainWindow)
{
    ui->setupUi(this);
} 
MainWindow::~MainWindow()
{
    deleteui;
}
voidMainWindow::on_pushButton_clicked()
{
    QStringimgName=QFileDialog::getOpenFileName(this,tr("File"),tr("./"),tr("ImageFile(*.png*.jpg)"));
    if(imgName.isNull())return;
    QTextCodec*code=QTextCodec::codecForName("gb2312");
    std::stringname=code->fromUnicode(imgName).data();
        //--------------------------------------------------
   QImage*labImg=newQImage;
   if(!(labImg->load(name.data())))
   {
      deletelabImg;
      return;
   }
   QPixmappixmap=QPixmap::fromImage(*labImg);
   QPainterpainter(&pixmap);
   painter.setPen(Qt::red);
   //--------------------------------------------------
    frontal_face_detectordetector=get_frontal_face_detector();
    dlib::array2d<unsignedchar>img;
    load_image(img,name.data()); 
    dlib::pyramid_up(img);
    std::vector<dlib::rectangle>dets=detector(img); 
    dlib::rectanglemyRect;
    intfaceCount=dets.size();
    ui->textBrowser->append(QString::number(faceCount));
    for(int32_ti=0;i<faceCount;i++)
    {
      myRect=dets.at(i);
      ui->textBrowser->append("x:"+QString::number(myRect.left()/2));
      ui->textBrowser->append("y:"+QString::number(myRect.top()/2));
      ui->textBrowser->append("width:"+QString::number(myRect.width()/2));
      ui->textBrowser->append("height:"+QString::number(myRect.height()/2)); 
      painter.drawRect(myRect.left()/2,myRect.top()/2,myRect.width()/2,myRect.height()/2);
    }
    ui->label->setPixmap(pixmap);
}