Python调用百度语音自动识别接口识别本地语音,并保存在本地文档的代码
时间: 2023-12-03 18:42:25 浏览: 27
以下是Python调用百度语音自动识别接口识别本地语音,并保存在本地文档的示例代码:
```python
import os
from aip import AipSpeech
# 设置APPID/AK/SK
APP_ID = 'your_app_id'
API_KEY = 'your_api_key'
SECRET_KEY = 'your_secret_key'
# 新建AipSpeech对象
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 读取本地语音文件
with open('audio.wav', 'rb') as f:
audio_data = f.read()
# 设置语音识别参数
options = {
'dev_pid': 1537 # 普通话输入法模型
}
# 调用语音识别接口
result = client.asr(audio_data, 'wav', 16000, options)
# 打印识别结果
if result['err_no'] == 0:
print(result['result'][0])
else:
print(result['err_msg'])
# 将识别结果保存到本地文档
with open('result.txt', 'w') as f:
f.write(result['result'][0])
```
在使用时,需要先从百度AI开放平台申请自己的APP_ID、API_KEY和SECRET_KEY,并将其替换到代码中的相应位置。此外,还需要将自己的本地语音文件路径替换到代码中的`'audio.wav'`位置,将识别结果保存的路径替换到代码中的`'result.txt'`位置。