def activeListen(self, silent=False): """主动问一个问题(适用于多轮对话)""" if config.get("/LED/enable", False): LED.wakeup() logger.debug("activeListen") try: if not silent: Player.play(constants.getData("beep_hi.wav")) listener = snowboydecoder.ActiveListener([ constants.getHotwordModel(config.get("hotword", "wukong.pmdl")) ]) voice = listener.listen( silent_count_threshold=config.get("silent_threshold", 15), recording_timeout=config.get("recording_timeout", 5) * 4, ) if not silent: Player.play(constants.getData("beep_lo.wav")) if voice: query = self.asr.transcribe(voice) utils.check_and_delete(voice) return query return "" except Exception as e: logger.error("主动聆听失败".format(e)) return ""
def activeListen(self, silent=False): """ 主动问一个问题(适用于多轮对话) """ if config.get('/LED/enable', False): LED.wakeup() logger.debug('activeListen') try: if not silent: #time.sleep(1) Player.play(constants.getData('beep_hi.wav')) listener = snowboydecoder.ActiveListener([ constants.getHotwordModel(config.get('hotword', 'wukong.pmdl')) ]) voice = listener.listen( silent_count_threshold=config.get('silent_threshold', 15), recording_timeout=config.get('recording_timeout', 5) * 4) if not silent: Player.play(constants.getData('beep_lo.wav')) if voice: query = self.asr.transcribe(voice) utils.check_and_delete(voice) return query return '' except Exception as e: logger.error(e) return ''
def do_not_bother_callback(): utils.do_not_bother = not utils.do_not_bother if utils.do_not_bother: snowboydecoder.play_audio_file(constants.getData('off.wav')) logger.info('勿扰模式打开') else: snowboydecoder.play_audio_file(constants.getData('on.wav')) logger.info('勿扰模式关闭')
def activeListen(self): """ 主动问一个问题(适用于多轮对话) """ snowboydecoder.play_audio_file(constants.getData('beep_hi.wav')) listener = snowboydecoder.ActiveListener([constants.getHotwordModel(config.get('hotword', 'wukong.pmdl'))]) voice = listener.listen() snowboydecoder.play_audio_file(constants.getData('beep_lo.wav')) query = self.asr.transcribe(voice) utils.check_and_delete(voice) return query
def activeListen(self): """ 主动问一个问题(适用于多轮对话) """ time.sleep(1) Player.play(constants.getData('beep_hi.wav')) listener = snowboydecoder.ActiveListener( [constants.getHotwordModel(config.get('hotword', 'wukong.pmdl'))]) voice = listener.listen( silent_count_threshold=config.get('silent_threshold', 15), recording_timeout=config.get('recording_timeout', 5) * 4) Player.play(constants.getData('beep_lo.wav')) query = self.asr.transcribe(voice) utils.check_and_delete(voice) return query
def switch_off_do_not_bother(self): """ 关闭勿扰模式 """ utils.do_not_bother = False Player.play(constants.getData("on.wav")) logger.info("勿扰模式关闭")
def switch_on_do_not_bother(self): """ 打开勿扰模式 """ utils.do_not_bother = True Player.play(constants.getData("off.wav")) logger.info("勿扰模式打开")
def _do_not_bother_on_callback(self): if config.get('/do_not_bother/hotword_switch', False): utils.do_not_bother = True Player.play(constants.getData('off.wav')) logger.info('勿扰模式打开') data = [255, 0, 0] * 12 pixels.show(data)
def detected_callback(): if not utils.is_proper_time(): return snowboydecoder.play_audio_file(constants.getData('beep_hi.wav')) global player if player is not None and player.is_playing(): player.stop() player = None
def converse(self, fp): """ 核心对话逻辑 """ try: self.interrupt() snowboydecoder.play_audio_file(constants.getData('beep_lo.wav')) query = self.asr.transcribe(fp) utils.check_and_delete(fp) self._doResponse(query) except Exception as e: logger.critical(e) utils.clean()
def _detected_callback(self): if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return Player.play(constants.getData('beep_hi.wav')) logger.info('开始录音') self._conversation.interrupt() self._conversation.isRecording = True
def _detected_callback(self): def start_record(): logger.info('开始录音') self._conversation.isRecording = True; if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return self._conversation.interrupt() Player.play(constants.getData('beep_hi.wav'), onCompleted=start_record, wait=True)
def _detected_callback(self): if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return server.onSay("{\"action_info\": \"wake\",\"msg\": \"唤醒\"}") # self._conversation.say(random.choice(self.wakes)) Player.play(constants.getData('bee_wake.mp3')) logger.info('开始录音') self._conversation.interrupt() self._conversation.isRecording = True
def _detected_callback(self): print("3") if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return self._conversation.say('在“滴”声后,说出你想去的地方') time.sleep(4) Player.play(constants.getData('beep_hi.wav')) logger.info('开始录音') self._conversation.interrupt() self._conversation.isRecording = True
def conversation(fp): global player, asr, ai, tts try: snowboydecoder.play_audio_file(constants.getData('beep_lo.wav')) print("converting audio to text") query = asr.transcribe(fp) utils.check_and_delete(fp) msg = ai.chat(query) voice = tts.get_speech(msg) player = Player.getPlayerByFileName(voice) player.play(voice) except ValueError as e: logger.critical(e) utils.clean()
def _detected_callback(self): def start_record(): logger.info('开始录音') print('开始录音') self._conversation.isRecording = True; if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return if self._conversation.isRecording: logger.warning('正在录音中,跳过') return self._conversation.interrupt() if config.get('/LED/enable', False): LED.wakeup() Player.play(constants.getData(random.choice(self._response_wav)), onCompleted=start_record, wait=True) with open("../communication/InPut.txt", "w") as file_writer: file_writer.write("true")
def converse(self, fp, callback=None): """ 核心对话逻辑 """ Player.play(constants.getData('beep_lo.wav')) logger.info('结束录音') self.isRecording = False if self.profiling: logger.info('性能调试已打开') pr = cProfile.Profile() pr.enable() self.doConverse(fp, callback) pr.disable() s = io.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print(s.getvalue()) else: self.doConverse(fp, callback)
def _detected_callback(self): def start_record(): logger.info("开始录音") self._conversation.isRecording = True utils.setRecordable(True) if not utils.is_proper_time(): logger.warning("勿扰模式开启中") return if self._conversation.isRecording: logger.warning("正在录音中,跳过") return self._conversation.interrupt() if config.get("/LED/enable", False): LED.wakeup() utils.setRecordable(False) Player.play(constants.getData("beep_hi.wav"), onCompleted=start_record, wait=True)
def handle(self, text, parsed): sys.path.append(constants.LIB_PATH) from robot.utils import emailUser quality = 100 count_down = 3 dest_path = os.path.expanduser('~/pictures') vertical_flip = False horizontal_flip = False send_to_user = True sound = True usb_camera = False # read config profile = config.get() if profile[self.SLUG] and 'enable' in profile[self.SLUG] and \ profile[self.SLUG]['enable']: if 'count_down' in profile[self.SLUG] and \ profile[self.SLUG]['count_down'] > 0: count_down = profile[self.SLUG]['count_down'] if 'quality' in profile[self.SLUG] and \ profile[self.SLUG]['quality'] > 0: quality = profile[self.SLUG]['quality'] if 'dest_path' in profile[self.SLUG] and \ profile[self.SLUG]['dest_path'] != '': dest_path = profile[self.SLUG]['dest_path'] if 'vertical_flip' in profile[self.SLUG] and \ profile[self.SLUG]['vertical_flip']: vertical_flip = True if 'horizontal_flip' in profile[self.SLUG] and \ profile[self.SLUG]['horizontal_flip']: horizontal_flip = True if 'send_to_user' in profile[self.SLUG] and \ not profile[self.SLUG]['send_to_user']: send_to_user = False if 'sound' in profile[self.SLUG] and \ not profile[self.SLUG]['sound']: sound = False if 'usb_camera' in profile[self.SLUG] and \ profile[self.SLUG]['usb_camera']: usb_camera = True if any(word in text for word in [u"安静", u"偷偷", u"悄悄"]): sound = False try: if not os.path.exists(dest_path): os.makedirs(dest_path) except Exception: self.say(u"抱歉,照片目录创建失败", cache=True) return dest_file = os.path.join(dest_path, "%s.jpg" % time.time()) if usb_camera: command = "fswebcam --no-banner -r 1024x765 -q " if vertical_flip: command = command+' -s v ' if horizontal_flip: command = command+'-s h ' command = command+dest_file else: command = ['raspistill', '-o', dest_file, '-q', str(quality)] if count_down > 0 and sound: command.extend(['-t', str(count_down*1000)]) if vertical_flip: command.append('-vf') if horizontal_flip: command.append('-hf') if sound and count_down > 0: self.say(u"收到,%d秒后启动拍照" % (count_down), cache=True) if usb_camera: time.sleep(count_down) process = subprocess.Popen(command, shell=usb_camera) res = process.wait() if res != 0: if sound: self.say(u"拍照失败,请检查相机是否连接正确", cache=True) return if sound: self.play(constants.getData('camera.wav')) # send to user if send_to_user: if sound: self.say(u'拍照成功!正在发送照片到您的邮箱' % target, cache=True) if emailUser(u"这是刚刚为您拍摄的照片", "", [dest_file]): if sound: self.say(u'发送成功', cache=True) else: if sound: self.say(u'发送失败了', cache=True) else: self.say(u"请先在配置文件中开启相机拍照功能", cache=True)
def _do_not_bother_off_callback(self): utils.do_not_bother = False snowboydecoder.play_audio_file(constants.getData('on.wav')) logger.info('勿扰模式关闭')
def _do_not_bother_off_callback(self): utils.do_not_bother = False Player.play(constants.getData('on.wav')) logger.info('勿扰模式关闭')
def _detected_callback(self): if not utils.is_proper_time(): logger.warning('勿扰模式开启中') return snowboydecoder.play_audio_file(constants.getData('beep_hi.wav')) self._conversation.interrupt()
def _do_not_bother_on_callback(self): utils.do_not_bother = True snowboydecoder.play_audio_file(constants.getData('off.wav')) logger.info('勿扰模式打开')
def converse(self, fp, callback=None): """ 核心对话逻辑 """ Player.play(constants.getData('beep_lo.wav')) logger.info('结束录音') self.isRecording = False self.doConverse(fp, callback)
def _recored_callback(self, fp): server.onSay("{\"action_info\": \"think\",\"msg\": \"思考\"}") logger.info('结束录音 开始思考') Player.play(constants.getData('bee_complte.mp3')) self._conversation.converse(fp, self._end_think)
def handle(self, text, parsed): quality = config.get("/camera/quality", 100) count_down = config.get("/camera/count_down", 3) dest_path = config.get("/camera/dest_path", os.path.expanduser("~/pictures")) device = config.get("/camera/device", "/dev/video0") vertical_flip = config.get("/camera/verical_flip", False) horizontal_flip = config.get("/camera/horizontal_flip", False) sound = config.get("/camera/sound", True) camera_type = config.get("/camera/type", 0) if config.has("/camera/usb_camera") and config.get( "/camera/usb_camera"): camera_type = 0 if any(word in text for word in ["安静", "偷偷", "悄悄"]): sound = False try: if not os.path.exists(dest_path): os.makedirs(dest_path) except Exception: self.say("抱歉,照片目录创建失败", cache=True) return dest_file = os.path.join(dest_path, "%s.jpg" % time.time()).replace(".", "", 1) if camera_type == 0: # usb camera logger.info("usb camera") command = [ "fswebcam", "--no-banner", "-r", "1024x765", "-q", "-d", device ] if vertical_flip: command.extend(["-s", "v"]) if horizontal_flip: command.extend(["-s", "h"]) command.append(dest_file) elif camera_type == 1: # Raspberry Pi 5MP logger.info("Raspberry Pi 5MP camera") command = ["raspistill", "-o", dest_file, "-q", str(quality)] if count_down > 0 and sound: command.extend(["-t", str(count_down * 1000)]) if vertical_flip: command.append("-vf") if horizontal_flip: command.append("-hf") else: # notebook camera logger.info("notebook camera") command = ["imagesnap", dest_file] if count_down > 0 and sound: command.extend(["-w", str(count_down)]) if sound and count_down > 0: self.say("收到,%d秒后启动拍照" % (count_down), cache=True) if camera_type == 0: time.sleep(count_down) try: subprocess.run(command, shell=False, check=True) if sound: self.play(constants.getData("camera.wav")) photo_url = "http://{}:{}/photo/{}".format( config.get("/server/host"), config.get("/server/port"), os.path.basename(dest_file), ) self.say("拍照成功:{}".format(photo_url), cache=True) except subprocess.CalledProcessError as e: logger.error(e) if sound: self.say("拍照失败,请检查相机是否连接正确", cache=True)
def handle(self, text, parsed): quality = config.get('/camera/quality', 100) count_down = config.get('/camera/count_down', 3) dest_path = config.get('/camera/dest_path', os.path.expanduser('~/pictures')) device = config.get('/camera/device', '/dev/video0') vertical_flip = config.get('/camera/verical_flip', False) horizontal_flip = config.get('/camera/horizontal_flip', False) sound = config.get('/camera/sound', True) camera_type = config.get('/camera/type', 0) if config.has('/camera/usb_camera') and config.get( '/camera/usb_camera'): camera_type = 0 if any(word in text for word in [u"安静", u"偷偷", u"悄悄"]): sound = False try: if not os.path.exists(dest_path): os.makedirs(dest_path) except Exception: self.say(u"抱歉,照片目录创建失败", cache=True) return dest_file = os.path.join(dest_path, "%s.jpg" % time.time()) if camera_type == 0: # usb camera logger.info('usb camera') command = [ 'fswebcam', '--no-banner', '-r', '1024x765', '-q', '-d', device ] if vertical_flip: command = command + ' -s v ' if horizontal_flip: command = command + '-s h ' command = command + dest_file elif camera_type == 1: # Raspberry Pi 5MP logger.info('Raspberry Pi 5MP camera') command = ['raspistill', '-o', dest_file, '-q', str(quality)] if count_down > 0 and sound: command.extend(['-t', str(count_down * 1000)]) if vertical_flip: command.append('-vf') if horizontal_flip: command.append('-hf') else: # notebook camera logger.info('notebook camera') command = ['imagesnap', dest_file] if count_down > 0 and sound: command.extend(['-w', str(count_down)]) if sound and count_down > 0: self.say(u"收到,%d秒后启动拍照" % (count_down), cache=True) if camera_type == 0: time.sleep(count_down) try: subprocess.run(command, shell=False, check=True) if sound: self.play(constants.getData('camera.wav')) photo_url = 'http://{}:{}/photo/{}'.format( config.get('/server/host'), config.get('/server/port'), os.path.basename(dest_file)) self.say(u'拍照成功:{}'.format(photo_url), cache=True) except subprocess.CalledProcessError as e: logger.error(e) if sound: self.say(u"拍照失败,请检查相机是否连接正确", cache=True)
def converse(self, fp, callback=None): """ 核心对话逻辑 """ snowboydecoder.play_audio_file(constants.getData('beep_lo.wav')) self.doConverse(fp, callback)
def _do_not_bother_off_callback(self): if config.get('/do_not_bother/hotword_switch', False): utils.do_not_bother = False Player.play(constants.getData('on.wav')) logger.info('勿扰模式关闭')
def _do_not_bother_on_callback(self): if config.get('/hotword/hotword_switch', False): utils.do_not_bother = True Player.play(constants.getData('off.wav')) logger.info('勿扰模式打开')