I am creating a virtual assistant with Python, and when I do the part where it prints everything I speak, the code runs without errors:
import speech_recognition as sr
listener = sr.Recognizer()
try:
with sr.Microphone() as source:
print("listening...")
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'alexa' in command:
print(command)
except:
pass
But when I want to add the code which will make the virtual assistant speak, it shows errors:
import speech_recognition as sr
import pyttsx3
listener = sr.Recognizer()
engine = pyttsx3.init()
engine.say('I am alexa')
engine.say('What can I do for you?')
engine.runAndWait()
try:
with sr.Microphone() as source:
print("listening...")
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if 'alexa' in command:
print(command)
except:
pass
This is the error:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\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 "C:/Users/User/PycharmProjects/romanticAlexa/main.py", line 5, in <module>
engine = pyttsx3.init()
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\__init__.py", line 22, in init
eng = Engine(driverName, debug)
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\engine.py", line 30, in __init__
self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug)
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\driver.py", line 52, in __init__
self._driver = self._module.buildDriver(weakref.proxy(self))
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 30, in buildDriver
return SAPI5Driver(proxy)
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\pyttsx3\drivers\sapi5.py", line 35, in __init__
self._tts = comtypes.client.CreateObject('SAPI.SPVoice')
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\comtypes\client\__init__.py", line 250, in CreateObject
return _manage(obj, clsid, interface=interface)
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage
obj = GetBestInterface(obj)
File "C:\Users\User\PycharmProjects\romanticAlexa\venv\lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: module 'comtypes.gen.SpeechLib' has no attribute 'ISpeechVoice'