def test_cancel_on_recognize(): asr = SpeechRecognizer(url, **asr_kwargs) asr.recognize(DelayedFileAudioSource(phone_wav), LanguageModelList(phone_grammar_uri)) time.sleep(2) asr.cancel_recognition() result = asr.wait_recognition_result() assert len(result) == 0
def test_cancel_without_recognize(): asr = SpeechRecognizer(url, **asr_kwargs) try: asr.cancel_recognition() except RecognitionException as e: assert e.code == "FAILURE" else: assert False
LanguageModelList.grammar_from_path(os.path.basename(argv[2]), argv[2])) else: lm = LanguageModelList(LanguageModelList.from_uri(argv[2])) credentials = ("", "") if argc == 5: credentials = (argv[3], argv[4]) asr = SpeechRecognizer(url, credentials=credentials, log_stream=ostream, listener=PrinterListener(), log_level="warning") with MicAudioSource() as mic: try: asr.recognize(mic, lm) result = asr.wait_recognition_result() if result: if result[0].resultCode == "RECOGNIZED": print(result[0].alternatives[0]['text']) else: print(result[0].resultCode) except KeyboardInterrupt: print("Caught interrupt. Closing ASR instance...", file=stderr) try: asr.cancel_recognition() except RecognitionException: pass # Ignores exceptions on canceling asr.close()