一、下载并安装相关软件包
1、cmake
链接:https://pan.baidu.com/s/1g57OuGMad_idSQAzkkpm4w提取码:0232
2、QT
链接:https://pan.baidu.com/s/1j8X7z3iHG-0IQVnjlAAtIQ提取码:hl8w
3、dlib
链接:https://pan.baidu.com/s/1DA1vHmt1Tx6cFp0Y2gCUPQ提取码:jmen
二、生成静态库和头文件
新建一个F:/dlib/build文件夹,打开cmake进行编译:
1、点击configure,设置生成器及编译器
选择QT中的gcc和g++编译器:
2、设置编译选项
取消GIF格式支持,因为这个需要另外安装库,不然会报错,INSTALL_PREFIX对应就是最终编译完成,得到库的保存路径。另外两个矩阵运算库,也不要勾选。
再次点击configure和generate。
3、编译
cd F:\dlib\build
mingw32-make
mingw32-make install
三、测试
在pro文件中添加:
LIBS+=F:/dlib/build/lib/libdlib.a
INCLUDEPATH+=F:/dlib/build/include
#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>
using namespace dlib;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString imgName = QFileDialog::getOpenFileName( this, tr("Open Image"), ".",tr("Image Files(*.png *.jpg *.jpeg *.bmp)"));
if(imgName.isNull())return;
QTextCodec*code=QTextCodec::codecForName("gb2312");
std::string name=code->fromUnicode(imgName).data();
//--------------------------------------------------
QImage*labImg=new QImage;
if(!(labImg->load(name.data())))
{
delete labImg;
return;
}
QPixmap pixmap=QPixmap::fromImage(*labImg);
QPainter painter(&pixmap);
painter.setPen(Qt::red);
//--------------------------------------------------
frontal_face_detector detector=get_frontal_face_detector();
dlib::array2d<unsigned char>img;
load_image(img,name.data());
dlib::pyramid_up(img);
std::vector<dlib::rectangle>dets=detector(img);
dlib::rectangle myRect;
int faceCount=dets.size();
ui->textBrowser->append(QString::number(faceCount));
for(int32_t i=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);
}