赞
踩
当下的人工智能势头很盛,虽然咱不是搞算法开发的,但是也不妨碍咱用他们的成果做应用,出于好奇,探索了一下如果使用Qt连接百度AI接口,下面是整个详细历程。
网站链接:https://cloud.baidu.com/?from=console
百度API商城:https://apis.baidu.com/
找一个白嫖资源:
在首页中,依此选择:产品–>人工智能–>图像识别
选上车牌识别接口:
创建完成之后点击管理应用即可看到自己创建的应用:
最终对我们有用的就是:API Key和Secret Key,这两个是用来申请access_token
的,access_token
就是我们在编码过程中访问的地址。
点击技术文档
我选择的是调用方式一
可以看到文档中提供了多种方式获取access_token,例如:bash、PHP、Java、Python、C++、C#、Node等
在此我选择使用Python:
然后access_token就获取成功了。
准备工作
.pro
)添加://注:与安装路径无关,之间复制粘贴即可
INCLUDEPATH += C:/OpenSSL-Win32/include
创建工程
识别流程:本地选中一张图片–>转为base64格式–>post()–>收到结果
QT += core gui network texttospeech
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QNetworkAccessManager>
#include<QtTextToSpeech/QTextToSpeech>
class Widget : public QWidget
{
Q_OBJECT
public slots:
void read_ack(QNetworkReply*);
void load_pix();
void decode_pix();
public:
Widget(QWidget *parent = 0);
~Widget();
QLabel *lb_pix;
QLabel *lb;
QLineEdit *le_path;
QLineEdit *le_code;
QPushButton *btn_load;
QPushButton *btn_decode;
QNetworkAccessManager *httpmanager;
QTextToSpeech *speeker;
};
#endif // WIDGET_H
#include "widget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFileDialog>
#include <QNetworkRequest>
#include <QFile>
#include <QNetworkReply>
#include <QUrl>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QMovie>
#include <QtTextToSpeech/QTextToSpeech>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//1. 构造所有需要的控件
lb_pix = new QLabel;
lb_pix->setFixedSize(470,530);
lb_pix->setScaledContents(true);
btn_load = new QPushButton("载入车牌");
btn_load->setFixedSize(100, 48);
btn_decode = new QPushButton("识别");
btn_decode->setFixedSize(100, 48);
le_path = new QLineEdit;
le_path->setPlaceholderText("车牌图片路径");
le_code = new QLineEdit;
le_code->setPlaceholderText("识别结果");
le_code->setMinimumHeight(50);
le_code->setStyleSheet("QLineEdit{font: 30px;};");
speeker = new QTextToSpeech;
httpmanager = new QNetworkAccessManager;
connect(httpmanager, SIGNAL(finished(QNetworkReply*)), this, SLOT(read_ack(QNetworkReply*)));
//2. 排版
QHBoxLayout *hbox = new QHBoxLayout;
hbox->addWidget(btn_load);
//hbox->addStretch();
hbox->addWidget(btn_decode);
QVBoxLayout *mainbox = new QVBoxLayout;
//mainbox->addWidget(lb);
mainbox->addWidget(lb_pix);
mainbox->addWidget(le_path);
mainbox->addWidget(le_code);
mainbox->addLayout(hbox);
setLayout(mainbox);
//3. 前后台挂接
connect(btn_load, SIGNAL(clicked(bool)), this, SLOT(load_pix()));
connect(btn_decode, SIGNAL(clicked(bool)), this, SLOT(decode_pix()));
//4. 美化
le_path->setStyleSheet("color:#747d8c;font-weight:500;font-size:20px;border-radius: 3px;");
le_code->setStyleSheet("color:#747d8c;font-weight:700;font-size:35px;border-radius: 3px;");
btn_load->setStyleSheet("QPushButton{"
"background-color:#f1f2f6;"
"border-radius: 10px;"
"font: 25px;}"
"QPushButton:hover{"
"background-color:#38ada9;"
"border-radius: 10px;"
"font: 30px;}"
);
btn_decode->setStyleSheet("QPushButton{"
"background-color:#f1f2f6;"
"border-radius: 10px;"
"font: 25px;}"
"QPushButton:hover{"
"background-color:#38ada9;"
"border-radius: 10px;"
"font: 30px;}"
);
}
Widget::~Widget()
{
}
//将二维码路径和图片显示出来
void Widget::load_pix()
{
QString filename = QFileDialog::getOpenFileName(this, "打开车牌照片", ".", "Images (*.png *.bmp *.jpg)");
le_path->setText(filename);
lb_pix->setPixmap(QPixmap(filename));
}
void Widget::decode_pix()
{
//向百度发送一个二维码识别请求(HTTP)
QNetworkRequest myrequset;// = new QNetworkRequest;
//填充请求url
myrequset.setUrl(QUrl("https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?"
"access_token=24.7d894444f8eb3d4d9d413409aafd2048.2592000.1627629226.282335-24468114"));
//填充header
myrequset.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
//填充请求的body
//1.挖出照片内容
QFile file(le_path->text());
file.open(QIODevice::ReadOnly);
QByteArray buf = file.readAll();
//2.转成base64及urlcode
QByteArray buf64 = buf.toBase64().toPercentEncoding();
QByteArray body = "image="+buf64;
//发送完整的一次识别请求
httpmanager->post(myrequset, body);
}
//结果解析
void Widget::read_ack(QNetworkReply* r)
{
//提取json格式的返回
QByteArray buf = r->readAll();
qDebug() << buf;
//提取json中的感兴趣的内容
QJsonDocument myjson = QJsonDocument::fromJson(buf);
QJsonObject node = myjson.object();
QJsonObject arr = node.value("words_result").toObject();
QString text = arr.value("number").toString();
le_code->setText(text);
//speeker->say("欢迎光临"+text);
}
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setStyleSheet("Widget{background-color: #82ccdd};");
w.setWindowTitle("车牌识别");
w.setMinimumSize(500, 600); //设置窗口大小
QIcon icon("src/1.png");
w.setWindowIcon(icon);
w.show();
return a.exec();
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。