def async_listen(call): nonlocal runner, detected_event hass.states.async_set(OBJECT_DECODER, STATE_LISTENING, state_attrs) engine = PreciseEngine('precise-engine', model) runner = PreciseRunner(engine, sensitivity=sensitivity, trigger_level=trigger_level, on_activation=lambda: detected_event.set()) # Runs in a separate thread detected_event.clear() runner.start() yield from asyncio.get_event_loop().run_in_executor(None, detected_event.wait) if not terminated: runner.stop() runner = None hass.states.async_set(OBJECT_DECODER, STATE_IDLE, state_attrs) # Fire detected event hass.bus.async_fire(EVENT_HOTWORD_DETECTED, { 'name': name, # name of the component 'model': model # model used })
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): super(PreciseHotword, self).__init__(key_phrase, config, lang) from precise_runner import (PreciseRunner, PreciseEngine, ReadWriteStream) local_conf = LocalConf(USER_CONFIG) if local_conf.get('precise', {}).get('dist_url') == \ 'http://bootstrap.mycroft.ai/artifacts/static/daily/': del local_conf['precise']['dist_url'] local_conf.store() Configuration.updated(None) self.download_complete = True self.show_download_progress = Timer(0, lambda: None) precise_config = Configuration.get()['precise'] precise_exe = self.install_exe(precise_config['dist_url']) self.precise_model = self.install_model( precise_config['model_url'], key_phrase.replace(' ', '-')).replace('.tar.gz', '.pb') self.has_found = False self.stream = ReadWriteStream() def on_activation(): self.has_found = True self.runner = PreciseRunner(PreciseEngine(precise_exe, self.precise_model), stream=self.stream, on_activation=on_activation) self.runner.start()
def calc_thresh(self, model_file, samples_folder): from precise_runner import PreciseEngine engine = PreciseEngine(self.engine_exe, model_file, self.chunk_size) engine.start() all_max = [] for sample_file in glob(join(samples_folder, '*.wav')): with wave.open(sample_file, 'r') as wr: frames = wr.readframes(wr.getnframes() - 1) chop = len(frames) % self.chunk_size max_pred = float('-inf') for i in range(10): engine.get_prediction(b'\0' * self.chunk_size) for pos in range(chop + self.chunk_size, len(frames) + 1, self.chunk_size): pred = engine.get_prediction(frames[pos - self.chunk_size:pos]) max_pred = max(max_pred, pred) print('MAX PRED:', max_pred) all_max.append(max_pred) av_max = sum(all_max) / len(all_max) stddev = sqrt(sum([(i - av_max)**2 for i in all_max])) good_max = [i for i in all_max if i > av_max - stddev] good_av = sum(good_max) / len(good_max) stddev = sqrt(sum([(i - good_av)**2 for i in good_max])) thresh = good_av - stddev return thresh
def load_runner(self) -> None: """Load precise runner.""" if self.engine is None: # pylint: disable=E0401 from precise_runner import PreciseEngine self.model_name = self.profile.get("wake.precise.model", "hey-mycroft-2.pb") self.model_path = self.profile.read_path(self.model_name) self.engine_path = os.path.expandvars( self.profile.get("wake.precise.engine_path", "precise-engine")) self._logger.debug("Loading Precise engine at %s", self.engine_path) self.engine = PreciseEngine(self.engine_path, self.model_path, chunk_size=self.chunk_size) if self.runner is None: # pylint: disable=E0401 from precise_runner import PreciseRunner, ReadWriteStream self.stream = ReadWriteStream() sensitivity = float( self.profile.get("wake.precise.sensitivity", 0.5)) trigger_level = int( self.profile.get("wake.precise.trigger_level", 3)) def on_prediction(prob: float) -> None: self.prediction_sem.release() def on_activation() -> None: self.detected = True self.send(self.myAddress, "activated") self.runner = PreciseRunner( self.engine, stream=self.stream, sensitivity=sensitivity, trigger_level=trigger_level, on_activation=on_activation, on_prediction=on_prediction, ) assert self.runner is not None self.runner.start() self._logger.debug( "Loaded Mycroft Precise (model=%s, sensitivity=%s, trigger_level=%s)", self.model_path, sensitivity, trigger_level, )
def __init__(self, args): super().__init__(args) if args.model == 'hey-mycroft': args.model = None self.engine = PreciseEngine(exe_file=None, model_file=args.model, chunk_size=args.chunk_size) self.runner = PreciseRunner(self.engine, args.trigger_level, sensitivity=args.sensitivity, on_activation=self.on_activation, on_prediction=self.on_prediction) self.session_id, self.chunk_num = '%09d' % randint(0, 999999999), 0
def main(): parser = ArgumentParser('Implementation demo of precise-engine') parser.add_argument('engine', help='Location of binary engine file') parser.add_argument('model') args = parser.parse_args() def on_prediction(prob): print('!' if prob > 0.5 else '.', end='', flush=True) def on_activation(): Popen(['aplay', '-q', 'data/activate.wav']) engine = PreciseEngine(args.engine, args.model) PreciseRunner(engine, on_prediction=on_prediction, on_activation=on_activation, trigger_level=0).start() Event().wait() # Wait forever
def __init__(self): super().__init__() self._hotwordThread = None try: self._stream = ReadWriteStream() self._handler = PreciseRunner( PreciseEngine( exe_file=f'{self.Commons.rootDir()}/venv/bin/precise-engine', model_file=f'{self.Commons.rootDir()}/trained/hotwords/mycroft-precise/athena.pb' ), sensitivity=self.ConfigManager.getAliceConfigByName('wakewordSensitivity'), stream=self._stream, on_activation=self.hotwordSpotted ) except: self._enabled = False
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): super().__init__(key_phrase, config, lang) global install_package from petact import install_package from precise_runner import (PreciseRunner, PreciseEngine, ReadWriteStream) local_conf = LocalConf(USER_CONFIG) if (local_conf.get('precise', {}).get('dist_url') == 'http://bootstrap.mycroft.ai/artifacts/static/daily/'): del local_conf['precise']['dist_url'] local_conf.store() Configuration.updated(None) self.download_complete = True self.show_download_progress = Timer(0, lambda: None) precise_config = Configuration.get()['precise'] precise_exe = self.update_precise(precise_config) local_model = self.config.get('local_model_file') if local_model: self.precise_model = expanduser(local_model) else: self.precise_model = self.install_model( precise_config['model_url'], key_phrase.replace(' ', '-')).replace('.tar.gz', '.pb') self.has_found = False self.stream = ReadWriteStream() def on_activation(): self.has_found = True trigger_level = self.config.get('trigger_level', 3) sensitivity = self.config.get('sensitivity', 0.5) self.runner = PreciseRunner( PreciseEngine(precise_exe, self.precise_model), trigger_level, sensitivity, stream=self.stream, on_activation=on_activation, ) self.runner.start()
def load_runner(self) -> None: if self.engine is None: from precise_runner import PreciseEngine self.model_name = self.profile.get("wake.precise.model") self.model_path = self.profile.read_path(self.model_name) self.engine_path = self.profile.get("wake.precise.engine_path") self._logger.debug(f"Loading Precise engine at {self.engine_path}") self.engine = PreciseEngine( self.engine_path, self.model_path, chunk_size=self.chunk_size ) if self.runner is None: from precise_runner import PreciseRunner self.stream = ByteStream() sensitivity = float(self.profile.get("wake.precise.sensitivity", 0.5)) trigger_level = int(self.profile.get("wake.precise.trigger_level", 3)) def on_prediction(prob: float) -> None: self.prediction_event.set() def on_activation() -> None: self.detected = True self.runner = PreciseRunner( self.engine, stream=self.stream, sensitivity=sensitivity, trigger_level=trigger_level, on_prediction=on_prediction, on_activation=on_activation, ) assert self.runner is not None self.runner.start() self._logger.debug( "Loaded Mycroft Precise (model=%s, sensitivity=%s, trigger_level=%s)" % (self.model_path, sensitivity, trigger_level) )
def listen_for_wake_words(self, word_library, activation_notifier, startdelay): def on_prediction(prob): pass def on_activation(): if self.speech_locked: print("no activation, speech is locked") return print("ACTIVATION: {}".format(activation_notifier)) msg = RosString() msg.data = activation_notifier self.command_triggered_pub.publish(msg) if activation_notifier != "heyrobot" and activation_notifier != "stop_listening": if self.engines[activation_notifier][2]: self.start_stream_callback(activation_notifier) elif activation_notifier == "heyrobot": if self.engines[activation_notifier][2]: self.start_countdown() self.start_perpetual_stream_callback() else: print("auto 4") self.end_countdown() self.end_perpetual_stream() path = sys.argv[1] time.sleep(startdelay) print("{} ready".format(activation_notifier)) engine = PreciseEngine('{}/.venv/bin/precise-engine'.format(path), word_library) self.engines[activation_notifier][0] = PreciseRunner( engine, on_prediction=on_prediction, on_activation=on_activation, trigger_level=0) self.engines[activation_notifier][0].start()
def __init__(self, factory, client, queue, sample_rate=16000): super(WebsocketAudioListener, self).__init__() self.client = client self.factory = factory self.sample_rate = sample_rate self.vad = webrtcvad.Vad(1) self.queue = queue self.hotword_found = False self.hotword_stream = ReadWriteStream() def on_activation(): self.hotword_found = True trigger_level = 1 sensitivity = 0.5 self.hotword_runner = PreciseRunner( PreciseEngine('/opt/backend/precise-engine/precise-engine', '/opt/backend/hey-mycroft.pb'), trigger_level, sensitivity, stream=self.hotword_stream, on_activation=on_activation, ) self.hotword_runner.start() BLOCKS_PER_SECOND = 50 self.block_size = int(self.sample_rate / float(BLOCKS_PER_SECOND)) # 320 padding_ms = 600 block_duration_ms = 1000 * \ self.block_size // self.sample_rate # 20 num_padding_blocks = padding_ms // block_duration_ms # 30 self.ratio = 0.75 self.ring_buffer = deque(maxlen=num_padding_blocks) self.triggered = False self.running = True
def __init__(self, model): print("*** MODEL ***", model) self.wake_word = rospy.Publisher(WAKE_WORD_TOPIC, Bool, queue_size=10) self.stream = ReadWriteStream() rospy.Subscriber(CONVERTED_AUDIO_TOPIC, Float32MultiArray, self.audio_cb) engine = PreciseEngine(os.path.join(PRECISE_DIR, '.venv/bin/precise-engine'), model, chunk_size=1024) # stream = binary 16 bit mono! PreciseRunner(engine, stream=self.stream, on_prediction=self.on_prediction, on_activation=self.on_activation, trigger_level=3, sensitivity=0.8).start()
def __init__(self): # engine data: https://github.com/mycroftai/precise-data/tree/dist # precise models: https://github.com/MycroftAI/precise-data/tree/models?files=1 sensitivity = float(0.5) trigger_level = int(3) model_path = "models/hey-mycroft.pb" engine_path = "precise-engine/precise-engine" engine = PreciseEngine( engine_path, model_path) self.stream = ReadWriteStream() self.runner = PreciseRunner( engine, sensitivity=sensitivity, trigger_level=trigger_level, on_activation=self.on_activation ) print("Starting precise") self.runner.start() while True: data = self.stream.read() self.update_runner(data)
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): super().__init__(key_phrase, config, lang) from petact import install_package from precise_runner import ( PreciseRunner, PreciseEngine, ReadWriteStream ) global install_package self.download_complete = True self.show_download_progress = Timer(0, lambda: None) precise_config = CONFIGURATION['precise'] precise_exe = self.update_precise(precise_config) local_model = self.config.get('local_model_file') if local_model: self.precise_model = expanduser(local_model) else: self.precise_model = self.install_model( precise_config['model_url'], key_phrase.replace(' ', '-') ).replace('.tar.gz', '.pb') self.has_found = False self.stream = ReadWriteStream() def on_activation(): self.has_found = True trigger_level = self.config.get('trigger_level', 3) sensitivity = self.config.get('sensitivity', 0.5) self.runner = PreciseRunner( PreciseEngine(precise_exe, self.precise_model), trigger_level, sensitivity, stream=self.stream, on_activation=on_activation, ) self.runner.start()
def __init__(self, rt, on_activation: Callable): super().__init__(rt, on_activation) exe_file = which('precise-engine') precise_folder = join(self.rt.paths.user_config, 'precise') if not exe_file: exe_file = join(precise_folder, 'precise-engine', 'precise-engine') download_extract_tar( self.program_url.format(arch=platform.machine()), precise_folder, check_md5=False, subdir='precise-engine', on_update=lambda: self.rt.interfaces.faceplate.text('Updating listener...'), on_complete=lambda: self.rt.interfaces.faceplate.reset() ) log.debug('Using precise executable: ' + exe_file) model_folder = join(precise_folder, 'models', self.wake_word) model_file = join(model_folder, self.wake_word + '.pb') model_url = self.model_url.format(model_name=self.wake_word) download_extract_tar(model_url, model_folder, check_md5=True) from precise_runner import PreciseRunner, PreciseEngine engine = PreciseEngine(exe_file, model_file, chunk_size=1024) self.runner = PreciseRunner(engine, on_activation=on_activation)
def handle_train(self, subfolder, dialog_file): if self.handle_precise_download(): return name = self.get_response('ask.speaker.name', validator=lambda x: x and len(x.split()) < 4, on_fail=lambda utterance: self.dialog_renderer .render('name.error', {'name': utterance})) if not name: return self.speak_dialog(dialog_file) wait_while_speaking() from precise_runner import PreciseEngine engine = PreciseEngine(self.engine_exe, self.model_file, self.chunk_size) engine.start() recording = self.record_wav() with wave.open(recording, 'r') as wr: orig_params = wr.getparams() frames = wr.readframes(wr.getnframes() - 1) ww_positions = self.extract_ww_positions(frames, engine) engine.stop() samples_folder = join(self.folder, 'samples', name) samples_raw_folder = join(samples_folder, 'not-wake-word') makedirs(samples_raw_folder, exist_ok=True) self.split_recording(frames, samples_raw_folder, ww_positions, orig_params) self.speak_dialog('recording.complete') models_folder = join(self.folder, 'user-models') makedirs(models_folder, exist_ok=True) model_file = join(models_folder, '{}.{}.net'.format('hey-mycroft', name)) self.transfer_train(samples_folder, model_file) self.speak_dialog('model.confirm') thresh = self.calc_thresh(model_file, samples_raw_folder) print("THRESH:", thresh)
def __init__(self, keyword=None, sensitivity=None, detected_callback=None ): super(HotwordDetector, self).__init__() sl = SettingLoader() self.settings = sl.settings self.paused_loop = False self.detected_callback = detected_callback self.sensitivity = sensitivity trigger_level = 3 self.keyword = keyword self.found_keyword = False if not os.path.exists(RESOURCE_FILE): if self.downloadPreciseEngine(): Utils.print_info("[Precise] Download complete") else: raise PreciseEngineNotFound("Error downloading precise engine, check your internet connection or try again later.") engine = PreciseEngine(RESOURCE_FILE, self.keyword) self.stream = ReadWriteStream() self.runner = PreciseRunner(engine, sensitivity=float(self.sensitivity), trigger_level=trigger_level, on_activation=self.activation ) self.runner.start() self.pause() # To avoid that precise starts detecting without beeing ready, we pause it right after start if self.settings.machine.startswith("arm"): # Because importing tensorflow takes up to 10 seconds, we sleep a while Utils.print_info("Starting precise trigger") time.sleep(10)
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): super().__init__(key_phrase, config, lang) from precise_runner import ( PreciseRunner, PreciseEngine, ReadWriteStream ) # We need to save to a writeable location, but the key we need # might be stored in a different, unwriteable, location # Make sure we pick the key we need from wherever it's located, # but save to a writeable location only local_conf = LocalConf( join(xdg.BaseDirectory.xdg_config_home, 'mycroft', 'mycroft.conf') ) for conf_dir in xdg.BaseDirectory.load_config_paths('mycroft'): conf = LocalConf(join(conf_dir, 'mycroft.conf')) # If the current config contains the precise key use it, # otherwise continue to the next file if conf.get('precise', None) is not None: local_conf['precise'] = conf.get('precise', None) break # If the key is not found yet, it might still exist on the old # (deprecated) location if local_conf.get('precise', None) is None: local_conf = LocalConf(OLD_USER_CONFIG) if not local_conf.get('precise', {}).get('use_precise', True): raise PreciseUnavailable if (local_conf.get('precise', {}).get('dist_url') == 'http://bootstrap.mycroft.ai/artifacts/static/daily/'): del local_conf['precise']['dist_url'] local_conf.store() Configuration.updated(None) self.download_complete = True self.show_download_progress = Timer(0, lambda: None) precise_config = Configuration.get()['precise'] precise_exe = self.update_precise(precise_config) local_model = self.config.get('local_model_file') if local_model: self.precise_model = expanduser(local_model) else: self.precise_model = self.install_model( precise_config['model_url'], key_phrase.replace(' ', '-') ).replace('.tar.gz', '.pb') self.has_found = False self.stream = ReadWriteStream() def on_activation(): self.has_found = True trigger_level = self.config.get('trigger_level', 3) sensitivity = self.config.get('sensitivity', 0.5) self.runner = PreciseRunner( PreciseEngine(precise_exe, self.precise_model), trigger_level, sensitivity, stream=self.stream, on_activation=on_activation, ) self.runner.start()
#!/usr/bin/env python from precise_runner import PreciseEngine, PreciseRunner engine = PreciseEngine('.venv/bin/precise-engine', 'my_model_file.pb') runner = PreciseRunner(engine, on_activation=lambda: print('hello')) runner.start()
time.sleep(3) print('hotword detection restart') ros.pub_hotword.publish(False) def start(): print('hotword node start') while not rospy.is_shutdown(): pass print('hotword node stop') if __name__ == "__main__": #ros ros = HotWordRos() rospy.init_node('Hotword', anonymous=True) rate = rospy.Rate(10) # 10hz # engine = PreciseEngine('/home/rastech/catkin_ws/src/venv/bin/precise-engine', # '/home/rastech/catkin_ws/src/fero_speaker/script/precise-data-models/new_ifero_191021.pb') engine = PreciseEngine(precise_engine, wake_word_model) runner = PreciseRunner(engine, sensitivity=0.5, on_activation=lambda: detectd()) runner.start() try: start() except rospy.ROSInterruptException: pass
def wake(): global wake_decoder if request.method == 'POST': system = request.form['system'] if system == 'pocketsphinx': # Pocketsphinx wake_statuses[system] = 'recording' if wake_decoder is None: ps_config = config['pocketsphinx'] training_cfg = config['training'] # Listen with the default acoustic model and mixed dictionary hmm = ps_config['acoustic_model'] dic = ps_config['dictionary'] keyphrase = request.form['keyphrase'] kws_threshold = float(ps_config['kws_threshold']) def detect(): from pocketsphinx import Pocketsphinx, Ad ad = Ad(None, 16000) # default input decoder = Pocketsphinx(lm=False, hmm=hmm, dic=dic, keyphrase=keyphrase, kws_threshold=kws_threshold) buf = bytearray(2048) with ad: with decoder.start_utterance(): while ad.readinto(buf) >= 0: decoder.process_raw(buf, False, False) if decoder.hyp(): with decoder.end_utterance(): logging.info('Wake word detected for %s' % system) wake_statuses[system] = 'detected' break # Run detection in sepearate thread thread = threading.Thread(target=detect, daemon=True) thread.start() logging.debug('Listening for %s with %s' % (keyphrase, system)) return system elif system == 'snowboy': # Snowboy snowboy_cfg = config['snowboy'] model_path = request.form['model'] sensitivity = float(snowboy_cfg['sensitivity']) audio_gain = float(snowboy_cfg['audio_gain']) from snowboy import snowboydecoder detector = snowboydecoder.HotwordDetector( model_path, sensitivity=sensitivity, audio_gain=audio_gain) def stop(): wake_statuses[system] = 'detected' detector.terminate() def detect(): detector.start(stop) # Run detection in sepearate thread thread = threading.Thread(target=detect, daemon=True) thread.start() logging.debug('Listening for %s with %s' % (os.path.basename(model_path), system)) return system elif system == 'precise': # Mycroft Precise precise_cfg = config['mycroft_precise'] model_path = request.form['model'] from precise_runner import PreciseEngine, PreciseRunner engine = PreciseEngine('precise-engine', model_path) def detect(): event = threading.Event() runner = PreciseRunner(engine, on_activation=lambda: event.set()) runner.start() event.wait() wake_statuses[system] = 'detected' runner.stop() thread = threading.Thread(target=detect, daemon=True) thread.start() logging.debug('Listening for %s with %s' % (os.path.basename(model_path), system)) return system else: return make_response('Unknown system: %s' % system, 500) return render_template('wake-word.html', current_page='wake-word', config=config, basename=os.path.basename)
#!/usr/bin/env python3 from precise_runner import PreciseEngine, PreciseRunner engine = PreciseEngine('precise-engine', 'jao-sandy.pb') runner = PreciseRunner(engine, on_activation=lambda: print('hello')) runner.start()
#!/usr/bin/env python3 import socket from threading import Thread from precise.network_runner import Listener from precise_runner import PreciseRunner, ReadWriteStream, PreciseEngine from precise_runner.runner import ListenerEngine ADDRESS = ('localhost', 10000) MODEL_NAME = 'my-model.pb.or.net' CHUNK_SIZE = 2048 stream = ReadWriteStream() runner = PreciseRunner( PreciseEngine('.venv/bin/precise-engine', MODEL_NAME), stream=stream, on_activation=lambda: print('Activated!') ) runner.start() class PreciseConnection: """Represents a socket connection routed to a precise process""" def __init__(self, connection, address): self.address = address self.connection = connection # type: socket.socket self.stream = ReadWriteStream() self.runner = PreciseRunner( ListenerEngine(Listener(MODEL_NAME, CHUNK_SIZE), CHUNK_SIZE), 1, stream=self.stream, on_activation=self.on_activation,
from time import sleep from precise_runner import PreciseEngine, PreciseRunner engine = PreciseEngine('precise-engine/precise-engine', 'geektime.pb') runner = PreciseRunner(engine, on_activation=lambda: print('hello')) runner.start() # Sleep forever while True: sleep(10)
#!/usr/bin/env python3 from precise_runner import PreciseEngine, PreciseRunner engine = PreciseEngine('/mycroft-precise/.venv/bin/precise-engine', '/mycroft-precise/marvin.pb') runner = PreciseRunner(engine, on_activation=lambda: print('hello')) runner.start() # Sleep forever from time import sleep while True: sleep(10)
from precise_runner import PreciseEngine, PreciseRunner from time import sleep print('Initializing...') def on_act(): print('wake word detected') def on_pred(x): x = 1 # initiate precise engine with mycroft model engine = PreciseEngine('precise-engine', 'hey-mycroft.tflite') # initiate precise runner that will listen, predict, and detect wakeword runner = PreciseRunner(engine, on_prediction=on_pred, on_activation=on_act) # start runner runner.start() # keep main thread active until user interrupt try: while 1: print('listening...') sleep(60) except: runner.stop()
import socket import threading from threading import Thread #threading.TIMEOUT_MAX = 4294967.0 #threading.TIMEOUT_MAX = 4294967.0 from precise.network_runner import Listener from precise_runner import PreciseRunner, ReadWriteStream, PreciseEngine from precise_runner.runner import ListenerEngine ADDRESS = ('localhost', 10000) MODEL_NAME = 'jao-sandy.pb' CHUNK_SIZE = 2048 stream = ReadWriteStream() runner = PreciseRunner(PreciseEngine('precise-engine', MODEL_NAME), stream=stream, on_activation=lambda: print('Activated!')) runner.start() class PreciseConnection: """Represents a socket connection routed to a precise process""" def __init__(self, connection, address): self.address = address self.connection = connection # type: socket.socket self.stream = ReadWriteStream() self.runner = PreciseRunner(ListenerEngine( Listener(MODEL_NAME, CHUNK_SIZE), CHUNK_SIZE), 1, stream=self.stream,
#!/usr/bin/env python3 from precise_runner import PreciseEngine, PreciseRunner engine = PreciseEngine('.venv/bin/precise-engine', 'hey-mycroft-2.pb') runner = PreciseRunner(engine, on_activation=lambda: print('hello')) runner.start()
from precise_runner import PreciseRunner, PreciseEngine from threading import Event def on_prediction(prob): print('!' if prob > 0.5 else '.', end='', flush=True) def on_activation(): print("ok") engine = PreciseEngine('C:\\Users\\TC\\Anaconda3\\Scripts\\precise-engine.exe', 'jao-sandy.pb') PreciseRunner(engine, on_prediction=on_prediction, on_activation=on_activation, trigger_level=0).start() Event().wait() # Wait forever
def __init__(self, decoder_model, sensitivity=[], audio_gain=1.0): def audio_callback(in_data, frame_count, time_info, status): self.ring_buffer.extend(in_data) play_data = chr(0) * len(in_data) return play_data, pyaudio.paContinue if type(decoder_model) is not list: decoder_model = [decoder_model] if type(sensitivity) is not list: sensitivity = [sensitivity] model_str = ",".join(decoder_model) try: self.detector = snowboydetect.SnowboyDetect( resource_filename=str( settings.LISTEN_SNOWBOY_RESOURCE).encode(), model_str=model_str.encode()) self.detector.SetAudioGain(audio_gain) self.num_hotwords = self.detector.NumHotwords() self.num_channels = self.detector.NumChannels() self.sample_rate = self.detector.SampleRate() if len(decoder_model) > 1 and len(sensitivity) == 1: sensitivity = sensitivity * self.num_hotwords if len(sensitivity) != 0: assert self.num_hotwords == len( sensitivity ), "number of hotwords in decoder_model (%d) and sensitivity " + "(%d) does not match" % ( self.num_hotwords, len(sensitivity)) sensitivity_str = ",".join([str(t) for t in sensitivity]) if len(sensitivity) != 0: self.detector.SetSensitivity(sensitivity_str.encode()) except Exception as e: print("Error while loading Snowboy: ", e) self.num_channels = 1 self.sample_rate = settings.LISTEN_SAMPLERATE self.num_hotwords = len(settings.LISTEN_SNOWBOY_MODELS) self.chunksize = settings.LISTEN_CHUNKSIZE self.seconds_per_buffer = float(self.chunksize) / self.sample_rate try: self.precise_chunk = settings.LISTEN_PRECISE_CHUNKSIZE print("Starting precise engine: ", self.precise_chunk) self.precise_engine = PreciseEngine(settings.LISTEN_PRECISE_BINARY, settings.LISTEN_PRECISE_MODEL, chunk_size=self.precise_chunk) self.precise = MyPreciseRunner(self.precise_engine, on_prediction=None, on_activation=None, trigger_level=3, sensitivity=0.5) except Exception as e: self.precise = None self.precise_engine = None print(e) if not settings.isDebug(): raise self.audio_format = pyaudio.paInt16 self.ring_buffer = RingBuffer(self.num_channels * self.sample_rate * 5) self.pythonaudio = pyaudio.PyAudio() self.frames = [] self.infos = [] self.sphinxrecognizer = mysphinx.MyRecognizer() #sr.Recognizer() try: self.sphinxrecognizer.prepare_sphinx2( language="en-GIT", keyword_entries=settings.LISTEN_SPHINX_KEYWORDS) except: pass for mic in enumerate(sr.Microphone.list_microphone_names()): print(mic) default_info = self.pythonaudio.get_default_input_device_info() print("Default microphone info ", default_info) if settings.LISTEN_MIC_INDEX is None: settings.LISTEN_MIC_INDEX = default_info['index'] self.sample_width = pyaudio.get_sample_size(self.audio_format) self.energy_threshold = settings.LISTEN_ENERGY_THRESHOLD self.stream_in = self.pythonaudio.open( input=True, output=False, input_device_index=settings.LISTEN_MIC_INDEX, format=self.audio_format, channels=self.num_channels, rate=self.sample_rate, frames_per_buffer=self.chunksize, stream_callback=audio_callback) print("Class settings:") print(self.__dict__)