def try_getstr(): pyrec.rec("1.wav") pcm_file = wav2pcm.wav_to_pcm("1.wav") # 识别本地文件 with open(pcm_file, "rb") as fp: file_context = fp.read() result = client.asr(file_context, 'pcm', 16000, { 'dev_pid': 1536, }) print(result) result = result.get('result', None) if result: print(result) return result
def processing(limit, begin): i = 0 print('begin', begin.value) while True: # 请说出语音指令 print("\n\n==================================================") rec_yuyin = "D:/Python/self_project/luyin/files/test" + str(i) + ".wav" #print(rec_yuyin) luyin.audio_record(rec_yuyin, 3) pcm_file = wav2pcm.wav_to_pcm(rec_yuyin) asr_result = shibie.shibie(pcm_file) # 识别语音指令 if len(asr_result) != 0: print("Identify Result:", asr_result) print("Start Control...") control.keyboard_control(asr_result, limit, begin) # 根据识别结果控制键盘 print("Control End...") if asr_result.find("退出") != -1: # 如果是"退出"指令则结束程序 break time.sleep(0.5) # 延时1秒 i = i + 1
def asr_main(audio_data_va): pcm_file = wav2pcm.wav_to_pcm(audio_data_va) signal = open(pcm_file, "rb").read() token = get_access_token() ## token = "以上获取token令牌可以保持下来,不用一直获取,一个月有效" uuid = "8C-16-45-44-09-E1" # 机器 MAC 地址 #print(resp) try: resp = baidu_asr(signal, uuid, token) # resp为list格式 if ',' not in resp: res = ''.join(resp) # 将list格式转化为str else: res = "None" except: print("音频质量差") res = "None" print(res) return res
def voice_recognize(): record.rec('test1.wav') #生成录音 time_start = time.time() #计时开始 pcm_file = wav2pcm.wav_to_pcm('test1.wav') #将wav转成pcm # 识别本地文件 result = client.asr(get_file_content(pcm_file), 'pcm', 16000, { 'dev_pid': 1536, }) if (result.get("err_no") == 0): time_end = time.time() #计时结束 res_str = '识别结果:%s,耗时%.2fs' % (result.get("result")[0], (time_end - time_start)) #截取结果 wav2pcm.deletef(pcm_file) #将此次识别的pcm文件删除 else: res_str = "识别失败" wav2pcm.deletef(pcm_file) #将此次识别的pcm文件删除 return res_str
def main(): while True: client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) pyrec.rec("1.wav") # 录音并生成wav文件,使用方式传入文件名 pcm_file = wav2pcm.wav_to_pcm("1.wav") # 将wav文件 转换成pcm文件 返回 pcm的文件名 # 读取文件 , 得到了PCM文件 with open(pcm_file, 'rb') as fp: file_context = fp.read() # 识别本地文件 res = client.asr(file_context, 'pcm', 16000, { 'dev_pid': 1536, }) # 从字典里面获取"result"的value 列表中第1个元素,就是识别出来的字符串 res_str = res.get("result")[0] print(res_str) #识别出来的内容交给图灵机器人来回答 Q_str = FAQ.answer(res_str, TULING_KEY) # 将转换出来的字符串进行语音合成 synth_file = "synth.mp3" synth_context = client.synthesis(Q_str, "zh", 1, { "vol": 6, "spd": 2, "pit": 3, "per": 0 }) with open(synth_file, "wb") as f: f.write(synth_context) # 播放合成语音 pygame.mixer.init() pygame.mixer.music.load(synth_file) pygame.mixer.music.play() count = 0 while (count < 9): csx = pygame.mixer.music.get_busy() if csx == 1: time.sleep(5) count = count + 1 else: count = 10 pygame.mixer.music.stop()
def parse_voice_file(filename): cmd_say('开始解析语音文件') newFilePath = wav_to_pcm(filename) print("Handling file ", filename) # 通过百度语音识别API识别本地录制并已转化的语音文件 resp = client.asr(get_file_content(newFilePath), 'pcm', 16000, { 'dev_pid': 1536, }) print("AI response:", resp) print("") result = resp['result'] cmd_texts = [] for r in result: print(r) cmd_texts.append(r) cmd_say(u"文件解析的结果是 {}".format(r)) for text in cmd_texts: # We can implement some strategies here if '音乐' in text: cmd_say("检测到有'音乐'这个关键字,正在打开音乐...") os.system("open -a NeteaseMusic") time.sleep(2) # 播放,需要设置全局快捷键 keyboard.press_and_release('ctrl+v') else: if '浏览器' in text: cmd_say("检测到有'浏览器'这个关键字,正在打开Chrome...") os.system("open -a Google\ Chrome") time.sleep(2)
def audio2text(): APP_ID = '11567018' API_KEY = 'v8P6XA1I4A2EGnPRQmpt9D99' SECRET_KEY = 'llbHhdFrRe2TFugk2RbNyCdx6yjLMjAq' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) pyrec.rec("1.wav") pcm = wav2pcm.wav_to_pcm("1.wav") with open('1.pcm', 'rb') as fp: file_context = fp.read() data = client.asr(file_context, 'pcm', 16000, {'dev_pid': 1536}) if (data[u'err_msg'] == u'success.'): with open('test.txt','w+') as f: for i in data[u'result']: print i f.write(i.encode("utf-8")) return True else: return False
#!/usr/bin/python # -*- coding: utf-8 -*- import pyrec import wav2pcm import baidu_ai import play_mp3 #import FAQ import opendb while 1: pyrec.rec("1.wav") pcm_file = wav2pcm.wav_to_pcm("1.wav") print(pcm_file) res_str = baidu_ai.audio_to_text(pcm_file) print(res_str) #Q_str = FAQ.faq(res_str) Q_str = opendb.poetmatch(res_str) if Q_str != 0: synth_file = baidu_ai.text_to_audio(Q_str) #synth_file = baidu_ai.text_to_audio(res_str) play_mp3.playmp3(synth_file)
#!/usr/bin/python3 import pyrec # 录音函数文件 import wav2pcm # wav转换pcm 函数文件 import baidu_ai # 语音合成函数,语音识别函数 文件 import playmp3 # 播放mp3 函数 文件 import FAQ import tuling import sizhi pyrec.rec("1.wav") # 录音并生成wav文件,使用方式传入文件名 pcm_file = wav2pcm.wav_to_pcm("1.wav") # 将wav文件 转换成pcm文件 返回 pcm的文件名 res_str = baidu_ai.audio_to_text(pcm_file) # 将转换后的pcm音频文件识别成 文字 res_str ter = sizhi.speak(res_str) Q_str = str(ter) synth_file = baidu_ai.text_to_audio(Q_str) playmp3.play_mp3(synth_file)
import luyin import wav2pcm import shibie import control import time import os i=0 while True: # 请说出语音指令 print("\n\n==================================================") rec_yuyin = "/home/hsj/Documents/test"+str(i)+".wav" #print(rec_yuyin) luyin.audio_record(rec_yuyin, 3) pcm_file = wav2pcm.wav_to_pcm(rec_yuyin) asr_result = shibie.shibie(pcm_file) # 识别语音指令 if len(asr_result) != 0: print("Identify Result:", asr_result) print("Start Control...") control.keyboard_control(asr_result) # 根据识别结果控制键盘 print("Control End...") if asr_result.find("退出") != -1: # 如果是"退出"指令则结束程序 break; time.sleep(1) # 延时1秒 i=i+1
from aip import AipSpeech import time import wav2pcm #import io #import sys #sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') APP_ID = '18240192' API_KEY = 'l6olC3AktVQfKUksIh1Z8Dv3' SECRET_KEY = '76sHQR5cUo9zigr1g7Ka4zkatNaGrNGT' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) pcm_file = wav2pcm.wav_to_pcm("D4_750.wav") def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() #识别本地文件 start_time = time.time() ret = client.asr(get_file_content('F:\\p project\\3.7\\D4_750.wav'), 'pcm', 16000, { 'dev_pid': 1537, }) used_time = time.time() - start_time print("used time: {}s".format(round(time.time() - start_time, 2))) print('ret:{}'.format(ret))