我正在尝试实现一个打开语音识别链接的代码。例如,如果我说";谷歌;它会转到特定的分支,计算机会要求我指定打开google.com应该遵循的链接?[代码][1][1] :https://i.stack.imgur.com/B9Nrg.png每次我想说要遵循的链接时,我都必须用蓝色形状重写代码。
import pyttsx3
import speech_recognition as sr
import webbrowser
speech_engine = sr.Recognizer()
speech_engine.energy_threshold = 300
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
#open web pages
with sr.Microphone() as micro:
print("Recording...")
speak("Tell me what page do you need me to open")
audio = speech_engine.record(micro, duration=5)
print("Recognizing...")
text = speech_engine.recognize_google(audio, language="en")
print(text)
if text == 'ok':
speak("what the fuck")
elif (text == 'open Amazon' or text =="Amazon"):
webbrowser.open("https://amazon.com")
print("opening Amazon")
elif (text == 'open Wikipedia' or text == "No"):
with sr.Microphone() as micro:
speak("Where do you need me to open it from?")
audioo = speech_engine.record(micro, duration=15)
print("Wait")
text = speech_engine.recognize_google(audioo, language="en")
print(text)
webbrowser.open(text)
print("opening Wikipedia")
elif (text == 'open Google' or text == 'Google'):
with sr.Microphone() as micro:
speak("Where do you need me to open it from?")
audioo = speech_engine.record(micro, duration=15)
print("Wait")
text = speech_engine.recognize_google(audioo, language="en")
print(text)
webbrowser.open(text)
#webbrowser.open("https://google.com")
print("I've opened google for you")
elif (text == 'open Moodle' or text =="Moodle"):
webbrowser.open("https://edu.tuiasi.ro")
print("opening Moodle")
elif (text == 'open Gmail' or text =="Gmail" or text == "Google mail" or text =="Mail" or text =="mail"):
with sr.Microphone() as micro:
speak("Where do you need me to open it from?")
audioo = speech_engine.record(micro, duration=15)
print("Wait")
text = speech_engine.recognize_google(audioo, language="en")
print(text)
webbrowser.open(text)
# webbrowser.open("www.gmail.com")
print("opening Gmail")
else:
print("Unrecognized Command")
这个代码工作正常,但我想要更好的东西,以便不使用
with sr.Microphone() as micro:
speak("Where do you need me to open it from?")
audioo = speech_engine.record(micro, duration=15)
print("Wait")
text = speech_engine.recognize_google(audioo, language="en")
print(text)
webbrowser.open(text)
每当我想";"通信";使用电脑。