由于我的测试经常需要对音频文件内容标注(根据语音的内容用拼音标注),一个一个人工标注很费时费力!那有没有简单点的方法,答案是有!!!一起学习吧~
1.在百度AI开发平台获取AppID、API Key、Secret Key,可以看我这篇博客
2.语音识别脚本如下:
# -*- coding: utf-8 -*-import osimport requestsimport base64from pypinyin import lazy_pinyinfilepath = 'E:\\untitled1\\audio\\test.wav'outpath = 'E:\\untitled1\\audio\\'base_url = "/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"APP_ID = ' ' # 填写之前在ai平台上获得的参数API_KEY = ' ' # 填写之前在ai平台上获得的参数SECRET_KEY = ' ' #填写之前在ai平台上获得的参数HOST = base_url % (API_KEY, SECRET_KEY )def getToken(host):res = requests.post(host)return res.json()['access_token']def get_audio(file):with open(file, 'rb') as f:data = f.read()return datadef speech2text(speech_data, token, dev_pid=1537):FORMAT = 'wav'RATE = '16000'CHANNEL = 1CUID = '*******'SPEECH = base64.b64encode(speech_data).decode('utf-8')data = {'format': FORMAT,'rate': RATE,'channel': CHANNEL,'cuid': CUID,'len': len(speech_data),'speech': SPEECH,'token': token,'dev_pid': dev_pid}url = '/server_api'headers = {'Content-Type': 'application/json'}r = requests.post(url, json=data, headers=headers)Null = r.json()if 'result' in Null:return Null['result'][0]else:return Nullif __name__ == '__main__':TOKEN = getToken(HOST)speech = get_audio(filepath)result = speech2text(speech, TOKEN)print(result)#打印中文识别结果'打开吹风。'#############将中文识别结果转为拼音#############name1 = lazy_pinyin(result)NAME = ''.join(name1)new_name1 = NAME.split("。")new_name2 = new_name1[0]print(new_name2) #打印识别结果的拼音'dakaichuifeng'os.rename(filepath, outpath+new_name2 + '.wav') #标注文件,对文件重命名
运行上面的脚本可以把test.wav音频文件的内容识别并打印出来,并把文件用拼音标注。
2.效果图:
但是一个音频运行一次脚本,也太费时间了吧,所以做个批处理的操作吧~
3.批处理标注文件脚本如下:
# -*- coding: utf-8 -*-import base64import osimport requestsfrom pypinyin import lazy_pinyinfilepath = 'E:\\untitled1\\audio\\' # 文件夹路径base_url = "/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s"APP_ID = ' ' # 填写之前在ai平台上获得的参数API_KEY = ' ' # 填写之前在ai平台上获得的参数SECRET_KEY = ' ' #填写之前在ai平台上获得的参数HOST = base_url % (API_KEY, SECRET_KEY)def getToken(host):res = requests.post(host)return res.json()['access_token']def get_audio(file):with open(file, 'rb') as f:data = f.read()return datadef speech2text(speech_data, token, dev_pid=1537):FORMAT = 'wav'RATE = '16000'CHANNEL = 1CUID = '*******'SPEECH = base64.b64encode(speech_data).decode('utf-8')data = {'format': FORMAT,'rate': RATE,'channel': CHANNEL,'cuid': CUID,'len': len(speech_data),'speech': SPEECH,'token': token,'dev_pid': dev_pid}url = '/server_api'headers = {'Content-Type': 'application/json'}r = requests.post(url, json=data, headers=headers)Null = r.json()if 'result' in Null:return Null['result'][0]else:return Null#########批量重命名文件#############for file in os.listdir(filepath):filename = file.split('-')[0]if os.path.isfile(os.path.join(filepath, file)) == True:TOKEN = getToken(HOST)speech = get_audio(filepath + file)result = speech2text(speech, TOKEN)print(result)name1 = lazy_pinyin(result) # 将识别结果转换为拼音NAME = ''.join(name1) # 换行new_name1 = NAME.split("。")new_name2 = new_name1[0]print(new_name2)os.rename(filepath + file, filepath + new_name2 + '.wav')
4.脚本运行前,文件名如图:
5.脚本运行后,文件名如图:
哈哈哈哈哈,还挺准确的~
就是免费的有次数限制…
如果觉得《Python-实现语音识别并批处理标注(重命名)文件》对你有帮助,请点赞、收藏,并留下你的观点哦!