Enter your keyword

Python speech to text

Today we will develop a python code to convert speech to text.
Pre Requisites:
First Anaconda Distribution or python should be installed
Anaconda can be installed from www.anaconda.com  which will install inbuilt with python 3.7.0 version

Python can be installed from www.python.org
once python is installed  we need to install following librabries:
Anaconda with python 3.7 version
1.) Speech Recognition
2) pyaudio
3)pyttsx3
These libraries can be installed using following commands on command prompt.

pip install speechrecognition

pip install pyttsx3

pip install pyaudio
if you face any issue while installing pyaudio download wheel file from following link;
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio
and then install using following command
pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl
Then write code following here:

# Python program to translate
# speech to text and text to speech

import speech_recognition as sr
import pyttsx3

# Initialize the recognizer
r = sr.Recognizer()

# Function to convert text to
# speech
def SpeakText(command):

# Initialize the engine
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()

# Loop infinitely for user to
# speak

while(1):

# Exception handling to handle
# exceptions at the runtime
try:

# use the microphone as source for input.
with sr.Microphone() as source2:

# wait for a second to let the recognizer
# adjust the energy threshold based on
# the surrounding noise level
r.adjust_for_ambient_noise(source2, duration=0.2)

#listens for the user’s input
audio2 = r.listen(source2)

# Using ggogle to recognize audio
MyText = r.recognize_google(audio2)
MyText = MyText.lower()

print(“Did you say “+MyText)
SpeakText(MyText)

except sr.RequestError as e:
print(“Could not request results; {0}”.format(e))

except sr.UnknownValueError:
print(“unknown error occured”)

download code from here
save this code as micexample.py
execute python code using following command
python mic example.py
start speaking
how are you
it will convert and print text on command prompt and rereads the text

screens here:

  • Sign up
Lost your password? Please enter your username or email address. You will receive a link to create a new password via email.