I wrote a simple program to test the speech_recognition and pyttsx3 to make a virtual assistant but I keep getting the error -
"raise RuntimeError('run loop already started')
RuntimeError: run loop already started" and I can't hear the voice! Im not a professional programmer so I am not able to find out the problem. If anyone could help, that would be great! Here is my code -
import speech_recognition as sr
import pyttsx3
r1=sr.Recognizer()
engine = pyttsx3.init()
def speak(audio):
engine.say(audio)
engine.runAndWait()
while True:
with sr.Microphone() as source:
print('Jarvis Enabled')
audio=r1.listen(source)
text=r1.recognize_google(audio)
print("You: " + text)
if text.lower()=="hello" or text.lower()=="hai":
speak("Hello Sir, How are you?")
print("JARVIS: Hello Sir, How are you?")
engine.runAndWait()
elif text.lower()=="what is your name":
speak("I am Just A Really Very Intelligent System, but you can call me JARVIS!")
print("JARVIS: I am Just A Really Very Intelligent System, but you can call me JARVIS!")
engine.runAndWait()
elif text.lower()=="what are you":
speak("I am Just A Really Very Intelligent System, your personal assistant!")
print("JARVIS: I am Just A Really Very Intelligent System, your personal assistant!")
engine.runAndWait()
elif text.lower()=="exit":
speak("Bye sir, Have a great day.")
print("JARVIS: Bye sir, have a great day.")
engine.runAndWait()
break
and here is the result,
runfile('/Users/---------------/Desktop/JARVIS_AI/jarvis.py', wdir='/Users/---------------/Desktop/JARVIS_AI')
Jarvis Enabled
You: hello
Traceback (most recent call last):
File "/Users/---------------/Desktop/JARVIS_AI/jarvis.py", line 18, in <module>
speak("Hello Sir, How are you?")
File "/Users/---------------/Desktop/JARVIS_AI/jarvis.py", line 9, in speak
engine.runAndWait()
File "/Users/---------------/opt/anaconda3/lib/python3.8/site-packages/pyttsx3/engine.py", line 185, in runAndWait
raise RuntimeError('run loop already started')
RuntimeError: run loop already started
It would be great if you could help me find a solution, i tried adding other snippets of code like engine.endLoop() etc. I have pyaudio, SpeechRecognition and pyttsx3 installed. Thanks in advance!
Related
#Recording voice input using microphone
file = "file.mp3"
flag=True
fst="My name is Dan. I will answer your queries about Science. If you want to exit, say Bye"
tts = gTTS(fst, 'en')
tts.save(file)
os.system("mpg123 " + file )
r = sr.Recognizer()
prYellow(fst)
I am working with this error in this area using python and I cannot fix it. Hoping that there's anyone here who can fix this error problem.
**This is the error I have: **
Traceback (most recent call last):
File "C:\Users\Khuslensaikhan\PycharmProjects\MyVirtualAssistant\main.py", line 3, in <module>
import pywhatkit
File "C:\Users\Khuslensaikhan\PycharmProjects\MyVirtualAssistant\venv\Lib\site-packages\pywhatkit\__init__.py", line 12, in <module>
from pywhatkit.handwriting import text_to_handwriting
File "C:\Users\Khuslensaikhan\PycharmProjects\MyVirtualAssistant\venv\Lib\site-packages\pywhatkit\handwriting.py", line 1, in <module>
import requests
File "C:\Users\Khuslensaikhan\PycharmProjects\MyVirtualAssistant\venv\Lib\site-packages\requests\__init__.py", line 63, in <module>
from . import utils
File "C:\Users\Khuslensaikhan\PycharmProjects\MyVirtualAssistant\venv\Lib\site-packages\requests\utils.py", line 27, in <module>
from .cookies import RequestsCookieJar, cookiejar_from_dict
File "C:\Users\Khuslensaikhan\PycharmProjects\MyVirtualAssistant\venv\Lib\site-packages\requests\cookies.py", line 172, in <module>
class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'collections' has no attribute 'MutableMapping'
This is my program source code. I'm trying to do virtual assistant.
import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia
import pyjokes
listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def say(text):
engine.say(text)
engine.runAndWait()
# noinspection PyBroadException
def get_command():
try:
with sr.Microphone() as source:
print('Say something please.')
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'amy' in command:
command = command.replace('amy', '')
say(command)
except:
pass
return command
def get_amy():
command = get_command()
print(command)
if 'play' in command:
song = command.replace('play', '')
say('Okay bro Im playing' + song)
pywhatkit.playonyt(song)
elif 'time' in command:
time = datetime.datetime.now().strftime('%I %M %p')
print('bro its ' + time)
say('bro its' + time)
elif 'who is' in command:
person = command.replace('who is', '')
information = wikipedia.summary(person, 2)
print(information)
say(information)
elif 'what is' in command:
question = command.replace('what is', '')
information = wikipedia.summary(question, 2)
print(information)
say(information)
elif 'who are you' in command:
say('Who do you think I am Amy you idiot')
elif 'I love you' in command:
say('Sorry Im in relationship with this Nitro five laptop')
elif 'joke' in command:
joke = pyjokes.get_joke()
print(joke)
say(joke)
else:
say('please tell me something again.I dont understand')
while True:
get_amy()
I tried to install flask as someone suggested but I still have error. And also I tried to check pip paths are correct. I'm currently using Python 3.8. I'm also importing those pip's:
Pywhatkit
Pyttsxt3
Python Speech recognition
Pywikipedia
Pyaudio
I wish to have voice assistant in Hindi. Can engine.say() accept translated text? My code is below. It speaks English text, but not Hindi text. No error occurs, however no voice occurs. (My laptop sound=100%).
Can anyone help?
import pyttsx3
from translate import Translator
engine = pyttsx3.init()
translator= Translator(from_lang="english",to_lang="hindi")
translation = translator.translate("Today is Tuesday")
print(translation)
engine.say("My first code on text-to-speech")
engine.say(translation)
engine.runAndWait()
Your code ran fine after I installed its upstream dependencies
pip3 install translate pyttsx3
when I ran it got this error
OSError: libespeak.so.1: cannot open shared object file: No such file or directory
so then I installed libespeak1 ... I am on Ubuntu so this command is
sudo apt-get install libespeak1
then below python code executed correctly and I could hear the translation
from translate import Translator
import pyttsx3
translator= Translator(from_lang="english",to_lang="hindi")
# translation = translator.translate("Today is Tuesday")
translation = translator.translate("Today is Tuesday and its the day before Wednesday")
engine = pyttsx3.init()
engine.setProperty("languages",'hi')
engine.say(translation)
engine.runAndWait()
Using pyttsx3 (tried versions 2.5 to current) on Visual Studios Code on Windows 10 With Python 3.10.0.
My Problem that I am currently having is that the code will run through, but no audio is being outputted. while debugging there is no pause stepping into or over the code (for parts including pyttsx3). I made sure my audio is on, and that it is working. I used a different tts library gtts and the audio worked, but I am trying to write offline. I also tried this exact code from VS code in PyCharm and I still had the same problem. Again with no errors or warnings.
import speech_recognition as sr
import pyttsx3
listener = sr.Recognizer()
engine = pyttsx3.init(driverName='sapi5')
#voices = engine.getProperty('voices')
#engine.setProperty('voice', voices[0].id)
engine.say("Testing, audio")
engine.runAndWait
try:
with sr.Microphone() as source:
print('Listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
engine.say(command)
engine.runAndWait
except:
pass
print('Hello')
I also tried this block of code with no driver name and the same problem above persists
import speech_recognition as sr
import pyttsx3
listener = sr.Recognizer()
engine = pyttsx3.init()
#voices = engine.getProperty('voices')
#engine.setProperty('voice', voices[0].id)
engine.say("Testing, audio")
engine.runAndWait
try:
with sr.Microphone() as source:
print('Listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
engine.say(command)
engine.runAndWait
except:
pass
print('Hello')
The two commented lines on both programs didn't change anything for me either.
I also tested the program with just pyttsx3.
import pyttsx3
engine = pyttsx3.init(driverName='sapi5')
engine.say("Testing, audio")
engine.runAndWait
and tested using 'Testing, audio' instead of "Testing, audio" and I tried single words as well.
Any help would be amazing! Thank you!
In the mean time I will try to test this(translated to work with Linux) program in Linux to see if my OS is the issue.
I will also try an older version of python to see if that is the issue. Along with python 2.
My biggest assumption is that pyttsx3 needs a earlier version of python to work, but I could also be 100% wrong about that.
You forgot to put the parentheses on engine.runAndWait. Do this: engine.runAndWait()
I am trying to use the SpeechRecognition STT module for an AI i am coding and i'm also using Pyttsx but i get this error.
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyttsx/__init__.py", line 18, in <module>
from engine import Engine
ImportError: No module named 'engine'
Alot of people have tried to answer this problem, for all the people having this problem but the answers aren't explained or don't work!
Here's my code
import speech_recognition
import pyttsx
speech_engine = pyttsx.init('sapi5') # see http://pyttsx.readthedocs.org/en/latest/engine.html#pyttsx.init
speech_engine.setProperty('rate', 150)
def speak(text):
speech_engine.say(text)
speech_engine.runAndWait()
recognizer = speech_recognition.Recognizer()
def listen():
with speech_recognition.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
return recognizer.recognize_sphinx(audio)
# or: return recognizer.recognize_google(audio)
except speech_recognition.UnknownValueError:
print("Could not understand audio")
except speech_recognition.RequestError as e:
print("Recog Error; {0}".format(e))
return ""
speak("Say something!")
speak("I heard you say " + listen())
if someone can explain and fix my problem that would be helpful
thank you!
Follow this link.
It has all the updates compatible with python 3.
https://github.com/jpercent/pyttsx