【发布时间】:2019-06-09 06:28:34
【问题描述】:
因此,在我进行编程时,我创建了一个变量 (rYouTube),用于存储识别器。我创建了另一个名为 rGoogle 的变量,其中存储了另一个识别器。唯一的问题是,每次我选择 Google 而不是 YouTube 时,我都会不断收到错误消息“UnboundLocalError: local variable 'rYouTube' referenced before assignment”,因为我的程序的工作方式是你选择一个然后程序继续运行(如果你选择了 YouTube ,你可以看看东西,如果你选择谷歌,你可以查东西)
所以我已经尝试将变量值作为占位符提供,但由于这些变量是音频变量,它不起作用。
print("Would you like to Direct Search?")
rYouTube = sr.Recognizer()
with sr.Microphone() as source:
rYouTube.adjust_for_ambient_noise(source)
YTaudio = rYouTube.listen(source)
print("LOADING...")
time.sleep(1)
try:
DirectYTRecognized = rYouTube.recognize_google(YTaudio)
print(DirectYTRecognized)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
if "yes" in DirectYTRecognized:
print("What do you want to watch?")
SearchYouTube = sr.Recognizer()
with sr.Microphone() as source:
SearchYouTube.adjust_for_ambient_noise(source)
YTSearchAudio = SearchYouTube.listen(source)
print("LOADING...")
time.sleep(1)
try:
FinalSearchYTAudio = SearchYouTube.recognize_google(YTSearchAudio)
print(FinalSearchYTAudio)
DirectYT = "https://youtube.com/results?search_query=" + FinalSearchYTAudio
webbrowser.open_new(DirectYT)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
#END OF YT DIRECT SEARCH-------------------------------------
#GOOGLE DIRECT SEARCH---------------------------------------
if "Google" in recognized:
print("Would you like to Direct Search?")
rGoogle = sr.Recognizer()
with sr.Microphone() as source:
rGoogle.adjust_for_ambient_noise(source)
GoogleAudio = rGoogle.listen(source)
print("LOADING...")
time.sleep(1)
try:
DirectGoogleRecognized = rGoogle.recognize_google(GoogleAudio)
print(DirectGoogleRecognized)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
if "yes" in DirectGoogleRecognized:
print("What do you want to look up?")
SearchGoogle = sr.Recognizer()
with sr.Microphone() as source:
SearchGoogle.adjust_for_ambient_noise(source)
GoogleSearchAudio = SearchGoogle.listen(source)
print("LOADING...")
time.sleep(1)
try:
FinalSearchGooleAudio = SearchGoogle.recognize_google(YTSearchAudio)
print(FinalSearchGoogleAudio)
DirectGoogle = "https://youtube.com/results?search_query=" + FinalSearchGoogleAudio
webbrowser.open_new(DirectGoogle)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
我希望程序继续工作,但它停止并显示:“UnboundLocalError: local variable 'rYouTube' referenced before assignment”
【问题讨论】:
标签: python variables speech-recognition