def on_enter(self, payload=None):
""" Executed on the entry to the Recognizing State. Upon entry, audio is captured from the Microphone and
recognition with preferred speech recognition engine is done. If successful, the machine transitions to Busy
State. On failure, it transitions to Error state.
:param payload: No payload is expected by this state
:return: None
"""
self.notify_renderer('listening')
recognizer = self.components.recognizer
try:
print("Say something!")
with self.components.microphone as source:
audio = recognizer.listen(source, phrase_time_limit=5)
self.notify_renderer('recognizing')
print("Got it! Now to recognize it...")
try:
value = self.__recognize_audio(
audio=audio, recognizer=recognizer)
print(value)
self.notify_renderer('recognized', value)
self.transition(self.allowedStateTransitions.get(
'busy'), payload=value)
except sr.UnknownValueError:
print("Oops! Didn't catch that")
self.transition(self.allowedStateTransitions.get(
'error'), payload='RecognitionError')
except sr.RequestError as e:
print(
"Uh oh! Couldn't request results from Speech Recognition service; {0}".format(e))
self.transition(self.allowedStateTransitions.get(
'error'), payload='ConnectionError')
except KeyboardInterrupt:
pass
评论列表
文章目录