基本概念
这里来简单介绍下百度的REST API文档,首先是获取Access Token
(资料图片仅供参考)
获取到了后,再发送请求,进行语音识别。
这里我采用Raw方式上传
这个就是百度AI语音识别服务的API,
这里我自己录了一段话:
注意他的格式和参数
代码与实例
程序运行截图如下:
这个识别还是比较准确的。
源代码如下:
#include #include #include #include #include #include #include #include const QString AppID = \"XXXXXXXXXXXX\";const QString APIKey = \"XXXXXXXXXXXX\";const QString SecretKey = \"XXXXXXXXXXXX\";static QString AccessToken = \"\"; //程序中获取int main(int argc, char *argv[]){QCoreApplication a(argc, argv);QNetworkAccessManager *manager = new QNetworkAccessManager;QNetworkRequest *request = new QNetworkRequest;//获取Access TokenQString tokenUrlStr = \"https://aip.baidubce.com/oauth/2.0/token\";request->setUrl(tokenUrlStr);QString tokenBody = QString(\"grant_type=client_credentials&client_id=%1&client_secret=%2\").arg(APIKey).arg(SecretKey);QNetworkReply *reply = manager->post(*request, tokenBody.toUtf8());QEventLoop loop;QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));loop.exec();QMapmap = QJsonDocument().fromJson(reply->readAll()).toVariant().toMap();AccessToken = map.find(\"refresh_token\").value().toString();qDebug() << \"get AccessToken finised! The AccessToken is : \" << AccessToken;QString requestUrl = QString(\"http://vop.baidu.com/server_api?cuid=123456&token=%1\").arg(AccessToken);request->setUrl(requestUrl);request->setHeader(QNetworkRequest::ContentTypeHeader, \"audio/wav;rate=16000\");request->setRawHeader(\"format\", \"wav\");QFile file(\"E:\Qt2019\voiceDemo\test3.wav\");if(!file.open(QIODevice::ReadOnly)){qDebug() << \"open wav filed!\";return 0;}reply = manager->post(*request, file.readAll());file.close();QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));loop.exec();qDebug() << QString::fromUtf8(reply->readAll());delete request;delete manager;return a.exec();}
【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】
点击这里: