Hands-On Guide To Develop Speech To Text Converter Using Python and Google API

Hey Siri”, “okay Google” and “Alexa” is something we say almost every day to quickly get information without having to type in the search box. These devices are great to listen and understand your voice and give a suitable output. How do they work? They are designed in a highly efficient speech recognition software that can understand multiple accents and a natural language processing algorithm to convert this speech into text. But before these smart devices find the information you asked for, they need to understand what you are saying. Let us implement a speech to text converter using Python and a google API.

In this article, we will build a simple speech to text converter with Python and the google cloud API.

What is speech recognition and how does it work?

Speech recognition is a system that translates the language being spoken into text format. To do this, a deep learning model is used that takes in audio signals, analyses them and converts them into the corresponding text.


Sign up for your weekly dose of what's up in emerging technology.
google API

Above is the workflow of the google API for converting speech to text. It takes in the voice input from the user device and this is sent to some of the core cloud functions. These functions perform internal processes like converting the audio input into signals and preprocessing them. Then, it is sent to the speech to text API which applies a deep learning model and understands what the user is trying to say. Finally, it is passed to the autoML NLP where the speech signal that is understood by the deep learning model is converted into text format and the output is displayed. 

Features of the speech to text API

  1. Streaming speech to text in real-time: the API is capable of processing real-time audio signals from the device microphone or take an audio file as input and convert it into text also. 
  2. Different models based on the domain: you can choose from different trained models depending on the requirements of the project. For example, for converting audio from a telephone, the enhanced phone call model can be used. 
  3. Adaptation: you can customize the API to understand rare words, currency, numbers etc by making these as additional classes.

Implementation with microphone

Now that we know how the Google API works we will put it to use and activate the microphone in the system and convert it into text. 

Download our Mobile App


Before we get into the implementation, you will have to download the library with the pip command. 

pip install SpeechRecognition

Next, we can use the API and write code to build the speech to text converter in real-time for the English language. 


We will first import the library and activate our microphone as follows:

import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.Microphone() as inputs:
    print("Please speak now")
    listening = recognizer.listen(inputs)

Now that we have the input ready it is time to call the Google API to recognize the speech and display the text. 

        print("Did you say: "+recognizer.recognize_google(listening))
         print("please speak again")


Another interesting thing about this is the number of languages it supports. It not only supports common languages of the world but also supports multiple Indian languages as well. Since I speak Kannada, I will include one small change in the code and display the output in Kannada. 

Implementation of speech to text in Kannada

To make the API understand the language and give the output, just make the following changes to the code.

import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.Microphone() as inputs:
    print("Please speak now")
    listening = recognizer.listen(inputs)
        print("Did you say: "+recognizer.recognize_google(listening,language = "kn-IN"))
         print("please speak again")


Implementation on an audio file

We saw how to use the API in real-time with the microphone for English and Kannada language. But what if we have an audio file and want to extract text from that? To do this, first, let us select the audio file. 

import speech_recognition as sr
recognition = sr.Recognizer()
with sr.AudioFile('myvoice.wav') as inputs:
    file_audio = recognition.listen(inputs)
        convert_text = recognition.recognize_google(file_audio)
         print('could not hear')

As shown the contents of the audio file is displayed as text. 


In this article, we saw how to make use of the Google API to convert speech to text using the microphone in English and Kannada and using an audio file as well. This can be really useful in natural language processing projects for handling audio files and transcripts as well. 

Support independent technology journalism

Get exclusive, premium content, ads-free experience & more

Rs. 299/month

Subscribe now for a 7-day free trial

More Great AIM Stories

Bhoomika Madhukar
I am an aspiring data scientist with a passion for teaching. I am a computer science graduate from Dayananda Sagar Institute. I have experience in building models in deep learning and reinforcement learning. My goal is to use AI in the field of education to make learning meaningful for everyone.

AIM Upcoming Events

Early Bird Passes expire on 3rd Feb

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox