Converting Speech To Text Using Python

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”.
 
Converting Speech To Text Using Python
 

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
  • Microphone
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”
  1. 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.
 
Converting Speech To Text Using Python
 
Step 4
 
Copy and save the below Code in Python IDLE 3.7.3.
  1. import speech_recognition as sr  
  2. r = sr.Recognizer()  
  3. with sr.Microphone() as source:  
  4.     print("Speak Anything :")  
  5.     audio = r.listen(source)  
  6.     try:  
  7.         text = r.recognize_google(audio)  
  8.         print("You said : {}".format(text))  
  9.          
  10.           
  11.     except:  
  12.         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”.
 
Converting Speech To Text Using Python
 
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.
 
Converting Speech To Text Using Python


Recommended Free Ebook
Similar Articles