In this article, I am demonstrating how to convert speech to text using Python. It’s all done with the help of “Speech Recognition” APIs & “PyAudio” Library. First, I am going to explain about “PyAudio” & “Speech Recognition”.
Speech Recognition API is available as both an online and offline API (Application Programming Interface). It helps to connect with any service such as Google Translate for converting speech into text.
This Python library is used for audio input/output operations through the microphone and speaker. It will help to get our voice through the microphone.
Hardware Requirements
Software Requirements
Python 3.7.3 (Already installed in your system)
PyAudio library (Download resource Attached in this article)
Step 1
Execute the below command in your command prompt to install a “Speech Recognition” API in Python. Before installation you will verify your Python version, which is “Python 3.7.3”
pip install SpeechRecognition
Step 2
Next, we can install “PyAudio” Library. You can follow the below steps to install this library.
Download PyAudio file (File will be attached in this article).
Open PowerShell and set path to file downloaded folder.
Execute the below command in PowerShell
_pip install PyAudio-0.2.11-cp37-cp37m-win_amd64.whl _
Step 3
Open Python 3.7.3 IDLE (64 bit) from the Windows menu.
Step 4
Copy and save the below Code in Python IDLE 3.7.3.
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak Anything :")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("You said : {}".format(text))
except:
print("Sorry could not recognize what you said")
Step 5
Plug your microphone into your PC/laptop audio jack.
Step 6
Run Python code by pressing the “F5” key in your keyboard (or) select “Run” “Run Module”.
Step 7
It’s ready to listen to your voice, say some words using a microphone, after it’s recognized, the converted text is displayed in your terminal window.
Finally, we have successfully converted speech to text using Python.
Thanks for reading!
#python #developer #PyAudio #tutorial