def update_info(self): #self.root.ids.lable_info.text = u'帅哥,今天你看起来很开心,需要来首歌吗' y = yuyin.Baiduyuyin() t = tuling.Tuling() v = voice.Voice() try: print('听取命令') v.my_record() print('收到命令,进行识别') s = y.asr('01.wav')['result'][0] print('识别结果为:%S' % s) print('启动图灵机器人对话') a = t.get_answer(s) print('图灵机巧人反馈:%s' % a) self.root.ids.lable_info.text = a except: self.root.ids.lable_info.text = "主人,没有听懂你的命令,请重新下命令吧" finally: Clock.schedule_once(lambda dt: self.update_info(), 5)
import record import wav2pcm import sound2word import tuling import speakout record.rec("1k.wav") # 实现录音,将文件存储在1k.wav #pcm_file = wav2pcm.wav_to_pcm("1k.wav") #将wav格式的语音转化为pcm格式 words = sound2word.asr_main("1k.wav") # 读取录音文件,通过API实现语音转写 new_words = tuling.Tuling(words) #实现与图灵机器人会话 speakout.tts_main(new_words) #将机器人回复的文字转化为语音 wav2pcm.play_mp3("test.mp3") #播放机器人的语音文件
count += 1 # print('.') self.save_wave_file('01.wav', my_buf) stream.close() def play(self): wf = wave.open(r"auido.mp3", 'rb') p = PyAudio() stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) while True: data = wf.readframes(self.chunk) if data == "": break stream.write(data) stream.close() p.terminate() if __name__ == '__main__': while True: voice = Voice() voice.my_record() print('Over!') # play() s = yuyin.Baiduyuyin().asr('01.wav')['result'][0] a = tuling.Tuling().get_answer(s) # yuyin.Baiduyuyin().synthesis(a) print(a)
def index(): if request.method == "GET": # 判断请求方式是GET请求 logging.info('receive a get request') try: my_signature = request.args.get('signature') # 获取携带的signature参数 my_timestamp = request.args.get('timestamp') # 获取携带的timestamp参数 my_nonce = request.args.get('nonce') # 获取携带的nonce参数 my_echostr = request.args.get('echostr') # 获取携带的echostr参数 token = 'liujinhong' # 一定要跟刚刚填写的token一致 # 进行字典排序 data = [token, my_timestamp, my_nonce] data.sort() logging.info('data: ' + str(data)) # 拼接成字符串 temp = ''.join(data) # 进行sha1加密 mysignature = hashlib.sha1(temp).hexdigest() # 加密后的字符串可与signature对比,标识该请求来源于微信 if my_signature == mysignature: logging.info('my_signature == mysignature, success') return my_echostr else: logging.info('my_signature != mysignature, success') return "" except Exception as e: logging.info('get request error: ' + str(e)) return "" elif request.method == "POST": # 判断请求方式是POST请求 logging.info('receive a post request') try: recData = request.get_data() recMsg = receiver.parse_xml(recData) if isinstance(recMsg, receiver.Msg): toUser = recMsg.FromUserName fromUser = recMsg.ToUserName if recMsg.MsgType == 'text': logging.info('this is a text message') t = tuling.Tuling() content = t.get_tuling_response(recMsg.Content) replyMsg = replyer.TextMsg(toUser, fromUser, content) return replyMsg.send() if recMsg.MsgType == 'image': logging.info('this is an image message') mediaId = recMsg.MediaId replyMsg = replyer.ImageMsg(toUser, fromUser, mediaId) return replyMsg.send() else: return replyer.Msg().send() else: logging.info('unhandled message type!') return replyer.Msg().send() except Exception as e: logging.info('post request error: ' + str(e)) return "" else: return ""