Skip to content

Instantly share code, notes, and snippets.

@GGulati
Last active April 15, 2023 14:00
Show Gist options
  • Save GGulati/1ebaeaaa7f7408647fef to your computer and use it in GitHub Desktop.
Save GGulati/1ebaeaaa7f7408647fef to your computer and use it in GitHub Desktop.
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())
@wajdi522
Copy link

plzzz
i need introduction for understanding 🔢

@GGulati
Copy link
Author

GGulati commented Mar 2, 2016

Hey, this is the code sample for my blog article at https://ggulati.wordpress.com/2016/02/24/coding-jarvis-in-python-3-in-2016/

@sroy8091
Copy link

The second speak call is not working properly. After saying "I" it stopped saying.

@sailsolar
Copy link

I get the following output on the terminal and the prompter stops after 'i'.

ALSA lib pcm_dsnoop.c:618:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
bt_audio_service_open: connect() failed: Connection refused (111)
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave

What should I do to correct this?

@mfuxi
Copy link

mfuxi commented Nov 3, 2016

When iḿ running the code on my Ubuntu I am getting this output:
Traceback (most recent call last): File "pycode/jarvis.py", line 4, in <module> speech_engine = pyttsx.init('sapi5') # see http://pyttsx.readthedocs.org/en/latest/engine.html#pyttsx.init File "/usr/local/lib/python2.7/dist-packages/pyttsx/__init__.py", line 39, in init eng = Engine(driverName, debug) File "/usr/local/lib/python2.7/dist-packages/pyttsx/engine.py", line 45, in __init__ self.proxy = driver.DriverProxy(weakref.proxy(self), driverName, debug) File "/usr/local/lib/python2.7/dist-packages/pyttsx/driver.py", line 64, in __init__ self._module = __import__(name, globals(), locals(), [driverName]) File "/usr/local/lib/python2.7/dist-packages/pyttsx/drivers/sapi5.py", line 19, in <module> import win32com.client ImportError: No module named win32com.client
Why is it trying to do import win32com.client? I am using Ubuntu..

Update
Managed to make it work by removing sapi5 from pyttsx('sapi5')
But after that I got @husanin-ahmad error, I managed to get rid of the bit_audio errors by removing bluez-alsa package.
I'm still working on the unable to open slave error

@vidit0210
Copy link

Same Error as @husanin-ahmad .Kindly Guide

@kunci115
Copy link

kunci115 commented Jan 3, 2017

i thought this code running only in windows,because it need win32com.client,or any win32com.client for linux ?

@k-ksharma
Copy link

look into the documentation of pyttsx and choose the correct engine corresponding to linux. no need of win32com client. hope it hrlps @kunci115
i think the engine is espeak or you can left that blank like pyttsx.init()

@ranand16
Copy link

ranand16 commented Feb 5, 2017

i have installed speech recognition but still i get

Traceback (most recent call last):
File "Jarvis.py", line 1, in
import SpeechRecognition
ImportError: No module named SpeechRecognition

@banziyeoh
Copy link

it does not record what i said

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment