首先来安装所需要的第三方库:
pip3 install baidu-aip
为了播放音频,还需要安装播放器:
sudo apt-get install omxplayer
sudo apt-get -y install mpg321
然后来看第一步,录音,通过麦克风录入音频,文件保存在当前目录下,一个函数解决:
def Sound_Recording(path) :
# 1.录音,通过麦克风录入音领,文件保存在当前目录下。
print("Recording: ")
os.system(\'sudo arecord -D "plughw:1,0" -f S16_LE -r 16000 -d 4\' + path)
# time.sleep(2)
# print("play:" )
# os.system( \'sudo omxplayer +path)
其次来看第二步,通过调用百度语音识别的的API将音频文件转换为文本文件,这里需要在百度开发者平台申请一个应用:
APP_ID = \'16****18\'
API_KEY = \'MBb******************U02\'
SECRET_KEY = \'WEGcGnz******************0ih51bN\'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
def speech_recognition(path) :
# 2.通过调用百度语音识别的API将音频文件转换为文本文件。
with open(path, \'rb\') as fp :
voices = fp.read()
try :
result = client.asr(voices, \'wav\', 16000, {\'dev_pid\' : 1537.})
# print(result )
result_text = result["result"][0]
print("you said: " + result_text)
return result_text
except KeyError :
print("KeyError")
然后是第三步,将文本文件通过图灵机器人的API进行对话处理,保存回复文本文件,这里同样需要在图灵机器人平台申请一个聊天机器人:
turing_api_key = \'fad1ed7e**************1b14206fd0\'
api_url = \'http://openapi.tuling123.com/openapi/api/v2\'
headers = {\'Content-Type\':\'application/json;charset=UTF-8\'}
def Tu_Ling(text_words=""):
#3.将文本文件通过图灵机器人的API进行对话处理,保存恢复文本文件,
req={
"reqType":0,
"perception": {
"inputText": {
"text": text_words
},
"selfInfo": {
"location": {
"city": "天津",
"province":"天津",
"street": "天津科技大学"
}
}
},
"userInfo": {
"apiKey": turing_api_key,
"userId": "Alex"
}
}
req["perception"]["inputText"]["text"] = text_words
response = requests.request("post",api_url, json=req, headers=headers)
response_dict = json.loads(response.text)
result = response_dict ["results"][0]["values"]["text"]
print("AI Robot said: "+ result)
return result
最后一步,将回复文本文件转换为语音。:
def speech_synthesis(text_words=""):
#4.将回复文本文件转换为语音。
result = client.synthesis(text_words, \'zh\', 1, {\'per\':4, \'vol\':10, \'pit\':9, \'spd\':5})
if not isinstance(result, dict):
with open(\'auido.mp3\',\'wb\') as f:
f.write(result)
os. system(\'mpg321 auido.mp3\')