- 注册个百度账号
- 在控制台创建应用,创建完应用后会给你APP_ID,API_KEY ,SECRET_KEY。
- 创建python 环境,
安装百度aip
pip install baidu-aip
4.新建一个python 文件,
from aip import AipSpeech
class Asr(AipSpeech):
def __init__(self):
appId = 'APP_ID'
apiKey = 'API_KEY '
secretKey = 'SECRET_KEY'
# 方法一
# AipSpeech.__init__(self,appId,apiKey,secretKey)
# 方法二
# super().__init__(appId,apiKey,secretKey)
# 方法三
super(Asr,self).__init__(appId,apiKey,secretKey)
def get_file_content(self,filepath):
"""读取录音"""
with open(filepath,"rb") as fp:
return fp.read()
def spr(self,filePath):
"""识别录音"""
speech = self.get_file_content(filePath)
return self.asr(speech,"pcm",16000,{'dev_pid': 1537})
if __name__ == '__main__':
filePath = "录音文件path"
obj=Asr()
ret=obj.spr(filePath)
print(ret)