正如Stack Overflow中建议的那样,我尝试通过使用MsAzure Speech to text选项中的快速开始示例代码将大型。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"
解释:
这里的myfunc1和myfunc2是嵌套的。所以你可以用nonlocal x
来指代外部作用域函数myfunc1的变量x