|
@@ -1,107 +0,0 @@
|
|
|
-import logging
|
|
|
|
|
-import time
|
|
|
|
|
-import speech_recognition as sr
|
|
|
|
|
-import output
|
|
|
|
|
-
|
|
|
|
|
-def _get_recognizer(pause_threshold):
|
|
|
|
|
- recognizer = sr.Recognizer()
|
|
|
|
|
- recognizer.pause_threshold = pause_threshold
|
|
|
|
|
- recognizer.dynamic_energy_threshold = False
|
|
|
|
|
- #recognizer.energy_threshold = 500
|
|
|
|
|
- recognizer.phrase_threshold = 0.15
|
|
|
|
|
-
|
|
|
|
|
- with sr.Microphone() as source:
|
|
|
|
|
- recognizer.adjust_for_ambient_noise(source)
|
|
|
|
|
-
|
|
|
|
|
- return recognizer
|
|
|
|
|
-
|
|
|
|
|
-class TextCollector(object):
|
|
|
|
|
- def __init__(self, terminators):
|
|
|
|
|
- self.res = []
|
|
|
|
|
- self.done = None
|
|
|
|
|
- self.terminators = [t.lower() for t in terminators]
|
|
|
|
|
-
|
|
|
|
|
- def call(self, text_or_tokens):
|
|
|
|
|
- for t in self.terminators:
|
|
|
|
|
- if isinstance(text_or_tokens, list) and text_or_tokens[-1].lower() == t:
|
|
|
|
|
- self.done = t
|
|
|
|
|
- text_or_tokens = text_or_tokens[:-1]
|
|
|
|
|
- break
|
|
|
|
|
-
|
|
|
|
|
- elif isinstance(text_or_tokens, basestring) and text_or_tokens.lower().endswith(t):
|
|
|
|
|
- self.done = t
|
|
|
|
|
- text_or_tokens = text_or_tokens[:-len(t)].strip()
|
|
|
|
|
- break
|
|
|
|
|
-
|
|
|
|
|
- if text_or_tokens:
|
|
|
|
|
- self.res.append(text_or_tokens)
|
|
|
|
|
-
|
|
|
|
|
-def listen_until_terminated(terminators=['done', 'cancel'], timeout=300, pause_threshold=5, tokenize=False, return_all=False, language='en-GB', preferred_phrases=None):
|
|
|
|
|
- if preferred_phrases is None:
|
|
|
|
|
- preferred_phrases = terminators
|
|
|
|
|
- else:
|
|
|
|
|
- preferred_phrases.extend(terminators)
|
|
|
|
|
-
|
|
|
|
|
- c = TextCollector(terminators)
|
|
|
|
|
-
|
|
|
|
|
- stopper = _listen_in_background(c.call, pause_threshold=pause_threshold, tokenize=tokenize, return_all=return_all, language=language, preferred_phrases=preferred_phrases)
|
|
|
|
|
-
|
|
|
|
|
- start_t = time.time()
|
|
|
|
|
- while not c.done and time.time() < start_t + timeout:
|
|
|
|
|
- time.sleep(0.1)
|
|
|
|
|
-
|
|
|
|
|
- stopper()
|
|
|
|
|
-
|
|
|
|
|
- return c.res, c.done
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def _listen_in_background(callback, pause_threshold=5, tokenize=False, return_all=False, language='en-GB', preferred_phrases=None):
|
|
|
|
|
- source = sr.Microphone()
|
|
|
|
|
- recognizer = _get_recognizer(pause_threshold)
|
|
|
|
|
-
|
|
|
|
|
- def _background_callback(recognizer, audio):
|
|
|
|
|
- text = _speech_to_text(audio, recognizer, tokenize, return_all, language, preferred_phrases)
|
|
|
|
|
- if text is not None:
|
|
|
|
|
- callback(text)
|
|
|
|
|
-
|
|
|
|
|
- return recognizer.listen_in_background(source, _background_callback)
|
|
|
|
|
-
|
|
|
|
|
-def _speech_to_text(audio, recognizer, tokenize, return_all, language, preferred_phrases):
|
|
|
|
|
- res = None
|
|
|
|
|
- logging.info('Calling recognition APIs (preferred_phrases: %s)...', preferred_phrases)
|
|
|
|
|
- try:
|
|
|
|
|
- t = time.time()
|
|
|
|
|
- res = recognizer.recognize_google_cloud(audio, language=language, show_all=return_all, preferred_phrases=preferred_phrases)
|
|
|
|
|
- logging.info('Google cloud speech: %s (%.2f s)', res, time.time() - t)
|
|
|
|
|
- except sr.UnknownValueError:
|
|
|
|
|
- logging.error('Google Cloud Speech Recognition could not understand audio')
|
|
|
|
|
- except sr.RequestError, e:
|
|
|
|
|
- logging.error("Could not request results from Google Cloud Speech Recognition service: %s", e)
|
|
|
|
|
-
|
|
|
|
|
- try:
|
|
|
|
|
- t = time.time()
|
|
|
|
|
- alt = recognizer.recognize_google(audio, language=language, show_all=return_all)
|
|
|
|
|
- res = res or alt
|
|
|
|
|
- logging.info('Google speech: %s (%.2f s)', res, time.time() - t)
|
|
|
|
|
- except sr.UnknownValueError:
|
|
|
|
|
- logging.error('Google Speech Recognition could not understand audio')
|
|
|
|
|
- except sr.RequestError, e:
|
|
|
|
|
- logging.error("Could not request results from Google Speech Recognition service: %s", e)
|
|
|
|
|
-
|
|
|
|
|
- if res and res.strip():
|
|
|
|
|
- output.success()
|
|
|
|
|
- return [t.strip() for t in res.split(' ') if t.strip()] if tokenize else res.strip()
|
|
|
|
|
-
|
|
|
|
|
- else:
|
|
|
|
|
- output.failure()
|
|
|
|
|
- return None
|
|
|
|
|
-
|
|
|
|
|
-def listen(pause_threshold=5, tokenize=False, return_all=False, language='en-GB', preferred_phrases=None):
|
|
|
|
|
- # obtain audio from the microphone
|
|
|
|
|
- recognizer = _get_recognizer(pause_threshold)
|
|
|
|
|
-
|
|
|
|
|
- with sr.Microphone() as source:
|
|
|
|
|
- logging.info('listening...')
|
|
|
|
|
- audio = recognizer.listen(source)
|
|
|
|
|
-
|
|
|
|
|
- return _speech_to_text(audio, recognizer, tokenize, return_all, language, preferred_phrases)
|
|
|