Поэтому, когда я программирую, я создал переменную (rYouTube), в которой я храню Recognizer. Я создаю другую переменную с именем rGoogle, в которой я храню другой Recognizer. Единственная проблема заключается в том, что я продолжаю получать сообщение об ошибке «UnboundLocalError: локальная переменная« ссылка на rYouTube »перед назначением» каждый раз, когда я выбираю Google вместо YouTube, потому что моя программа работает так, как вы выбираете одно, и программа продолжается (если вы выбрали YouTube , вы можете смотреть вещи смотреть, если вы выбрали Google, вы можете искать вещи)
Итак, я уже пытался указать значения переменных в качестве заполнителей, но, поскольку эти переменные являются аудио переменными, он не работает.
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: локальная переменная 'rYouTube', на которую ссылаются перед присваиванием"