? ? ? ? 百度語音識(shí)別API是可以免費(fèi)試用的,通過百度賬號(hào)登錄到百度智能云,在語音技術(shù)頁面創(chuàng)建的應(yīng)用,生成一個(gè)語音識(shí)別的應(yīng)用,這個(gè)應(yīng)用會(huì)給你一個(gè)APIKey和一個(gè)Secret Key,如圖14.1所示。
?我們?cè)谧约旱某绦蛑杏?API Key 和 Secret Key 這兩個(gè)值獲取 Koken,然后再通過 Token 調(diào)用語音識(shí)別接口,因此需要經(jīng)過兩次URL請(qǐng)求才能實(shí)現(xiàn)語音識(shí)別功能,第一次請(qǐng)求獲得 Token,第二次請(qǐng)求調(diào)用語音識(shí)別功能。
1、創(chuàng)建語音操作類(create_audio.py)
為了使得音頻操作代碼可以通用和復(fù)用,新建一個(gè)文件create_audio.py,在文件中生成一個(gè)音頻操作類TestAudio,這個(gè)類實(shí)現(xiàn)對(duì)音頻參數(shù)的初始化,錄音和保存音頻文件等功能。樣例代碼如下所示。
# 調(diào)用百度語音識(shí)別
import wave
import pyaudio
class TestAudio:
# 初始化
def __init__(self,fname):
self.chunk=2048
self.samepling_rate=16000
self.sampwidth=2
self.channels=1
self.record_time=6
self.filename=fname
# 把音頻保存到文件,這里data為列表類型
def save_file(self,data):
wf=wave.open(self.filename,'wb')
wf.setnchannels(self.channels)
wf.setsampwidth(self.sampwidth)
wf.setframerate(self.samepling_rate)
wf.writeframes(b"".join(data))
wf.close()
# 進(jìn)行錄音的函數(shù)
def record(self):
pa=pyaudio.PyAudio()
stream=pa.open(format=pyaudio.paInt16,channels=self.channels,rate=self.samepling_rate,input=True,frames_per_buffer=self.chunk)
print("開始錄音,請(qǐng)講話。。。")
mybuf=[]
for i in range(0,int(self.samepling_rate/self.chunk*self.record_time)):
data =stream.read(self.chunk)
mybuf.append(data)
stream.stop_stream()
stream.close()
pa.terminate()
print("錄音結(jié)束")
self.save_file(mybuf)
if __name__=="__main__":
test=TestAudio('test.wav')
test.record()
2、語音識(shí)別函數(shù)(主函數(shù))
注意:更改APIKey 和 SecretKey 值
# 調(diào)用百度語音識(shí)別
import requests
import base64
import CreateTestAudio
vhttp="https:// "
vurl="openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"
APIKey="xx"
SecretKey="xxx"
base_url=vhttp+vurl%(APIKey,SecretKey)
# 獲取token
def getToken(base_url):
res=requests.post(base_url)
return res.json()['access_token']
#
def SpeechTOText(speech_data,token,dev_pid=1537):
FILETYPE='wav'
RATE=16000
CHANNEL=1
CUID='12345678PYTHON12345678'
SPEECH=base64.b64decode(speech_data).decode('utf-8')
data={'format':FILETYPE,'rate':RATE,'channel':CHANNEL,'cuid':CUID,'len':len(speech_data),'speech':SPEECH,'token':token,'dev_pid':dev_pid}
# 拼接URL地址
url=vhttp+'vop.baidu.com/server_api'
headers={'Content-Type':'application/json'}
print('正在識(shí)別。。。')
r=requests.post(url,json=data,headers=headers)
Result=r.json()
if 'result' in Result:
return Result['result'][0]
else:
return Result
def get_audio(file):
with open(file,'rb') as f:
data=f.read()
return data
#主函數(shù)
if __name__=='__main__':
message=input("按任意鍵開始錄入")
test =CreateTestAudio('test.wav')
test.record()
TOKEN =getToken(base_url)
speech =get_audio('test.wav')
result=SpeechTOText(speech,TOKEN,1537)
print(result)
2、測(cè)試
在命令行終端啟動(dòng)程序,根據(jù)提示說一段話,等待程序執(zhí)行,程序返回這段話的內(nèi)容,說明語音識(shí)別程序運(yùn)行成功,如下所示。
文章來源:http://www.zghlxwxcb.cn/news/detail-716276.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-716276.html
到了這里,關(guān)于第14章-Python-人工智能-語言識(shí)別-調(diào)用百度語音識(shí)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!