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 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 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)