Speech Recognition in Python



In this article, you will learn about speech recognition in Python.

Speech Recognition in Python

There are several libraries in Python that allow you to recognize speech, including:

  1. SpeechRecognition: This is a library that provides a clean and easy-to-use interface to many speech recognition APIs, including Google Speech Recognition, IBM Speech to Text, and others.
  2. PocketSphinx: This is a lightweight speech recognition engine, specifically tuned for handheld and mobile devices, though it works well on the desktop too.
  3. Google Cloud Speech API: This is a cloud-based speech recognition API provided by Google that allows you to transcribe audio files into text.

Here is an example of how you can use the SpeechRecognition library to transcribe speech to text:

import speech_recognition as sr

# initialize recognizer class (for recognizing the speech)
r = sr.Recognizer()

# Reading Microphone as source
# listening the speech and store in audio_text variable
with sr.Microphone() as source:
    audio_text = r.listen(source)
    print("Time over, thanks")

# recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
    # using google speech recognition
    print("Text: "+r.recognize_google(audio_text))
     print("Sorry, I did not get that")

Note that you’ll need to have the required dependencies installed and an internet connection to use the Google Speech Recognition API.