In this article i will explain you how you can convert any voice data to text format in python
Step 1: Install necessary python library which gonna help in converting speech to text
- Install SpeechRecognition python module
#To install speech recogntion module if you are using python 3
sudo pip3 install SpeechRecognition
- Install pyaudio python module linux ubuntu user
#To install pyaudio if you are using linux ubuntu run this command
sudo apt-get install portaudio19-dev python-pyaudio
#After running above command run the following command
sudo pip3 install PyAudio
- Install pyaudio python module windows and macos user
pip3 install PyAudio
Step 2: Once all the required python library installed copy paste the following code in to your python script file
import speech_recognition as sr
sample_rate = 48000
chunk_size = 2048
r = sr.Recognizer()
#This will return all the list of input devices on your computer in the form of list you can select any input device and
#and pass the index of the device as first argument of sr.Microphone function
mic_list = sr.Microphone.list_microphone_names()
with sr.Microphone(0, sample_rate = sample_rate, chunk_size = chunk_size) as source:
#this will adjust the energy threshold based on the surrounding noise level
r.adjust_for_ambient_noise(source)
print ("Tell Something")
#listens for the user's input
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print ("you said: ",text)
#error occurs when not understand what was said
except sr.UnknownValueError:
print("Not able to understand your audio")
except sr.RequestError as e:
print("Some error occured; {0}".format(e))
Step 3: Run the python script
#In case if you are using python 3 you need to use python3 instead of python to run your script
python your_script.py