So i have a voice assistant project i use speech recognition and pytsx3, the problem is the functions repetition, so this is my code
main.py:
import possibilities as ps
clear = lambda: os.system('cls')
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[3].id)
engine.setProperty("rate", 160)
def speak(audio):
engine.say(audio)
engine.runAndWait()
while True:
rec = sr.Recognizer()
with sr.Microphone() as mic:
print("Manini listen...")
audio = rec.listen(mic)
try:
text = rec.recognize_google(audio, language='en')
query = text.lower()
except Exception as e:
print("Manini Listen...")
if 'wikipedia' in query:
ps.wikipediaSer(query)
elif "say" in query:
ps.sayrep(query)
elif 'open youtube' in query:
ps.openyoutube()
possibilties.py:
def wikipediaSer(query):
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences = 2)
speak("According to Wikipedia")
print(results)
speak(results)
def sayrep(query):
say_text = query.replace("say", "")
speak(say_text)
def openyoutube():
speak("Here you go to Youtube\n")
webbrowser.open("youtube.com")
so when I execute the project and say a command Wikipedia par example, Wikipedia code repet 5 times. how I can fix this repetition please.
Related
'''
on executing the following code, when I am giving any instruction to this voice assistant :
it is not printing whatever i am saying
it is opening myntra's website for every instruction I am giving
'''
import pyttsx3 #text to speech
import speech_recognition as sr #recognizes speech
import wikipedia
import webbrowser
import datetime
import os
import smtplib
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #activates voice
def speak(audio):
# enables system to speak
engine.say(audio)
engine.runAndWait()
def wishme():
to take know the current time
hour = int(datetime.datetime.now().hour)
if hour >= 00 and hour < 12:
speak("good morning!")
elif hour >= 12 and hour < 18:
speak("good afternoon!")
else:
speak("good evening!")
speak("I am JARVIS sir , how may i help you ?")
def takeCommand():
'''It takes microphone input from user and returns string output'''
# I added r.adjust_for_ambient_noise(source) and reduced threshold frequency
r = sr.Recognizer() # to recognize the user's voice
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
r.energy_threshold = 50 #to listen low frequency voices
r.adjust_for_ambient_noise(source) #to avoid noise
audio = r.listen(source)
try:
print("Recognizing...")
# to recognize user's voice
query = r.recognize_google(audio , Language = "en-in")
print(f"user said: {query}\n")
except Exception as e:
print("Say that again please...")
return "None"
return query
if __name__ == "__main__":
# speak("Akshima is a good girl")
wishme()
while True:
query = takeCommand().lower()
# logic for executing tasks based on query
# maybe there is an error in this part of the code
if 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia", "")
result = wikipedia.summary(query, sentences=2)
speak("according to wikipedia")
print(result)
speak(result)
elif 'open youtube' in query:
# to open youtube
webbrowser.open("youtube.com")
elif 'open google' in query:
webbrowser.open("google.com") #to open google
elif 'play music' in query:
webbrowser.open("spotify.com")
elif 'open myntra' or 'open any online shopping site ' in query:
webbrowser.open("myntra.com")
elif 'open instagram' in query:
webbrowser.open("instagram.com")
elif 'chill' in query:
webbrowser.open("netflix.com")
elif "the time" in query:
# tell user the current time
strTime = datetime.datetime.now().srtftime("%H:%M:%S")
speak(f"The time is {strTime}\n")
elif 'open code' in query:
#to open vs code
path = "C:\\Users\\HP\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(path)
I created a voice assistant using python neuralintents module. but it didn't give a way to use responses in intents.json to give a feedback for user. Instead it is mapping response to a function.
Is there any way to use responses in intents.json.
I got this by watching this tutorial :
https://youtu.be/SXsyLdKkKX0
Github repo :
https://github.com/NeuralNine/neuralintents
Source code from video
from email.mime import audio
from fileinput import filename
from neuralintents import GenericAssistant
import speech_recognition
import pyttsx3 as tts
import sys
import webbrowser
recognizer = speech_recognition.Recognizer()
speaker = tts.init()
speaker.setProperty('rate', 150)
todo_list = ['Go shopping', 'Clean Room', 'Recpard Videos']
def create_note():
global recognizer
speaker.say("What do you want to write into your note")
speaker.runAndWait()
done = False
while not done:
try:
with speech_recognition.Microphone as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
note = recognizer.recognize_google(audio)
note = note.lower()
speaker.say("Choose a filename!")
speaker.runAndWait()
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
filename = recognizer.recognize_google(audio)
filename = filename.lower()
with open(filename, 'w') as f:
f.write(note)
done = True
speaker.say(f"I sucessfully created the note {filename}")
speaker.runAndWait
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()
speaker.say("I did not understand you! Please try again!")
speaker.runAndWait()
def add_todo():
global recognizer
speaker.say("What doto do you want to add?")
speaker.runAndWait()
done = False
while not done:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
item = recognizer.recognize_google(audio)
item = item.lower()
todo_list.append(item)
done = True
speaker.say(f"I added {item} to the to do list!")
speaker.runAndWait()
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()
speaker.say("I did not understand. Please try again!")
speaker.runAndWait()
def show_todos():
speaker.say("The items in your to do list are following")
for item in todo_list:
speaker.say(item)
speaker.runAndWait()
def hello():
speaker.say("Hello. What can I do for you?")
speaker.runAndWait()
def exitt():
speaker.say("Bye")
speaker.runAndWait()
sys.exit(0)
mapppings = {
"greeting": hello,
"create_note": create_note,
"add_todos": add_todo,
"show_todos": show_todos,
"goodbye": exitt,
}
assistant = GenericAssistant('intents.json', intent_methods=mapppings)
assistant.train_model()
assistant.save_model()
def talk(text):
engine = tts.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine. setProperty("rate", 150)
engine.say(text)
engine.runAndWait()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
command = recognizer.recognize_google(audio)
command = command.lower()
assistant.request(command)
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()
if you want the assistant to speak the response you call the function talk and pass assistant.request(command) as parameter, but if you have bindings for adding todo ect. u first call the assistant.request(command) alone and after that you chec if the request has any response and if it does you pass the response to the talk function
def talk(text):
engine = tts.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine. setProperty("rate", 150)
engine.say(text)
engine.runAndWait()
while True:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
command = recognizer.recognize_google(audio)
command = command.lower()
assistant.request(command)
if(assistant.request(command))
talk(assistant.request(command))
except speech_recognition.UnknownValueError:
recognizer = speech_recognition.Recognizer()
import speech_recognition as sr
import pyttsx3
import string
import random
#Text To Speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices)
engine.setProperty('voice',voices[0].id)
engine.setProperty('rate', 145) #you can replace it to incease or decrease dound speed default(200)
def speak(audio): #here audio is var which contain text
engine.say(audio)
engine.runAndWait()
#now convert audio to text
def takecom():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listning....")
audio = r.listen(source)
try:
print("Recognising....")
text = r.recognize_google(audio,language='en-in')
print(text)
except Exception:
speak("error...")
print("Network connection error")
return "none"
return text
#for main function
if __name__ == "__main__":
while True:
query = takecom().lower()
if 'create password' in query or 'c' in query :
if __name__ == "__main__":
s1 = string.ascii_lowercase
s2 = string.ascii_uppercase
s3 = string.digits
s4 = string.punctuation
speak('what do you want to keep the length of the password type here')
plen =int(input('what is the length of the password')) #p
s=[]
s.extend(list(s1))
s.extend(list(s2))
s.extend(list(s3))
s.extend(list(s4))
print("Your password is:")
print("".join(random.sample(s,plen)))
speak("".join(random.sample(s,plen)))
elif query == 'none':
continue
elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query:
ex_exit = 'ok byy'
speak(ex_exit)
exit()
When i run this code every thing work fine but it ask to write the length of the password and when i write to length in it then the code proceed but i don't want to write anything in it is there any way so that i can give a voice input in the function plen (#p) to do the program work with the help of voice command.
I am using python 3.8
Oh god finally i got answer of my question by my own.
let's see the answer
In the line plen =int(input('what is the length of the password')) #p
we need to change this code to plen =int(takecom()) show that the word which we will speak will get directly converted into input in it and the code will work properly with voice command.
Here is the final code
import speech_recognition as sr
import pyttsx3
import string
import random
#Text To Speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
#print(voices)
engine.setProperty('voice',voices[0].id)
engine.setProperty('rate', 145) #you can replace it to incease or decrease dound speed default(200)
def speak(audio): #here audio is var which contain text
engine.say(audio)
engine.runAndWait()
#now convert audio to text
def takecom():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listning....")
audio = r.listen(source)
try:
print("Recognising....")
text = r.recognize_google(audio,language='en-in')
print(text)
except Exception:
speak("error...")
print("Network connection error")
return "none"
return text
#for main function
if __name__ == "__main__":
while True:
query = takecom().lower()
if 'create password' in query or 'c' in query :
if __name__ == "__main__":
s1 = string.ascii_lowercase
s2 = string.ascii_uppercase
s3 = string.digits
s4 = string.punctuation
speak('what do you want to keep the length of the password type here')
plen =int(takecom()) #p
s=[]
s.extend(list(s1))
s.extend(list(s2))
s.extend(list(s3))
s.extend(list(s4))
print("Your password is:")
print("".join(random.sample(s,plen)))
speak("".join(random.sample(s,plen)))
elif query == 'none':
continue
elif 'exit' in query or 'abort' in query or 'stop' in query or 'bye' in query or 'quit' in query:
ex_exit = 'ok byy'
speak(ex_exit)
exit()
just change ↓
plen =int(input('what is the length of the password')) #p
to ↓
plen =int(takecom()) #p
This code accepts user input as audio and responds accordingly and I want to use tkinter to display the conversation. I tried using using multithreading but it won't work.Please help me regarding how to use multithreading here.
Expected result :
The code should print the conversation on a window
And also listen to the user simultaneously
Here is my code
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
from tkinter import *
import threading
import time
root = Tk()
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# print(voices[0].id)
engine.setProperty('voice', voices[0].id)
def speak(audio):
engine.say(audio)
engine.runAndWait()
t = Text(root)
t.grid(row=0, column=0,
columnspan=2, rowspan=2, padx=5, pady=5)
t.insert(END, 'Genos says: ' + audio + '\n')
def wishme():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("Good Morning sir")
elif hour >= 12 and hour < 18:
speak("Good Afternoon sir")
else:
speak("Good Evening sir")
speak("I am Genos. How can I Serve you?")
return
t = Text(root)
t.grid(row=0, column=0,
columnspan=2, rowspan=2, padx=5, pady=5)
t.insert(END, 'Genos says: ' + wishme() + '\n')
def takecommand():
# it takes mic input from the user and return string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing..")
query = r.recognize_google(audio, language='en-in')
print(f"user Said :{query}\n")
except Exception as e:
print(e)
speak("Say that again please")
return "None"
t1 = Text(root)
t1.grid(row=0, column=2,
columnspan=2, rowspan=2, padx=5, pady=5)
t1.insert(END, 'user says: ' + query + '\n')
return query
def sendEmail(to, content):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login('mail#gmail.com', '###')
server.sendmail('mail#gmail.com', to, content)
server.close()
return
if __name__ == "__main__":
wishme()
# while True:
for _ in range(10):
query = takecommand().lower()
# Logic for executing task based query
if 'wikipedia' in query:
speak('searching Wikipedia....')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=5)
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
webbrowser.open("youtube.com")
elif 'open google' in query:
webbrowser.open("google.com")
elif 'open stackoverflow' in query:
webbrowser.open("stackoverflow.com")
elif 'play music' in query:
music_dir = 'D:\\SAHIL\\$ONGS_MJ'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir the time is {strTime}")
elif 'open code' in query:
codepath = "C:\\Users\\Sahil\\AppData\\Local\\Programs\\Microsoft VS Code
Insiders\\Code - Insiders.exe"
os.startfile(codepath)
elif 'email to sahil' in query:
try:
speak("What should i say ?")
content = takecommand()
to = "mail#gmail.com#gmail.com"
sendEmail(to, content)
speak("Email has been sent")
except Exception as e:
print(e)
speak("Could nott send the email ")
elif 'open mailbox' in query:
webbrowser.open("gmail.com");
elif 'how are you' in query:
speak("I am fine sir. How are you?")
continue
elif 'i am fine' in query:
speak("that's good to know, how can I help you")
continue
elif 'goodbye' in query:
speak("bye Sir")
exit()
root.mainloop()
I couldn't test speech recognition but at least it displays text which it speeks.
I put all your loop in function and run in Thread. I also send queue to this thread soit can send text (or commands like \quit) to main thread which use after to periodically (200ms) get text from queue and display in Text
import os
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import smtplib
import time
import threading
import queue
import tkinter as tk
# --- functions ---
def speak(text):
engine.say(text)
engine.runAndWait()
def wishme(queue):
hour = datetime.datetime.now().hour
if 0 <= hour < 12:
text = "Good Morning sir"
elif 12 <= hour < 18:
text = "Good Afternoon sir"
else:
text = "Good Evening sir"
queue.put(f'{text}.')
speak(text)
queue.put("I am Genos. How can I Serve you?\n")
speak("I am Genos. How can I Serve you?")
def takecommand():
# it takes mic input from the user and return string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing..")
query = r.recognize_google(audio, language='en-in')
print(f"user Said :{query}\n")
except Exception as e:
print(e)
speak("Say that again please")
return "None"
return query
def sendEmail(to, content):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login('mail#gmail.com', '###')
server.sendmail('mail#gmail.com', to, content)
server.close()
return
def my_loop(queue):
wishme(queue)
while True:
query = takecommand().lower()
# Logic for executing task based query
if 'wikipedia' in query:
queue.put('searching Wikipedia....')
speak('searching Wikipedia....')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=5)
queue.put("According to wikipedia" + str(results))
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
queue.put("opening youtube.com")
webbrowser.open("youtube.com")
elif 'open google' in query:
queue.put("opening google.com")
webbrowser.open("google.com")
elif 'open stackoverflow' in query:
queue.put("opening stackoverflow.com")
webbrowser.open("stackoverflow.com")
elif 'open mailbox' in query:
webbrowser.open("gmail.com");
elif 'play music' in query:
music_dir = 'D:\\SAHIL\\$ONGS_MJ'
songs = os.listdir(music_dir)
queue.put(f"playing music {songs[0]}")
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir the time is {strTime}", queue)
elif 'open code' in query:
codepath = "C:\\Users\\Sahil\\AppData\\Local\\Programs\\Microsoft VS Code Insiders\\Code - Insiders.exe"
queue.put(f"opening code {codepath}")
os.startfile(codepath)
elif 'email to sahil' in query:
try:
queue.put("What should i say ?")
speak("What should i say ?")
content = takecommand()
to = "mail#gmail.com#gmail.com"
sendEmail(to, content)
queue.put(f"Email has been sent: {content}")
speak("Email has been sent")
except Exception as e:
print(e)
queue.put(f"Could nott send the email: {e}")
speak("Could nott send the email ")
elif 'how are you' in query:
queue.put("I am fine sir. How are you?")
speak("I am fine sir. How are you?")
elif 'i am fine' in query:
queue.put("that's good to know, how can I help you")
speak("that's good to know, how can I help you")
elif 'goodbye' in query:
queue.put("bye Sir")
speak("bye Sir")
queue.put("\quit")
break # exit loop and thread will end
#exit()
def update_text():
if not queue.empty():
text = queue.get()
if text == '\quit':
root.destroy() # close window and stop `root.mainloop()`
return # don't run `after` again
else:
t.insert('end', text)
root.after(200, update_text)
# --- main ---
if __name__ == "__main__":
engine = pyttsx3.init('sapi5') # Windows
#engine = pyttsx3.init('espeak') # Linux
voices = engine.getProperty('voices')
# print(voices[0].id)
engine.setProperty('voice', voices[0].id)
# ---
root = tk.Tk()
t = tk.Text()
t.pack()
queue = queue.Queue()
update_text()
task = threading.Thread(target=my_loop, args=(queue,)) # it has to be `,` in `(queue,)` to create tuple with one value
task.start() # start thread
root.mainloop()
task.join() # wait for end of thread
This is a code for a virtual assistant this code terminates after the user speaks once. I need this code to iterate till the user says exit. I tried using 'while True' after 'if name == "main":
wishme()' it runs, but in that case it does not show the tkinter window. Can you help me please?
Here is my code
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
from tkinter import *
root = Tk()
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices[0].id)
engine.setProperty('voice', voices[0].id)
def speak(audio):
e1 = Text(root)
e1.grid(row=0, column=0)
engine.say(audio)
engine.runAndWait()
t = Text(root)
t.grid(row = 0, column = 0,
columnspan = 2, rowspan = 2, padx = 5, pady = 5)
t.insert(END, 'Genos says: '+ audio +'\n')
def wishme():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
speak("Good Morning sir")
elif hour>=12 and hour<18:
speak("Good Afternoon sir")
else:
speak("Good Evening sir")
speak("I am Genos. How can I Serve you?")
return
def takecommand():
# it takes mic input from the user and return string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing..")
query = r.recognize_google(audio, language='en-in')
print(f"user Said :{query}\n")
except Exception as e:
print(e)
print("Say that again please")
return "None"
t1 = Text(root)
t1.grid(row = 0, column = 2,
columnspan = 2, rowspan = 2, padx = 5, pady = 5)
t1.insert(END, 'user says: '+query+'\n')
return query
def sendEmail(to, content):
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login('your email#gmail.com','password')
server.sendmail('your email#gmail.com',to,content)
server.close()
return
if __name__ == "__main__":
wishme()
#while True:
if 1:
query = takecommand().lower()
#Logic for executing task based query
if 'wikipedia' in query:
speak('searching Wikipedia....')
query = query.replace("wikpedia", "")
results = wikipedia.summary(query, sentences=5)
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
webbrowser.open("youtube.com")
elif 'open google' in query:
webbrowser.open("google.com")
elif 'open stackoverflow' in query:
webbrowser.open("stackoverflow.com")
elif 'play music' in query:
music_dir = 'D:\\SAHIL\\$ONGS_MJ'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Sir the time is {strTime}")
elif 'open code' in query:
codepath = "C:\\Users\\Sahil\\AppData\\Local\\Programs\\Microsoft VS Code Insiders\\Code
- Insiders.exe"
os.startfile(codepath)
elif 'email to sahil' in query:
try:
speak("What should i say ?")
content = takecommand()
to = "your email11#gmail.com"
sendEmail(to, content)
speak("Email has been sent")
except Exception as e:
print(e)
speak("Could nott send the email ")
elif 'open mailbox' in query:
webbrowser.open("gmail.com");
elif 'exit' in query:
exit()
root.mainloop()