Help with Espeak

Hello,

I’m trying to use espeak for my code and I downloaded espeak-ng from the Manjaro App store and tested it by doing: espeak-ng "This is a test" and it works. But when I try to use it in my code it won’t work. I tried using the regular engine = pyttsx3.init() but I get an error.

Does anyone know how I can get espeak to work or if there’s another offline tts alternative I can use?

Thanks.

If I run this I get no speech output (but the code recognizes and executes the commands properly)

import json
import random
import datetime
import operator
import wikipedia
import wolframalpha
import pyttsx3
import espeakng
import speech_recognition as sr 

Commander = "Commander"
print ("Initializing B.A.X.T.E.R...")

def speak(text):
    mySpeaker = espeakng.Speaker()
    #mySpeaker.say('Initializing Baxter')
    

def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning" + Commander)
    elif hour>=12 and hour<18:
        speak("Good Afternoon" + Commander)   
    else:
        speak("Good Evening" + Commander) 
    speak("How may I be of service?")   

def listenCommand():
    command=0
    hear = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = hear.listen(source)

#---------------------------
# Uses google API to listen
    try:
        print("Recognizing...")
        command = hear.recognize_google(audio, language='en-in')
        print(f'{Commander} : {command}\n')

    except:
            pass

    return command 

#--------------------------------

speak("Initializing Baxter..........")
wishMe()

def main():
    command = listenCommand()
    command=str(command).lower()

    if ('who is' in command) or ('what is' in command):
        speak('Searching Wikipedia...')
        command = command.replace("who is","")
        command = command.replace("what is","")
        results = wikipedia.summary(command, sentences = 1)
        print("Baxter:",results)
        return speak(results) 
        

    elif 'stop' in command:
        speak("Shutting Down...")
        return exit()
       
    else:
        return 0 

while True:
    main()  

If I use this code (my original one. It doesn’t work and I get an error):

import json
import random
import datetime
import operator
import wikipedia
import wolframalpha
import pyttsx3
import speech_recognition as sr 
 
Commander = "Commander"
print ("Initialising B.A.X.T.E.R...")
 
def speak(text):
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice',voices[1].id)
    engine.setProperty('rate', 125 )
    engine.setProperty('volume',1.0)
    engine.say(text)
    engine.runAndWait()
 
def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning" + Commander)
    elif hour>=12 and hour<18:
        speak("Good Afternoon" + Commander)   
    else:
        speak("Good Evening" + Commander) 
    speak("How may I be of service?")   
 
    def listenCommand():
    command=0
    hear = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = hear.listen(source)
 
#---------------------------
# Uses google API to listen
    try:
        print("Recognizing...")
        command = hear.recognize_google(audio, language='en-in')
        print(f'{BOSS} : {command}\n')
 
    except:
            pass
 
    return command 
 
#--------------------------------
 
speak("Initialising Baxter..........")
wishMe()
 
def main():
    command = listenCommand()
    command=str(command).lower()
 
    if ('who is' in command) or ('what is' in command):
        speak('Searching Wikipedia...')
        command = command.replace("who is","")
        command = command.replace("what is","")
        results = wikipedia.summary(command, sentences = 1)
        print("Baxter:",results)
        return speak(results) 
 
elif 'stop' in command:
        speak("Shutting Down...")
        return exit()
        
    else:
        return 0 
 
while True:
    main()  

This is the error I get:

Initialising B.A.X.T.E.R...
Traceback (most recent call last):
  File "/home/commander/.local/lib/python3.9/site-packages/pyttsx3/__init__.py", line 20, in init
    eng = _activeEngines[driverName]
  File "/usr/lib/python3.9/weakref.py", line 137, in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/commander/BAXTERTest2.py", line 53, in <module>
    speak("Initialising Baxter..........")
  File "/home/commander/BAXTERTest2.py", line 14, in speak
    engine = pyttsx3.init()
  File "/home/commander/.local/lib/python3.9/site-packages/pyttsx3/__init__.py", line 22, in init
    eng = Engine(driverName, debug)
  File "/home/commander/.local/lib/python3.9/site-packages/pyttsx3/engine.py", line 30, in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
  File "/home/commander/.local/lib/python3.9/site-packages/pyttsx3/driver.py", line 50, in __init__
    self._module = importlib.import_module(name)
  File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/commander/.local/lib/python3.9/site-packages/pyttsx3/drivers/espeak.py", line 9, in <module>
    from . import _espeak, toUtf8, fromUtf8
  File "/home/commander/.local/lib/python3.9/site-packages/pyttsx3/drivers/_espeak.py", line 18, in <module>
    dll = cdll.LoadLibrary('libespeak.so.1')
  File "/usr/lib/python3.9/ctypes/__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.9/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libespeak.so.1: cannot open shared object file: No such file or directory

I hope you have fixed your problem. If not, I think thoses lines should be fixed by adding mySpeaker.say(text) untead of been commenting.

def speak(text):
    mySpeaker = espeakng.Speaker()
    mySpeaker.say(text)

Without this line, espeak was never called and no speech output was executed.