This repository has been archived by the owner on May 17, 2022. It is now read-only.
forked from MycroftAI/mycroft-core
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'hotword_factory' into patch-15
Conflicts: dev_setup.sh mycroft/client/speech/listener.py mycroft/client/speech/mic.py mycroft/client/speech/recognizer/pocketsphinx_recognizer.py mycroft/configuration/mycroft.conf mycroft/skills/padatious_service.py requirements.txt test/unittests/client/audio_consumer_test.py test/unittests/client/local_recognizer_test.py
- Loading branch information
Showing
15 changed files
with
206 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ logs/* | |
mycroft/audio-accuracy-test/data/* | ||
scripts/*.screen | ||
doc/_build/ | ||
.installed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# Copyright 2017 Mycroft AI, Inc. | ||
# | ||
# This file is part of Mycroft Core. | ||
# | ||
# Mycroft Core is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# Mycroft Core is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from mycroft.configuration import ConfigurationManager | ||
from mycroft.util.log import getLogger | ||
from os.path import dirname, exists, join, abspath | ||
import os | ||
import time | ||
import tempfile | ||
|
||
__author__ = 'seanfitz, jdorleans, jarbas' | ||
|
||
LOG = getLogger("HotwordFactory") | ||
|
||
RECOGNIZER_DIR = join(abspath(dirname(__file__)), "recognizer") | ||
|
||
|
||
class HotWordEngine(object): | ||
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): | ||
self.lang = str(lang).lower() | ||
self.key_phrase = str(key_phrase).lower() | ||
# rough estimate 1 phoneme per 2 chars | ||
self.num_phonemes = len(key_phrase) / 2 + 1 | ||
if config is None: | ||
config = ConfigurationManager.get().get("hot_words", {}) | ||
config = config.get(self.key_phrase, {}) | ||
self.config = config | ||
self.listener_config = ConfigurationManager.get().get("listener", {}) | ||
|
||
def found_wake_word(self, frame_data): | ||
return False | ||
|
||
|
||
class PocketsphinxHotWord(HotWordEngine): | ||
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): | ||
super(PocketsphinxHotWord, self).__init__(key_phrase, config, lang) | ||
# Hotword module imports | ||
from pocketsphinx import Decoder | ||
# Hotword module config | ||
module = self.config.get("module") | ||
if module != "pocketsphinx": | ||
LOG.warning( | ||
str(module) + " module does not match with " | ||
"Hotword class pocketsphinx") | ||
# Hotword module params | ||
self.phonemes = self.config.get("phonemes", "HH EY . M AY K R AO F T") | ||
self.num_phonemes = len(self.phonemes.split()) | ||
self.threshold = self.config.get("threshold", 1e-90) | ||
self.sample_rate = self.listener_config.get("sample_rate", 1600) | ||
dict_name = self.create_dict(key_phrase, self.phonemes) | ||
config = self.create_config(dict_name, Decoder.default_config()) | ||
self.decoder = Decoder(config) | ||
|
||
def create_dict(self, key_phrase, phonemes): | ||
(fd, file_name) = tempfile.mkstemp() | ||
words = key_phrase.split() | ||
phoneme_groups = phonemes.split('.') | ||
with os.fdopen(fd, 'w') as f: | ||
for word, phoneme in zip(words, phoneme_groups): | ||
f.write(word + ' ' + phoneme + '\n') | ||
return file_name | ||
|
||
def create_config(self, dict_name, config): | ||
model_file = join(RECOGNIZER_DIR, 'model', self.lang, 'hmm') | ||
if not exists(model_file): | ||
LOG.error('PocketSphinx model not found at ' + str(model_file)) | ||
config.set_string('-hmm', model_file) | ||
config.set_string('-dict', dict_name) | ||
config.set_string('-keyphrase', self.key_phrase) | ||
config.set_float('-kws_threshold', float(self.threshold)) | ||
config.set_float('-samprate', self.sample_rate) | ||
config.set_int('-nfft', 2048) | ||
config.set_string('-logfn', '/dev/null') | ||
return config | ||
|
||
def transcribe(self, byte_data, metrics=None): | ||
start = time.time() | ||
self.decoder.start_utt() | ||
self.decoder.process_raw(byte_data, False, False) | ||
self.decoder.end_utt() | ||
if metrics: | ||
metrics.timer("mycroft.stt.local.time_s", time.time() - start) | ||
return self.decoder.hyp() | ||
|
||
def found_wake_word(self, frame_data): | ||
hyp = self.transcribe(frame_data) | ||
return hyp and self.key_phrase in hyp.hypstr.lower() | ||
|
||
|
||
class SnowboyHotWord(HotWordEngine): | ||
def __init__(self, key_phrase="hey mycroft", config=None, lang="en-us"): | ||
super(SnowboyHotWord, self).__init__(key_phrase, config, lang) | ||
# Hotword module imports | ||
from snowboydecoder import HotwordDetector | ||
# Hotword module config | ||
module = self.config.get("module") | ||
if module != "snowboy": | ||
LOG.warning(module + " module does not match with Hotword class " | ||
"snowboy") | ||
# Hotword params | ||
models = self.config.get("models", {}) | ||
paths = [] | ||
for key in models: | ||
paths.append(models[key]) | ||
sensitivity = self.config.get("sensitivity", 0.5) | ||
self.snowboy = HotwordDetector(paths, | ||
sensitivity=[sensitivity] * len(paths)) | ||
self.lang = str(lang).lower() | ||
self.key_phrase = str(key_phrase).lower() | ||
|
||
def found_wake_word(self, frame_data): | ||
wake_word = self.snowboy.detector.RunDetection(frame_data) | ||
return wake_word == 1 | ||
|
||
|
||
class HotWordFactory(object): | ||
CLASSES = { | ||
"pocketsphinx": PocketsphinxHotWord, | ||
"snowboy": SnowboyHotWord | ||
} | ||
|
||
@staticmethod | ||
def create_hotword(hotword="hey mycroft", config=None, lang="en-us"): | ||
LOG.info("creating " + hotword) | ||
if not config: | ||
config = ConfigurationManager.get().get("hotwords", {}) | ||
module = config.get(hotword).get("module", "pocketsphinx") | ||
config = config.get(hotword, {"module": module}) | ||
clazz = HotWordFactory.CLASSES.get(module) | ||
return clazz(hotword, config, lang=lang) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.