The Wayback Machine - https://web.archive.org/web/20240930165631/https://www.geeksforgeeks.org/speech-recognition-in-python-using-google-speech-api/
Open In App

Speech Recognition in Python using Google Speech API

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an introduction to how to make use of the SpeechRecognition library of Python. This is useful as it can be used on microcontrollers such as Raspberry Pi with the help of an external microphone.

Required Installations

The following must be installed:

Python Speech Recognition module:

sudo pip install SpeechRecognition 

PyAudio: Use the following command for Linux users

sudo apt-get install python-pyaudio python3-pyaudio

If the versions in the repositories are too old, install pyaudio using the following command

sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && 
sudo pip install pyaudio

Use pip3 instead of pip for python3. Windows users can install pyaudio by executing the following command in a terminal

pip install pyaudio

To effectively use these libraries and get comfortable with Python programming , building a strong foundation is essential. For beginners looking to enhance their Python skills, the Python Foundation course is highly recommended. This course covers everything from basic syntax to advanced topics, ensuring you have a solid grasp of Python.

Speech Input Using a Microphone and Translation of Speech to Text

  1. Configure Microphone (For external microphones): It is advisable to specify the microphone during the program to avoid any glitches. Type lsusb in the terminal for LInux and you can use the PowerShell’s Get-PnpDevice -PresentOnly | Where-Object { $_.InstanceId -match ‘^USB’ } command to list the connected USB devices. A list of connected devices will show up. The microphone name would look like this
USB Device 0x46d:0x825: Audio (hw:1, 0)
  1. Make a note of this as it will be used in the program.
  2. Set Chunk Size: This basically involved specifying how many bytes of data we want to read at once. Typically, this value is specified in powers of 2 such as 1024 or 2048
  3. Set Sampling Rate: Sampling rate defines how often values are recorded for processing
  4. Set Device ID to the selected microphone : In this step, we specify the device ID of the microphone that we wish to use in order to avoid ambiguity in case there are multiple microphones. This also helps debug, in the sense that, while running the program, we will know whether the specified microphone is being recognized. During the program, we specify a parameter device_id. The program will say that device_id could not be found if the microphone is not recognized.
  5. Allow Adjusting for Ambient Noise: Since the surrounding noise varies, we must allow the program a second or two to adjust the energy threshold of recording so it is adjusted according to the external noise level.
  6. Speech to text translation: This is done with the help of Google Speech Recognition. This requires an active internet connection to work. However, there are certain offline Recognition systems such as PocketSphinx, that have a very rigorous installation process that requires several dependencies. Google Speech Recognition is one of the easiest to use.

Troubleshooting

The following problems are commonly encountered

Muted Microphone: This leads to input not being received. To check for this, you can use alsamixer. It can be installed using

sudo apt-get install libasound2 alsa-utils alsa-oss

Type amixer . The output will look somewhat like this

Simple mixer control 'Master', 0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 65536
Mono:
Front Left: Playback 41855 [64%] [on]
Front Right: Playback 65536 [100%] [on]
Simple mixer control 'Capture', 0
Capabilities: cvolume cswitch cswitch-joined
Capture channels: Front Left - Front Right
Limits: Capture 0 - 65536
Front Left: Capture 0 [0%] [off] #switched off
Front Right: Capture 0 [0%] [off]

As you can see, the capture device is currently switched off. To switch it on, type alsamixer As you can see in the first picture, it is displaying our playback devices. Press F4 to toggle to Capture devices.

In the second picture, the highlighted portion shows that the capture device is muted. To unmute it, press the space bar . As you can see in the last picture, the highlighted part confirms that the capture device is not muted.

Current microphone not selected as a capture device: In this case, the microphone can be set by typing alsamixer and selecting sound cards. Here, you can select the default microphone device. As shown in the picture, the highlighted portion is where you have to select the sound card. The second picture shows the screen selection for the sound card.

No Internet Connection: The speech-to-text conversion requires an active internet connection.


Previous Article
Next Article

Similar Reads

Python - Get Today's Current Day using Speech Recognition
We often don't remember the day of the date due to our load of work we are doing. So, here is a Python program with the help of which we can find the day of the date with just a simple chat with our laptop or mobile. Modules NeededDateTime: This is a Library in Python with the help of which we can manipulate the date and time. It comes preinstalled
4 min read
Speech Recognition in Hindi using Python
We can use Python for Speech Recognition, it is mostly used to recognize English words. However, in this article, we are going to use Python so that it can also recognize Hindi words with the help of the Speech Recognition module. Requirements:Speech Recognition Module: It is a library with the help of which Python can recognize the command given.
2 min read
Speech Recognition in Python using CMU Sphinx
"Hey, Siri!", "Okay, Google!" and "Alexa playing some music" are some of the words that have become an integral part of our life as giving voice commands to our virtual assistants make our life a lot easier. But have you ever wondered how these devices are giving commands via voice/speech? Do applications understand your voice? How does the compute
5 min read
Restart your Computer with Speech Recognition
We can do this with the help of Python. Python has many libraries that can help many things to be done were easy. We need the help of the terminal for doing this task. Python's one of the best library Speech Recognition will help us to do this so. Modules RequiredPyttsx3: This is a text to speech Library in Python . We have to use pip for it.pip in
4 min read
Python: Convert Speech to text and text to Speech
Speech Recognition is an important feature in several applications used such as home automation, artificial intelligence, etc. This article aims to provide an introduction on how to make use of the SpeechRecognition and pyttsx3 library of Python.Installation required: Python Speech Recognition module: pip install speechrecognitionPyAudio: Use the f
3 min read
Food Recognition Selenium using Caloriemama API
Selenium is a powerful tool for controlling the web browser through a program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we are going to see how to automate the Caloriemama website using selenium. We are just g
2 min read
Python | Reading contents of PDF using OCR (Optical Character Recognition)
Python is widely used for analyzing the data but the data need not be in the required format always. In such cases, we convert that format (like PDF or JPG, etc.) to the text format, in order to analyze the data in a better way. Python offers many libraries to do this task. There are several ways of doing this, including using libraries like PyPDF2
6 min read
Magnetic Ink Character Recognition using Python
Magnetic Ink Character Recognition or MICR which is generally used in a bank. This is a 9 digit code to identify the location of the bank branch. MICR code is a code printed on cheques using MICR (Magnetic Ink Character Recognition technology). This enables identification of the cheques and which in turns means faster processing. In this article, w
2 min read
Image Recognition using TensorFlow
In this article, we'll create an image recognition model using TensorFlow and Keras. TensorFlow is a robust deep learning framework, and Keras is a high-level API(Application Programming Interface) that provides a modular, easy-to-use, and organized interface to solve real-life deep learning problems. Image Recognition: In Image recognition, we inp
6 min read
Intent Recognition using TensorFlow
Intent recognition is a method of natural language processing, which deals with determining intent of a given sentence, or in simple terms "what the sentence means". It is commonly used in chatbots, virtual assistants, and other conversational AI systems to understand user requests and provide appropriate responses. In this article, We will show a
10 min read
Part of Speech Tagging with Stop words using NLTK in python
The Natural Language Toolkit (NLTK) is a platform used for building programs for text analysis. One of the more powerful aspects of the NLTK module is the Part of Speech tagging.In order to run the below python program you must have to install NLTK. Please follow the installation steps. Open your terminal, run pip install nltk.Write python in the c
4 min read
Python | Text to Speech by using pyttsx3
pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline and is compatible with both Python 2 and 3. An application invokes the pyttsx3.init() factory function to get a reference to a pyttsx3. Engine instance. it is a very easy to use tool which converts the entered text into speech. The pyttsx3 modul
2 min read
Text to speech GUI convertor using Tkinter in Python
Prerequisite: Introduction to Tkinter Tkinter is the standard GUI library for Python. Python when combined with tkinter provides a fast and easy way to create GUI applications.By this library we can make a compelling choice for building GUI applications in Python, especially for applications where a modern sheen is unnecessary, and the top priority
3 min read
Convert Text to Speech in Python using win32com.client
There are several APIs available to convert text to speech in python. One of such APIs available in the python library commonly known as win32com library. It provides a bunch of methods to get excited about and one of them is the Dispatch method of the library. Dispatch method when passed with the argument of SAPI.SpVoice It interacts with the Micr
2 min read
Convert PDF File Text to Audio Speech using Python
Let us see how to read a PDF that is converting a textual PDF file into audio. Packages Used: pyttsx3: It is a Python library for Text to Speech. It has many functions which will help the machine to communicate with us. It will help the machine to speak to usPyPDF2: It will help to the text from the PDF. A Pure-Python library built as a PDF toolkit
2 min read
Calculate distance and duration between two places using google distance matrix API in Python
Google Map Distance Matrix API is a service that provides travel distance and time is taken to reach a destination. This API returns the recommended route(not detailed) between origin and destination, which consists of duration and distance values for each pair. To use this API, one must need the API key, which can be get form here. Modules needed
2 min read
How to make a Google Translation API using Python?
Google Translate is a free multilingual translation service, based on statistical and neural machine translation, developed by Google. It is widely used to translate complete websites or webpages from one languages to another. We will be creating a python terminal application which will take the source language, target language, a phrase to transla
4 min read
Language Translator Using Google API in Python
API stands for Application Programming Interface. It acts as an intermediate between two applications or software. In simple terms, API acts as a messenger that takes your request to destinations and then brings back its response for you. Google API is developed by Google to allow communications with their servers and use their API keys to develop
3 min read
Speech To Text using IBM Watson Studio
IBM Watson Studio is an integrated environment designed to develop, train, manage models, and deploy AI-powered applications and is a Software as a Service (SaaS) solution delivered on the IBM Cloud. The IBM Cloud provides lots of services like Speech To Text, Text To Speech, Visual Recognition, Natural Language Classifier, Language Translator, etc
2 min read
Python Text To Speech | pyttsx module
pyttsx is a cross-platform text to speech library which is platform independent. The major advantage of using this library for text-to-speech conversion is that it works offline. However, pyttsx supports only Python 2.x. Hence, we will see pyttsx3 which is modified to work on both Python 2.x and Python 3.x with the same code. Use this command for I
2 min read
Extract speech text from video in Python
Nowadays, videos have become an integral part of our lives. Videos educate us and provide the necessary information. In this article, we will learn how to extract text speech from video using Python. Extract Speech Text from the Video To extract speech text from video in Python, we require the following modules to install, Here we are using Python
3 min read
Convert Text to Speech in Python
There are several APIs available to convert text to speech in Python. One of such APIs is the Google Text to Speech API commonly known as the gTTS API. gTTS is a very easy to use tool which converts the text entered, into audio which can be saved as a mp3 file. The gTTS API supports several languages including English, Hindi, Tamil, French, German
4 min read
Audio processing using Pydub and Google speechRecognition API
Audio files are a widespread means of transferring information. So, let's see how to break down audio files (.wav files) into smaller chunks, and to recognize the content in them and store it to a text file. To know more about audio files and their formats, refer Audio_formats. Need to break down an audio file? When we do any processing on audio fi
6 min read
Know the Difference Between REST API and RESTful API
APIs (Application Programming Interface) act as an interface between two applications to interact and provide the relevant data. It uses a set of protocols using which the operation is done. Salesforce was the first organization to officially launch API, followed by eBay and Amazon. Also, 60% of transactions made on eBay use their APIs. If we talk
5 min read
POS(Parts-Of-Speech) Tagging in NLP
One of the core tasks in Natural Language Processing (NLP) is Parts of Speech (PoS) tagging, which is giving each word in a text a grammatical category, such as nouns, verbs, adjectives, and adverbs. Through improved comprehension of phrase structure and semantics, this technique makes it possible for machines to study and comprehend human language
11 min read
Google Cloud Platform - Running Different Versions of Python on Google Cloud Run
Problem Statement: Rinki works for StoreCraft(Say) as a site reliability engineer, where she's on-call maintaining their custom storefront. StoreCraft's systems are developed in-house in Python and have been running on virtual machines for years, a set up that evolved as StoreCraft's businesses did. These virtual machines use slightly different ver
3 min read
What is Google Sheets API and How to Use it?
We all are familiar with spreadsheets and worked with them since we first learned about computers. We are used to arranging our data in a tabular manner in the form of rows and columns. When we are working on a project and wish to save our data in a tabular form, we think of relational databases. Instead of going with a relational database, we can
8 min read
Why Google Shutters Google+ ?
Google+ the social media platform provided by Google, was shut down recently by the company. It was not showing expected growth in terms of user presence and interaction even after investing a lot in recent years. Its competitors had progressed a lot leaving Google+ behind to such an extent that a recent survey concluded that a user spends only abo
4 min read
Google Cloud Platform - Creating Google Cloud Console Account & Projects
Google Cloud Platform (GCP) is a suite of cloud computing services offered by Google to the developers to implement the infrastructure used in Google products in their custom applications. GCP provides several cloud services for storage and computing and provides infrastructure as a service (IaaS), platform as a service (PaaS), and serverless compu
3 min read
How to Google like a Pro – 10 Google Search Tips for Better Results!
Every single day, countless individuals rely on Google search to fulfill their diverse needs. Whether it's students seeking knowledge for their assignments, professionals conducting research, or even those looking for a quick dose of entertainment, Google is the go-to tool. However, are you truly harnessing the power of effective Google search tips
12 min read
Article Tags :
Practice Tags :