Beispiel #1
0
def test_basic_slm():
    asr = SpeechRecognizer(url, **asr_kwargs)
    asr.recognize(FileAudioSource(phone_wav), LanguageModelList(slm))
    result = asr.wait_recognition_result()[0]
    asr.close()
    assert len(result.alternatives[0]['text']) > 0
    assert int(result.alternatives[0]['score']) > 90
Beispiel #2
0
def test_no_match():
    asr = SpeechRecognizer(url, **asr_kwargs)
    asr.recognize(FileAudioSource(silence_wav), LanguageModelList(slm))
    result = asr.wait_recognition_result()[0]
    asr.close()
    # NO_SPEECH occurs with enabled endpointer, and NO_MATCH with disabled
    assert result.result_code in ("NO_SPEECH", "NO_MATCH"), \
        "Result code is {}".format(result.resultCode)
Beispiel #3
0
def test_wait_recognition_result_duplicate():
    asr = SpeechRecognizer(url, **asr_kwargs)
    asr.recognize(FileAudioSource(phone_wav),
                  LanguageModelList(phone_grammar_uri))
    result = asr.wait_recognition_result()[0].alternatives[0]
    result_empty = asr.wait_recognition_result()
    asr.close()
    assert len(result['text']) > 0
    assert len(result['interpretations']) > 0
    assert int(result['score']) > 90
    assert len(result_empty) == 0
Beispiel #4
0
def test_basic_grammar():
    asr = SpeechRecognizer(url, **asr_kwargs)
    asr.recognize(FileAudioSource(phone_wav),
                  LanguageModelList(phone_grammar_uri))
    result = asr.wait_recognition_result()
    asr.close()
    alt = None
    for res in result:
        if len(res.alternatives) > 0:
            alt = res.alternatives[0]
            break
    assert alt is not None
    assert len(alt['text']) > 0
    assert len(alt['interpretations']) > 0
    assert int(alt['score']) > 90
Beispiel #5
0
def test_inline_grammar():
    with open(yes_grammar_path) as f:
        body = f.read()
    asr = SpeechRecognizer(url, **asr_kwargs)
    asr.recognize(FileAudioSource(yes_wav),
                  LanguageModelList(("yes_no", body)))
    result = asr.wait_recognition_result()
    asr.close()
    alt = None
    for res in result:
        if len(res.alternatives) > 0:
            alt = res.alternatives[0]
            break
    assert alt is not None
    assert len(alt['text']) > 0
    assert len(alt['interpretations']) > 0
    assert int(alt['score']) > 90
Beispiel #6
0
def recognize_worker(url, asr_kwargs, wav_path, lm_list, recognitions,
                     executions, assertion):
    error_msg = ("Parameters: url:{}, wav_path:{}, lm_list:{}".format(
        url, wav_path, lm_list))
    error_msg += "\nOn execution {}/{}\nOn recognition {}/{}"
    error_msg += "\nError from assertion: {}"
    for e in range(executions):
        beg = time()
        asr = SpeechRecognizer(url, **asr_kwargs)
        for r in range(recognitions):
            asr.recognize(FileAudioSource(wav_path), lm_list)
            success, msg = assertion(asr.wait_recognition_result())
            if not success:
                error_msg = error_msg.format(executions, e, recognitions, r,
                                             msg)
                assert success, error_msg
        asr.close()
        asr._logger.info("[TIMER] TotalTime: {} s".format(time() - beg))
Beispiel #7
0
def test_equivalence_file_buffer():

    # File
    asr = SpeechRecognizer(url, **asr_kwargs)
    asr.recognize(FileAudioSource(phone_wav),
                  LanguageModelList(slm))
    result_file = asr.wait_recognition_result()[0].alternatives[0]['text']
    asr.close()

    # Buffer
    asr = SpeechRecognizer(url, **asr_kwargs)
    source = BufferAudioSource()
    asr.recognize(source,
                  LanguageModelList(slm))
    sig, rate = sf.read(phone_wav)
    source.write((sig * 2**15).astype('int16').tobytes())
    source.finish()
    result_buffer = asr.wait_recognition_result()[0].alternatives[0]['text']
    asr.close()

    assert result_file == result_buffer
Beispiel #8
0
if __name__ == "__main__":
    ostream = open('log.txt', 'a')
    argc = len(argv)
    if argc != 4 and argc != 6:
        usage()

    url = argv[1]
    if os.path.isfile(argv[2]):
        lm = LanguageModelList(
            LanguageModelList.grammar_from_path('asdasdas', argv[2]))
    else:
        lm = LanguageModelList(LanguageModelList.from_uri(argv[2]))
    apath = argv[3]
    credentials = ("", "")
    if argc == 6:
        credentials = (argv[4], argv[5])

    asr = SpeechRecognizer(url,
                           credentials=credentials,
                           log_stream=ostream,
                           log_level="debug",
                           max_wait_seconds=600)
    asr.recognize(FileAudioSource(apath), lm)
    res = asr.wait_recognition_result()
    if res:
        for k in res:
            print(k.alternatives)
    else:
        print("Empty result! Check log.txt for more info.")
    asr.close()