Qt调用face++的http接口,实现人脸对比
生活随笔
收集整理的這篇文章主要介紹了
Qt调用face++的http接口,实现人脸对比
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
概念
演示
源碼
?
概念
Qt中有QNetworkAccessManager、QHttpMultiPart、QJsonArray、QHttpPart,可以實現發送http數據包,其中QNetworkAccessManager中有個函數,可以實現Get或者Post方法。
?
演示
程序運行截圖如下:
加入圖片后,點擊提交,可活得face++返回的json數據,其中就有一個confidence值,這個值就是可信度!
比如下面這兩種劉德華的照片如下圖所示:
如果是兩張不同的圖片
?
源碼
程序結構如下:
widget.ui如下:
widget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QHttpPart>QT_BEGIN_NAMESPACE class QNetworkAccessManager; class QNetworkReply; QT_END_NAMESPACEnamespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();protected:void loadLeftLabel();void loadRightLabel();void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;QHttpPart setHttpPart(QByteArray body, QVariant value);QHttpPart imageHttpPart(const QString &fileName, QVariant value);protected slots:void finished(QNetworkReply *reply);void btnClicked();private:Ui::Widget *ui;QString m_leftFileName;QString m_rightFileName;QNetworkAccessManager *m_manager; };#endif // WIDGET_Hmain.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> #include <QMouseEvent> #include <QNetworkReply> #include <QNetworkAccessManager> #include <QFileDialog> #include <QByteArray> #include <QHttpMultiPart> #include <QJsonArray> #include <QMap> #include <QJsonDocument> #include <QJsonObject> #include <QMessageBox>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);this->setWindowTitle("CSDN IT1995");m_manager = new QNetworkAccessManager(this);connect(ui->okPushButton, SIGNAL(clicked(bool)), this, SLOT(btnClicked()));connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));\ }Widget::~Widget() {delete ui; }void Widget::loadLeftLabel() {QString fileName = QFileDialog::getOpenFileName(this, "選擇圖片", qApp->applicationDirPath(), "Image Files (*.png *.jpg *.bmp)");if(!fileName.isEmpty()){m_leftFileName = fileName;ui->face1Label->clear();ui->face1Label->setPixmap(QPixmap(fileName));return;}ui->face1Label->clear();ui->face1Label->setText("face1");m_leftFileName = ""; }void Widget::loadRightLabel() {QString fileName = QFileDialog::getOpenFileName(this, "選擇圖片", qApp->applicationDirPath(), "Image Files (*.png *.jpg *.bmp)");if(!fileName.isEmpty()){m_rightFileName = fileName;ui->face2Label->clear();ui->face2Label->setPixmap(QPixmap(fileName));return;}ui->face2Label->clear();ui->face2Label->setText("face1");m_rightFileName = ""; }void Widget::mouseReleaseEvent(QMouseEvent *event) {int currentX = event->pos().x();int currentY = event->pos().y();int leftPicStartX = ui->face1Label->pos().x();int leftPicEndX = leftPicStartX + ui->face1Label->width();int leftPicStartY = ui->face1Label->pos().y();int leftPicEndY = leftPicStartY + ui->face1Label->height();int rightPicStartX = ui->face2Label->pos().x();int rightPicEndX = rightPicStartX + ui->face2Label->width();int rightPicStartY = ui->face2Label->pos().y();int rightPicEndY = rightPicStartY + ui->face2Label->height();//left Pictureif((leftPicStartX < currentX) && (leftPicEndX > currentX)&& (leftPicStartY < currentY) && (leftPicEndY > currentY)){qDebug() << "left Picture";loadLeftLabel();}//right Pictureif((rightPicStartX < currentX) && (rightPicEndX > currentX)&& (rightPicStartY < currentY) && (rightPicEndY > currentY)){qDebug() << "right Picture";loadRightLabel();}QWidget::mouseReleaseEvent(event); }QHttpPart Widget::setHttpPart(QByteArray body, QVariant value) {QHttpPart part;part.setHeader(QNetworkRequest::ContentDispositionHeader, value);part.setBody(body);return part; }QHttpPart Widget::imageHttpPart(const QString &fileName, QVariant value) {QHttpPart imagePart;imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, value);QByteArray fileFlow;QFile file(fileName);if(file.open(QIODevice::ReadOnly)){fileFlow = file.readAll();imagePart.setBody(fileFlow.toBase64());}return imagePart; }void Widget::finished(QNetworkReply *reply) {QByteArray responseData = reply->readAll();QMap<QString, QVariant> map;QJsonDocument document;map = document.fromJson(responseData).toVariant().toMap();qDebug() << responseData;float confidence = map.value("confidence").toFloat();QMessageBox::information(this, "tip", "The confidence is " + QString::number(confidence)); }void Widget::btnClicked() {QString apiKey = "XqIypFbRHDRJ2LmBfMwO531E8wnZ0FVm";QString apiSecret = "TBS_8r7czQ3_ZLmox-jv9E3tOLyjIjop";QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);QHttpPart keyPart = setHttpPart(apiKey.toLatin1(), QVariant("form-data; name=\"api_key\""));multiPart->append(keyPart);QHttpPart secret = setHttpPart(apiSecret.toLatin1(), QVariant("form-data; name=\"api_secret\""));multiPart->append(secret);QHttpPart faceImage1;faceImage1 = imageHttpPart(m_leftFileName, "form-data; name=\"image_base64_1\"");multiPart->append(faceImage1);QHttpPart faceImage2;faceImage2 = imageHttpPart(m_rightFileName, "form-data; name=\"image_base64_2\"");multiPart->append(faceImage2);QUrl url("https://api-cn.faceplusplus.com/facepp/v3/compare");QNetworkRequest request;request.setUrl(url);QNetworkReply *reply = m_manager->post(request, multiPart);multiPart->setParent(reply); }?
總結
以上是生活随笔為你收集整理的Qt调用face++的http接口,实现人脸对比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java|达梦工作笔记-达梦数据库同步工
- 下一篇: Python笔记-BeautifulSo