微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 MsAzure 快速入门示例代码将大型 .wav 文件转换为 .txt 文件【持续识别】

如何解决使用 MsAzure 快速入门示例代码将大型 .wav 文件转换为 .txt 文件【持续识别】

正如 Stack Overflow 中所建议的,我尝试使用 MsAzure Speech 转文本选项的快速入门中的示例代码将大型 .wav 文件转换为准确的 .txt 文件。它给了我错误

我写的代码

audio_config = speechsdk.audio.AudioConfig(filename="FilePath//Audio.wav")
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config,audio_config=audio_config)

done = False

def stop_cb(evt):
    print('CLOSING on {}'.format(evt))
    speech_recognizer.stop_continuous_recognition()
    nonlocal done
    done = True

(Here I got an error 
File "<ipython-input-10-1ca5a9053d2a>",line 5
    nonlocal done
    ^
SyntaxError: no binding for nonlocal 'done' found


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)))

speech_recognizer.session_stopped.connect(stop_cb)
speech_recognizer.canceled.connect(stop_cb)

import time
speech_recognizer.start_continuous_recognition()
while not done:
    time.sleep(.5)

有人可以建议我纠正我出错的地方吗?如果任何机构可以在 MsAzure 上建议任何不同的方法将大型 .wav 文件转换为 .txt,我也将不胜感激。

解决方法

使用 global 解决您的问题。

done = False

def stop_cb(evt):
    print('CLOSING on {}'.format(evt))
    speech_recognizer.stop_continuous_recognition()
    global done
    done = True

nonlocal 关键字用于处理嵌套函数内部的变量,其中变量不应属于内部函数。

在您的情况下,您没有外部函数/嵌套函数。因此,您必须使用 global 关键字。

nonlocal 的正确用法如下:

def myfunc1():
  x= "test string"
  def myfunc2():
    nonlocal x
    x = "test string updated"

说明:

这里 myfunc1myfunc2 是嵌套的。因此,您可以使用 nonlocal x 引用外部作用域函数 myfunc1

的变量 x

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。