I'm trying to implement a code that opens links from speech recognition. How can i write easily that if for example I said "google" it will go on specific branch and computer will ask me to dictate the link that it should follow to open google.com? [code][1] [1]: https://i.stack.imgur.com/B9Nrg.png I have to rewrite the code under blue shape every time I want to say the link to be followed.
Asked
Active
Viewed 285 times
0
-
4paste your code / sample data /desired output and error message to your question as text – eshirvana Jan 18 '22 at 18:49
-
1Hi and welcome to SO. It is important for the community that you *also* demonstrate that you are working to solve your issue. The best way to do that in my opinion is to include the **text** based version of the source code you have so far, even if it is not working quite right. – JonSG Jan 18 '22 at 19:14
1 Answers
0
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")
This code is working properly but i want something better in order not to use
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)
every time when i want to "communicate" with the computer.

Geek97M
- 1
-
1you should edit your question and add the code to it instead of answering your own question – pugi Jan 18 '22 at 20:42