如何仅获取可识别的文本?

2024-05-20 21:01:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Azure语言用python发短信

import azure.cognitiveservices.speech as speechsdk



var = lambda evt: print('ss: {}'.format(evt))
speech_recognizer.recognizing.connect(var)

然后,在尝试获取实际识别器文本的结果之后,它以以下内容结束:

ss: SpeechRecognitionEventArgs(session_id=0aea5e8b80e544b48414f2d27585b6c4, result=SpeechRecognitionResult(result_id=86c7de30436f4db1b064121bd617f24b, text="Hello.", reason=ResultReason.RecognizedSpeech))

我只想打印你好?你知道吗


Tags: lambdaimport语言idvarasserviceresult
1条回答
网友
1楼 · 发布于 2024-05-20 21:01:08

如果您使用简单的麦克风来识别文本,您可以使用以下内容来获取文本:

def speech_recognize_once_from_mic():
    """performs one-shot speech recognition from the default microphone"""
    # <SpeechRecognitionWithMicrophone>
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    # Creates a speech recognizer using microphone as audio input.
    # The default language is "en-us".
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)

    # Starts speech recognition, and returns after a single utterance is recognized. The end of a
    # single utterance is determined by listening for silence at the end or until a maximum of 15
    # seconds of audio is processed. It returns the recognition text as result.
    # Note: Since recognize_once() returns only a single utterance, it is suitable only for single
    # shot recognition like command or query.
    # For long-running multi-utterance recognition, use start_continuous_recognition() instead.
    result = speech_recognizer.recognize_once()

    # Check the result
    if result.reason == speechsdk.ResultReason.RecognizedSpeech:
        print("Recognized: {}".format(result.text))
    elif result.reason == speechsdk.ResultReason.NoMatch:
        print("No speech could be recognized")
    elif result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = result.cancellation_details
        print("Speech Recognition canceled: {}".format(cancellation_details.reason))
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print("Error details: {}".format(cancellation_details.error_details))
    # </SpeechRecognitionWithMicrophone>

请查看this回购以供进一步参考。希望有帮助。你知道吗

相关问题 更多 >