Introduction
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”.
About “Speech Recognition” API
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.
About “PyAudio”Library
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.
Prerequisites for development
Hardware Requirements
Software Requirements
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.
Summary
Finally, we have successfully converted speech to text using Python.