语音识别,当然还是要先看文档啦,根据文档的提示操作

先录一段音,保存在自己能记得住的路径下,录好的音频不是百度文档中写的pcm格式的,所以我们这里用到了一个转换的工具,名字叫ffmpeg,下载好ffmpeg后,打开文件,找到里面的bin目录,复制bin目录的路径,添加到环境变量中,

 1 from aip import AipSpeech
 2 import os
 3 
 4 
 5 """ 你的 APPID AK SK """
 6 APP_ID = '15420336'
 7 API_KEY = 'VwSGcqqwsCl282LGKnFwHDIA'
 8 SECRET_KEY = 'h4oL6Y9yRuvmD0oSdQGQZchNcix4TF5P'
 9 
10 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
11 
12 
13 # 读取文件
14 def get_file_content(filePath):
15     # 执行系统命令,转换音频格式
16     os.system(f"ffmpeg -y  -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm")
17     with open(f"{filePath}.pcm", 'rb') as fp:
18         return fp.read()
19 
20 # 识别本地文件
21 res = client.asr(get_file_content('wyn.wma'), 'pcm', 16000, {
22     'dev_pid': 1536,
23 })
24 
25 print(res.get("result")[0])