【问题标题】:Continuous speech recognition from microphone on MS Azure来自 MS Azure 上麦克风的连续语音识别
【发布时间】:2021-08-13 21:30:32
【问题描述】:

我想使用 Azure 语音服务从麦克风进行语音识别。我有一个程序在 Python 中使用识别一次异步()顺利运行,但它只识别第一个语音,但音频限制为 15 秒。我对此主题进行了一些研究,并查看了来自 MS (https://github.com/Azure-Samples/cognitive-services-speech-sdk/blob/master/samples/python/console/speech_sample.py) 的示例代码,但找不到任何可以通过麦克风进行连续语音识别的东西……有什么提示吗?

【问题讨论】:

    标签: python azure speech-recognition speech-to-text


    【解决方案1】:

    你可以试试下面的代码:

    import azure.cognitiveservices.speech as speechsdk
    import os
    import time
    
     
    path = os.getcwd()
    # Creates an instance of a speech config with specified subscription key and service region.
    # Replace with your own subscription key and region identifier from here: https://aka.ms/speech/sdkregion
    speech_key, service_region = "6.....9", "eastus"
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    
    # Creates a recognizer with the given settings
    speech_config.speech_recognition_language="en-US"
    #source_language_config = speechsdk.languageconfig.SourceLanguageConfig("en-US", "The Endpoint ID for your custom model.")
    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config)
    
    done = False 
    def stop_cb(evt):
        print('CLOSING on {}'.format(evt))
        speech_recognizer.stop_continuous_recognition()
        global done
        done= True
        
    
    #Connect callbacks to the events fired by the speech recognizer    
    speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
    speech_recognizer.recognized.connect(lambda evt: print('RECOGNIZED: {}'.format(evt)))
    speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
    speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED {}'.format(evt)))
    speech_recognizer.canceled.connect(lambda evt: print('CANCELED {}'.format(evt)))
    # stop continuous recognition on either session stopped or canceled events
    speech_recognizer.session_stopped.connect(stop_cb)
    speech_recognizer.canceled.connect(stop_cb)
    
    speech_recognizer.start_continuous_recognition()
    
    while not done:
        time.sleep(.5)
    

    说明: 默认情况下,当您不提供 audioconfig - 默认输入源是麦克风。

    如果您想配置/自定义 - 您可以使用 audioconfig

    在连续识别中,有各种事件回调,例如 - 识别、已识别、已取消。

    输出:

    【讨论】:

    • 如果有帮助,请考虑接受此解决方案 :) meta.stackexchange.com/questions/5234/…
    • 是否可以将最终结果存储在单独的变量中?现在它只识别部分句子... SpeechRecognitionEventArgs(session_id=6a1a614b24dd4f00994676c056be2b74, result=SpeechRecognitionResult(result_id=02199fb65d5c493a8edbe586c568c575, text="hi" text="hi i'm" text="hi i'm john"text="Hi ,我是约翰。”
    • RecognizedEvent - 将有一个完整的识别句子。将此附加到变量中。
    • 工作顺利,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2020-09-12
    • 2012-01-07
    • 1970-01-01
    • 2023-02-12
    • 2013-03-19
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多