树莓派3B+指南(十二)百度语音aip安装与使用

百度语音aip安装与使用

前面的文章snowboy已经可以使用唤醒词了,接下在我们使用百度的语音识别来让树莓派听懂我们的话。基于python目前只有在线模式的,希望百度可以早些日子给出离线的SDK,那么会方便多了。
在百度的技术文档中其实写的很清楚了,但是自己整理一下,便于自己再次阅读。
https://ai.baidu.com/docs#/ASR-Online-Python-SDK/top

1.安装aip
执行下面指令就可以了

pip3 install baidu-aip

2.语音识别与语音合成

from aip import AipSpeech

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

#以下是读取录音文件返回文字的方法
# 读取文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

# 识别本地文件,并返回识别结果
my_file = '你的录音文件'
result = client.asr(get_file_content(my_file), 'pcm', 16000, { 'dev_pid': 1536,})
print(result)

#以下是读取文本,返回audio.mp3文件的方法
result  = client.synthesis('你好百度', 'zh', 1, {
    'vol': 5,
})

# 识别正确返回语音二进制
if not isinstance(result, dict):
    with open('auido.mp3', 'wb') as f:
        f.write(result)

如果有其他错误,或其他配置,请参考百度ai技术文档。

至此就结束了,希望可以帮到您!

你可能感兴趣的:(树莓派,python,语音识别)