【问题标题】:Speech to text using MsAzure Service Key使用 MsAzure 服务密钥的语音转文本
【发布时间】:2021-01-21 13:47:43
【问题描述】:

您好,我正在尝试使用 MsAzure Quick start GitHub Codes for Speech-to-text 将大型 (.wav) 音频文件转换为 .txt 文件。我已经粘贴了下面的代码。这些既不会产生结果,也不会产生任何错误消息。

def speech_recognize_continuous_from_file():
    """performs continuous speech recognition with input from an audio file"""
    # <SpeechContinuousRecognitionWithFile>
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    audio_config = speechsdk.audio.AudioConfig(filename=weatherfilename)

    speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

    done = False

    def stop_cb(evt):
        """callback that stops continuous recognition upon receiving an event `evt`"""
        print('CLOSING on {}'.format(evt))
        speech_recognizer.stop_continuous_recognition()
        nonlocal done
        done = True

    all_results = []
    def handle_final_result(evt):
        all_results.append(evt.result.text)

    speech_recognizer.recognized.connect(handle_final_result)
    # 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)

    # Start continuous speech recognition
    speech_recognizer.start_continuous_recognition()
    while not done:
        time.sleep(.5)

    print("Printing all results:")
    print(all_results)

任何机构都可以建议我哪里出错了。 O最近几天挣扎了很多,没有任何有效的结果。 如果有人能建议我如何下载文本文件并将其保存在所需的文件夹中,我也很感激。

谢谢

【问题讨论】:

    标签: python-3.x azure speech-recognition


    【解决方案1】:

    如果您想将结果内容写入 .txt 文件,请尝试以下测试代码:

    import azure.cognitiveservices.speech as speechsdk
    import time
    
    service_region = ''
    speech_key=''
    filename = 'D:/test.wav'
    text_file_path = 'D:/temp/result.txt'
    
    def speech_recognize_continuous_from_file():
        """performs continuous speech recognition with input from an audio file"""
        # <SpeechContinuousRecognitionWithFile>
        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
        audio_config = speechsdk.audio.AudioConfig(filename=filename)
    
        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
    
        done = False
    
        def stop_cb(evt):
            """callback that stops continuous recognition upon receiving an event `evt`"""
            print('CLOSING on {}'.format(evt))
            speech_recognizer.stop_continuous_recognition()
            nonlocal done
            done = True
    
        all_results = []
        def handle_final_result(evt):
            all_results.append(evt.result.text)
    
        speech_recognizer.recognized.connect(handle_final_result)
        # 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)
    
        # Start continuous speech recognition
        speech_recognizer.start_continuous_recognition()
        while not done:
            time.sleep(.5)
    
        print("writing result into txt file...")
    
        f = open(text_file_path, "w")
        for content in all_results:
            f.write(content)
        f.close()
        print("done.")
    
        print(all_results)
        
    
    speech_recognize_continuous_from_file()
    

    我将代码保存到单独的 .py 文件并直接运行,结果:

    【讨论】:

    • 谢谢。我从 .ipynb 文件中得到了类似的结果。您能否建议如何获得清晰易读的通用文本文件。
    • 所以你想将结果内容写入.txt文件?如果是这样,我已经更新了我的答案,如果对你有帮助,请接受。
    • 是的,它奏效了。文本质量准确率为 95%。有什么办法可以提高文字质量吗?此外,文本文件分布在无限的景观中。有什么方法可以自定义纸张宽度并增加长度而不是宽度太宽。
    • @SALILRAY,是的,您可以使用自定义语音来改进您的语音转文本服务:docs.microsoft.com/en-us/azure/cognitive-services/…
    • 非常感谢您对像我这样的新学习者的一贯支持。我打开链接并按照描述的顺序按照说明进行操作。我尝试通过上传我的音频文件(.wav 格式)在一个新项目中做到这一点。我将数据存储在我的 OneDrive 中。它说无效数据。无法完成上传。此外,自定义语音是否有任何可用的 Python 代码块?如果是这样,您可以提供该链接吗?谢谢
    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多