Voice recognition in python

Nkugwa Mark William
2 min readFeb 21, 2023

--

Speech recognition is the process of converting spoken words into text. It is a form of natural language processing that has become increasingly popular in recent years. Python has a number of libraries for speech recognition, including the SpeechRecognition library.

The SpeechRecognition library is a popular Python library for performing speech recognition. It provides a simple way to convert spoken words into text, and it supports a variety of different speech recognition engines.

Here is an example code for using the SpeechRecognition library in Python:

import speech_recognition as sr

# create a Recognizer object
r = sr.Recognizer()

# use the default microphone as the audio source
with sr.Microphone() as source:
print("Speak now!")
audio = r.listen(source)

# recognize speech using Google Speech Recognition
try:
print("You said: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))

Let’s go through the code line by line:

  1. import speech_recognition as sr: We begin by importing the SpeechRecognition library using the standard Python import statement. We also give the library a shorter name, "sr", to make it easier to use in our code.
  2. r = sr.Recognizer(): We create a Recognizer object, which will be used to recognize speech.
  3. with sr.Microphone() as source:: We create a context manager using the with keyword, which is used to ensure that the microphone is properly released when we're done using it. We also create a Microphone object, which is used as the audio source for the recognizer.
  4. print("Speak now!"): We print a message to the console, asking the user to speak.
  5. audio = r.listen(source): We use the recognizer to listen for audio from the microphone, and store the resulting audio data in a variable called audio.
  6. try:: We begin a try block to handle errors that might occur during speech recognition.
  7. print("You said: " + r.recognize_google(audio)): We use the Google Speech Recognition engine to convert the spoken words in the audio variable into text. If the recognition is successful, we print the resulting text to the console.
  8. except sr.UnknownValueError:: If the speech recognizer is unable to recognize the speech, it will raise an UnknownValueError. We catch this error and print an error message to the console.
  9. except sr.RequestError as e:: If there is a problem with the Google Speech Recognition service, the recognizer will raise a RequestError. We catch this error and print an error message to the console.

This code should give you a basic idea of how speech recognition works in Python using the SpeechRecognition library. The library supports a variety of different speech recognition engines, so you can experiment with different engines to see which one works best for your needs.

--

--

Nkugwa Mark William
Nkugwa Mark William

Written by Nkugwa Mark William

Nkugwa Mark William is a Chemical and Process engineer , entrepreneur, software engineer and a technologists with Apps on google play store and e commerce sites

No responses yet