数据集:
//设置request按钮的文本显示
ui->request->setText("获取AccessToken");
ui->request->setDisabled(true);
//设置request按钮的可点击属性
ui->request->setEnabled(true);
getAccessToken = connect(ui->request, SIGNAL(clicked()), this, SLOT(on_getAccessToken()));
//获取AccessToken
void MainWindow::on_getAccessToken(){
QString apiKey = "x7AfM65W69R5Z6fY8wVYvOct";//替换为个人的apiKey
QString secretKey = "qw1eG**************AV6d7oNM";//替换为个人的secretKey
QNetworkRequest request;
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("charset", "UTF-8");
QUrl url("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey);
request.setUrl(url);
QByteArray data;
get_accessToken = new QNetworkAccessManager(this);
connect(get_accessToken, SIGNAL(finished(QNetworkReply*)), this, SLOT(accessTokenResult(QNetworkReply*)));
get_accessToken->post(request, data);
}
//accessToken结果接收
void MainWindow::accessTokenResult(QNetworkReply* pReply){
if(pReply->error() == QNetworkReply::NoError){
QByteArray string = pReply->readAll();
//获取内容
QJsonParseError jsonErr;
QJsonDocument jsonDoc = QJsonDocument::fromJson(string, &jsonErr);
if (jsonDoc.isObject()){
QJsonObject jsonObj = jsonDoc.object();
accessToken = jsonObj.take("access_token").toString();
qDebug()<<"access_token:"+accessToken;
//替换request按钮功能
disconnect(getAccessToken);
ui->request->setText("识别");
connect(ui->request, SIGNAL(clicked()), this, SLOT(on_recognize()));
}
}
}
void MainWindow::imageToBase64(QImage image){
//image to base64
QByteArray imageData;
QBuffer buff(&imageData);
image.save(&buff, "jpg");
imageDatas = imageData.toBase64();
}
//识别
void MainWindow::on_recognize(){
if (imageDatas.isEmpty())
{
qDebug() << "image base64 empty";
return;
}
if (accessToken.isEmpty())
{
qDebug() << "accessToken empty";
return;
}
QUrl url;
url.setUrl("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/moxingkz?access_token="+accessToken);
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("Content-Type:application/json;charset=UTF-8"));
QJsonObject post_data;;
QJsonDocument document;
post_data.insert("image", imageDatas);
post_data.insert("image_type", "BASE64");
document.setObject(post_data);
QByteArray post_param = document.toJson(QJsonDocument::Compact);
to_recoginze = new QNetworkAccessManager(this);
connect(to_recoginze, SIGNAL(finished(QNetworkReply*)), this, SLOT(recognizeResult(QNetworkReply*)));
to_recoginze->post(request, post_param);
}
//识别结果接收
void MainWindow::recognizeResult(QNetworkReply* pReply){
QByteArray replaystring = pReply->readAll();
qDebug()<<"reganizeResult()"+replaystring;
QNetworkReply::NetworkError err = pReply->error();
if(err != QNetworkReply::NoError) {
qDebug() << "Failed: " << replaystring;
}else {
//获取内容
QJsonParseError jsonErr;
QJsonDocument jsonDoc = QJsonDocument::fromJson(replaystring, &jsonErr);
//qDebug()<
double yesNum = 0, noNum = 0;
if (jsonDoc.isObject()){
QJsonObject jsonObj = jsonDoc.object();
QJsonValue results = jsonObj.value("results");
if (results.isArray()) {
QJsonArray resultsArray = results.toArray();
for (int i = 0; i < resultsArray.count(); i++) {
QJsonObject temp = resultsArray[i].toObject();
if(temp.take("name") == "yes")
yesNum = temp.take("score").toDouble();
else
noNum = temp.take("score").toDouble();
}
}
}
qDebug()<<yesNum;
qDebug()<<noNum;
}
}
void MainWindow::on_openfile_clicked()
{
QString fileName = QFileDialog::getOpenFileName(
this, tr("open image file"),
"./", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)"));
if(fileName.isEmpty())
{
QMessageBox mesg;
mesg.warning(this,"警告","打开图片失败!");
return;
}
qDebug()<<fileName;
QImage image(fileName);
ui->imageview->setPixmap(QPixmap::fromImage(image));
//设置request按钮的可点击属性
ui->request->setEnabled(true);
getAccessToken = connect(ui->request, SIGNAL(clicked()), this, SLOT(on_getAccessToken()));
imageToBase64(image);
}
QT += core gui multimedia multimediawidgets
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void setfblComobox(QCamera *camera);
void imageToBase64(QImage image);
private slots:
void on_screenshot_clicked();
void on_cameraType_activated(int index);
void on_imageCaptured(int id, const QImage &preview);
void on_fbl_activated(int index);
void on_openfile_clicked();
//获取AccessToken
void on_getAccessToken();
//识别
void on_recognize();
//accessToken结果接收
void accessTokenResult(QNetworkReply* pReply);
//识别结果接收
void recognizeResult(QNetworkReply* pReply);
private:
Ui::MainWindow *ui;
QCamera *camera;
QCameraViewfinder *viewfind;
QList<QCameraInfo> cameras;
QCameraImageCapture *imageCapture;
QList<QSize> mResSize = {};//分辨率List 定义
QComboBox *box;
QMetaObject::Connection getAccessToken;
QNetworkAccessManager *get_accessToken;
QNetworkAccessManager *to_recoginze;
QString accessToken, imageDatas;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QComboBox *cameraType = new QComboBox();
box = ui->fbl;
cameraType = ui->cameraType;
cameraType->clear();
cameras = QCameraInfo::availableCameras();
foreach(const QCameraInfo &cameraInfo, cameras) {
//qDebug() << "CameraInfo:" << cameraInfo;
cameraType->addItem(cameraInfo.description());
}
camera = new QCamera(this);
viewfind = new QCameraViewfinder();
viewfind->show();
camera->setViewfinder(viewfind);
imageCapture = new QCameraImageCapture(camera);
camera->start();
connect(imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(on_imageCaptured(int, QImage)));
setfblComobox(camera);
//设置request按钮的文本显示
ui->request->setText("获取AccessToken");
ui->request->setDisabled(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_cameraType_activated(int index)
{
index = ui->cameraType->currentIndex();
qDebug()<<"Index"<< index <<": "<< ui->cameraType->currentText();
camera->stop();
camera = new QCamera(cameras[index]);
camera->setCaptureMode(QCamera::CaptureVideo);
camera->setViewfinder(viewfind);
imageCapture = new QCameraImageCapture(camera);
camera->start();
connect(imageCapture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(on_imageCaptured(int, QImage)));
setfblComobox(camera);
}
void MainWindow::on_imageCaptured(int id, const QImage &preview){
Q_UNUSED(id);
int with = ui->imageview->width();
int height = ui->imageview->height();
QPixmap pixmap = QPixmap::fromImage(preview);
//QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放
ui->imageview->setPixmap(fitpixmap);
if(ui->request->text() != "识别"){
//设置request按钮的可点击属性
ui->request->setEnabled(true);
getAccessToken = connect(ui->request, SIGNAL(clicked()), this, SLOT(on_getAccessToken()));
}
imageToBase64(preview);
}
void MainWindow::on_screenshot_clicked()
{
qDebug()<<"ScreenShot";
imageCapture->capture();
}
void MainWindow::on_fbl_activated(int index){
index = ui->fbl->currentIndex();
//qDebug()<<"Index"<< index <<": "<< ui->fbl->currentText();
//qDebug()<<"mResSize:"<
//设置摄像头参数
QCameraViewfinderSettings set;
set.setResolution(mResSize[index]);
camera->setViewfinderSettings(set);
}
void MainWindow::setfblComobox(QCamera *camera){
mResSize.clear();
mResSize = camera->supportedViewfinderResolutions();
box->clear();
int i=0;
foreach (QSize msize, mResSize) {
//qDebug()<
box->addItem(QString::number(msize.width(),10)+"*"+QString::number(msize.height(),10), i++);
} //摄像头支持分辨率打印
box->setCurrentIndex(i-1);
}
void MainWindow::on_openfile_clicked()
{
QString fileName = QFileDialog::getOpenFileName(
this, tr("open image file"),
"./", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)"));
if(fileName.isEmpty())
{
QMessageBox mesg;
mesg.warning(this,"警告","打开图片失败!");
return;
}
qDebug()<<fileName;
QImage image(fileName);
//等比缩放显示图片
int with = ui->imageview->width();
int height = ui->imageview->height();
QPixmap pixmap = QPixmap::fromImage(image);
//QPixmap fitpixmap = pixmap.scaled(with, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
QPixmap fitpixmap = pixmap.scaled(with, height, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放
ui->imageview->setPixmap(fitpixmap);
//设置request按钮的可点击属性
ui->request->setEnabled(true);
getAccessToken = connect(ui->request, SIGNAL(clicked()), this, SLOT(on_getAccessToken()));
imageToBase64(image);
}
//获取AccessToken
void MainWindow::on_getAccessToken(){
QString apiKey = "x7AfM65W69R5Z6fY8wVYvOct";//替换为个人的apiKey
QString secretKey = "qw1eG**************AV6d7oNM";//替换为个人的secretKey
QNetworkRequest request;
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("charset", "UTF-8");
QUrl url("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey);
request.setUrl(url);
QByteArray data;
get_accessToken = new QNetworkAccessManager(this);
connect(get_accessToken, SIGNAL(finished(QNetworkReply*)), this, SLOT(accessTokenResult(QNetworkReply*)));
get_accessToken->post(request, data);
}
//识别
void MainWindow::on_recognize(){
if (imageDatas.isEmpty())
{
qDebug() << "image base64 empty";
return;
}
if (accessToken.isEmpty())
{
qDebug() << "accessToken empty";
return;
}
QUrl url;
url.setUrl("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/moxingkz?access_token="+accessToken);
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("Content-Type:application/json;charset=UTF-8"));
QJsonObject post_data;;
QJsonDocument document;
post_data.insert("image", imageDatas);
post_data.insert("image_type", "BASE64");
document.setObject(post_data);
QByteArray post_param = document.toJson(QJsonDocument::Compact);
to_recoginze = new QNetworkAccessManager(this);
connect(to_recoginze, SIGNAL(finished(QNetworkReply*)), this, SLOT(recognizeResult(QNetworkReply*)));
to_recoginze->post(request, post_param);
}
//accessToken结果接收
void MainWindow::accessTokenResult(QNetworkReply* pReply){
if(pReply->error() == QNetworkReply::NoError){
QByteArray string = pReply->readAll();
//获取内容
QJsonParseError jsonErr;
QJsonDocument jsonDoc = QJsonDocument::fromJson(string, &jsonErr);
if (jsonDoc.isObject()){
QJsonObject jsonObj = jsonDoc.object();
accessToken = jsonObj.take("access_token").toString();
qDebug()<<"access_token:"+accessToken;
//替换request按钮功能
disconnect(getAccessToken);
ui->request->setText("识别");
connect(ui->request, SIGNAL(clicked()), this, SLOT(on_recognize()));
}
}
}
//识别结果接收
void MainWindow::recognizeResult(QNetworkReply* pReply){
QByteArray replaystring = pReply->readAll();
qDebug()<<"reganizeResult()"+replaystring;
QNetworkReply::NetworkError err = pReply->error();
if(err != QNetworkReply::NoError) {
qDebug() << "Failed: " << replaystring;
}else {
//获取内容
QJsonParseError jsonErr;
QJsonDocument jsonDoc = QJsonDocument::fromJson(replaystring, &jsonErr);
//qDebug()<
double yesNum = 0, noNum = 0;
if (jsonDoc.isObject()){
QJsonObject jsonObj = jsonDoc.object();
QJsonValue results = jsonObj.value("results");
if (results.isArray()) {
QJsonArray resultsArray = results.toArray();
for (int i = 0; i < resultsArray.count(); i++) {
QJsonObject temp = resultsArray[i].toObject();
if(temp.take("name") == "yes")
yesNum = temp.take("score").toDouble();
else
noNum = temp.take("score").toDouble();
}
}
}
qDebug()<<yesNum;
qDebug()<<noNum;
}
}
void MainWindow::imageToBase64(QImage image){
//image to base64
QByteArray imageData;
QBuffer buff(&imageData);
image.save(&buff, "jpg");
imageDatas = imageData.toBase64();
}
<ui version="4.0">
<class>MainWindowclass>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0x>
<y>0y>
<width>824width>
<height>549height>
rect>
property>
<property name="windowTitle">
<string>MainWindowstring>
property>
<widget class="QWidget" name="centralwidget">
<property name="font">
<font>
<pointsize>7pointsize>
font>
property>
<layout class="QHBoxLayout" stretch="4,2">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraintenum>
property>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="imageview">
<property name="maximumSize">
<size>
<width>680width>
<height>400height>
size>
property>
<property name="text">
<string/>
property>
widget>
item>
layout>
widget>
item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<widget class="QWidget" name="widget_3" native="true">
<property name="geometry">
<rect>
<x>0x>
<y>100y>
<width>261width>
<height>161height>
rect>
property>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>0x>
<y>40y>
<width>61width>
<height>31height>
rect>
property>
<property name="font">
<font>
<pointsize>12pointsize>
font>
property>
<property name="text">
<string>摄像头string>
property>
widget>
<widget class="QComboBox" name="cameraType">
<property name="geometry">
<rect>
<x>70x>
<y>50y>
<width>181width>
<height>23height>
rect>
property>
<property name="font">
<font>
<pointsize>10pointsize>
font>
property>
widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>0x>
<y>110y>
<width>71width>
<height>31height>
rect>
property>
<property name="font">
<font>
<pointsize>12pointsize>
font>
property>
<property name="text">
<string>分辨率string>
property>
widget>
<widget class="QComboBox" name="fbl">
<property name="geometry">
<rect>
<x>70x>
<y>110y>
<width>181width>
<height>23height>
rect>
property>
<property name="font">
<font>
<pointsize>10pointsize>
font>
property>
widget>
widget>
<widget class="QWidget" name="widget_4" native="true">
<property name="geometry">
<rect>
<x>0x>
<y>320y>
<width>261width>
<height>181height>
rect>
property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QPushButton" name="screenshot">
<property name="text">
<string>截图string>
property>
widget>
item>
<item>
<widget class="QPushButton" name="openfile">
<property name="text">
<string>打开图片string>
property>
widget>
item>
<item>
<widget class="QPushButton" name="request">
<property name="text">
<string>识别string>
property>
widget>
item>
layout>
widget>
widget>
item>
layout>
widget>
widget>
<resources/>
<connections/>
ui>