語音識別極速版能將60秒以內的完整音頻檔案識別為文字,用于近場短語音互動,如手機語音搜索、聊天輸入等場景, 支持上傳完整的錄音檔案,錄音檔案時長不超過60秒,實時回傳識別結果,本文主要介紹采用百度語音識別,實作小程式的聽寫功能,
想了解微信小程式的開發程序,請參看我之前的帖子:《UNIT接入小程式》https://ai.baidu.com/forum/topic/show/953022
想了解語音識別極速版的呼叫程序,請參看我之前的帖子:《語音問答機器人小程式》
https://ai.baidu.com/forum/topic/show/953177
1 系統框架
用到的技術主要有:百度語音識別和微信小程式,采用微信提供的錄音管理器 recorderManager實作錄音,錄音格式aac,小程式將用戶上傳的語音提交給百度語音證識別服務,回傳文本資訊并顯示出來,全部功能都在小程式客戶端完成,不需要服務器,適合個人開發者學習除錯使用,同時也為商業應用提供相應解決方案,
2創建小程式專案
在根目錄的全域組態檔app.json中增加:"pages/asr/asr" ,會自動創建相關頁面檔案,結構如下:
asr.js:功能邏輯模塊
asr.wxss:頁面樣式檔案
asr.wxml:頁面布局檔案
asr.json:頁面組態檔
3 呼叫語音識別極速版API
3.1 首先要在控制臺創建應用,呼叫語音識別極速版API,“獲取API Key/Secret Key”,
介面檔案地址:https://ai.baidu.com/docs#/ASR-API-PRO/top
請求URL: https://vop.baidu.com/pro_api
Body中放置請求引數,引數詳情如下:
回傳引數:
3.2 語音識別極速版功能實作
(1)發送URL請求核心代碼
//在baiduai.js中發送URL請求,并進行封裝,
let asrRequest = (tempFilePath, len, arg) =>{ // corpus是要發送的對話;arg是回呼方法
var that = this;
var voice = fs.readFileSync(tempFilePath, "base64");
var asr_token = app.globalData.access_token;
console.log("[Console log]:asr_token" + asr_token);
var rqJson = {
'dev_pid': 80001,
'format': 'm4a',
'rate': 16000,
'token': asr_token,
'cuid': 'qwertyuguilgfds678iutfydthrgfe',
'channel': 1,
'len': len,
'speech': voice
};
var rq = JSON.stringify(rqJson);
console.log(rq);
var ASRUrl = app.globalData.ASRUrl;
// cusid是用來實作背景關系的,可以自己隨意定義內容,要夠長夠隨機
var cusid = app.globalData.NLPCusid;
//console.log("[Console log]:ASRRequest(),URL:" + ASRUrl);
wx.request({
url: ASRUrl,
data: rq,
header: { 'content-type': 'application/json' },
method: 'POST',
success: function (res) {
var resData = https://www.cnblogs.com/AIBOOM/p/res.data;
// var text = resData.result;
console.log("[Console log]:resData" + resData);
var nli = JSON.stringify(resData);
console.log("[Console log]:Result:" + nli);
// 回呼函式,決議資料
typeof arg.success == "function" && arg.success(nli);
},
fail: function (res) {
// console.log("[Console log]:ASRRequest() failed...");
// console.error("[Console log]:Error Message:" + res.errMsg);
typeof arg.fail == "function" && arg.fail();
},
complete: function () {
// console.log("[Console log]:ASRRequest() complete...");
typeof arg.complete == "function" && arg.complete();
}
})
}
//介面
module.exports = {
asrRequest:asrRequest,
}
(2)定義按鈕點擊事件
//在asr.js中定義按鈕點擊事件
sendAsrRequest(tempFilePath, fileSize) {
var that = this;
api.asrRequest(tempFilePath, fileSize, {
'success': function (res) {
var resData = https://www.cnblogs.com/AIBOOM/p/JSON.parse(res);
// console.log(res.result);
// var resData = res.data;
//提取json資料的'result'
var asr_out = resData.result;
that.setData({asr_output: asr_out});
console.log("有回傳語音:"+asr_out);
if (res.status == "error") {
wx.showToast({
title: '回傳asr資料有誤!',
})
return;
}
},
'fail': function (res) {
wx.showToast({
title: '請求asr失敗!',
})
return;
}
});
},
(3)定義按鈕點擊事件
//在asr.js中定義定義按鈕點擊事件
// 按鈕按下
touchdown: function () {
var that = this;
// 開始錄音
recorderManager.start(voiceOptions);
this.setData({
isSpeaking: true,
})
that.speaking.call();
// console.log("[Console log]:Touch down!Start recording!");
},
// 停止錄音,會觸發onStop事件
touchup: function () {
var that = this;
recorderManager.stop(voiceOptions)
// console.log("[Console log]:Touch up!Stop recording!");
this.setData({
isSpeaking: false,
speakerUrl: '/res/image/speaker.png',
})
clearInterval(that.speakerInterval);//定時器停止
},
// 添加錄音停止觸發事件,這段代碼可以放到onLoad()里,頁面加載的時候就添加上
recorderManager.onStop((res) => {
const { tempFilePath, fileSize } = res
// console.log("ok!!res:", res);
this.sendAsrRequest(res.tempFilePath, res.fileSize);
// console.log("ok!! res.fileSize:", res.fileSize);
// console.log("ok!! res.tempFilePath:", res.tempFilePath);
});
recorderManager.onError((res) => {
// console.log("error", res);
});
(4)修改頁面樣式檔案
/* pages/asr/asr.wxss */
.atbottom {
width: 100%;
height: 50px;
display: flex;
flex-direction: row;
justify-content: center;
position: fixed;
background: #3366FF;
bottom: 0;
}
.result{
font-size: 32rpx;
color: #fa4627;
border-top: 1rpx solid #eeeeee;
margin:30rpx 20rpx 0rpx 20rpx;
padding: 10rpx;
}
.card {
border: 2px solid #807474e5;
border-radius: 5px;
height: 450px;
background-color: #f7f33b94;
box-shadow: 4px 1px 1px #cccccc;
margin: 8px;
position: relative;
}
.image {
width: 10%;
height: 20px;
background-color: #eeeeee;
4 實作效果
作者:wangwei8638
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/4058.html
標籤:其他
