Automated speech recognition system using Python

Detailed project documentation for an automated speech recognition system using Python.

Project Overview

The goal of this project is to build an automated speech recognition system using Python and the SpeechRecognition library. The system will allow users to speak commands and have the system perform various actions based on the recognized speech.

Project Requirements

  • Python 3.x

  • SpeechRecognition library

  • Pyttsx3 library

  • Requests library

  • OpenWeatherMap API key (optional)

Project Architecture

The project can be divided into three main components: speech recognition, command processing, and text-to-speech output.

The speech recognition component uses the SpeechRecognition library to capture audio from the user’s microphone, recognize the speech, and convert it into text. The recognized text is then passed to the command processing component for execution.

The command processing component uses a dictionary of predefined commands and their corresponding actions to determine what action to take based on the recognized text. The commands can be customized to perform various actions such as telling the time, checking the weather, playing music, and more.

The text-to-speech output component uses the Pyttsx3 library to convert the system’s responses into speech and play them through the user’s speakers.

Project Implementation

Here is a step-by-step guide to implementing the automated speech recognition system in Python:

  1. Install the SpeechRecognition, Pyttsx3, and Requests libraries using pip.
pip install SpeechRecognition pyttsx3 requests
  1. If you want to use the get_weather() function, sign up for a free API key from OpenWeatherMap and replace the YOUR_API_KEY_HERE placeholder in the code with your own API key.

2. Create a new Python file and import the necessary libraries:

import speech_recognition as sr
import pyttsx3
import requests
import json

3. Define a function to convert text to speech using the Pyttsx3 library:

def speak(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

4. Define a function to get the current time:

def get_time():
    now = datetime.datetime.now()
    time_str = now.strftime("%I:%M %p")
    return "The time is {}".format(time_str)

5. Define a function to get the current weather using the OpenWeatherMap API:

def get_weather():
    api_key = "YOUR_API_KEY_HERE"
    base_url = "http://api.openweathermap.org/data/2.5/weather?"
    city_name = "New York"
    complete_url = base_url + "appid=" + api_key + "&q=" + city_name
    response = requests.get(complete_url)
    data = json.loads(response.text)
    weather = data["weather"][0]["description"]
    return "The weather in {} is {}".format(city_name, weather)

6. Create a dictionary of commands and their corresponding actions:

commands = {
    "hello": lambda: speak("Hi, how can I help you?"),
    "what's the time": lambda: speak(get_time()),
    "what's the weather like": lambda: speak(get_weather()),
    "goodbye": lambda: speak("Goodbye!"),
}

7. Create a Recognizer object and set the microphone as the audio source:

r = sr.Recognizer()
with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source)
    print("Say something...")
    audio = r.listen(source)

8. Use the Recognizer object to recognize the speech and convert it into text:

try:
    text = r.recognize_google(audio)
print("You said: ", text)
except sr.UnknownValueError: print("Could not understand audio") except sr.RequestError as e: print("Could not request results; {0}".format(e))

9. Use the recognized text to execute the corresponding command:
```python


if text.lower() in commands:
    commands[text.lower()]()
else:
    speak("Sorry, I didn't understand that command.")

10. Finally, call the speak() function to convert the system's response into speech and play it through the user's speakers:

speak("How else can I help you?")

Conclusion

In this project, we have built an automated speech recognition system using Python and the SpeechRecognition library. The system allows users to speak commands and have the system perform various actions based on the recognized speech. The system can be extended to include additional commands and actions, making it a useful tool for a wide range of applications.

Here is the complete source code for the automated speech recognition system using Python:

import speech_recognition as sr
import pyttsx3
import requests
import json
import datetime

# Function to convert text to speech
def speak(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()
# Function to get the current time
def get_time():
    now = datetime.datetime.now()
    time_str = now.strftime("%I:%M %p")
    return "The time is {}".format(time_str)
# Function to get the current weather using OpenWeatherMap API
def get_weather():
    api_key = "YOUR_API_KEY_HERE"
    base_url = "http://api.openweathermap.org/data/2.5/weather?"
    city_name = "New York"
    complete_url = base_url + "appid=" + api_key + "&q=" + city_name
    response = requests.get(complete_url)
    data = json.loads(response.text)
    weather = data["weather"][0]["description"]
    return "The weather in {} is {}".format(city_name, weather)
# Dictionary of commands and their corresponding actions
commands = {
    "hello": lambda: speak("Hi, how can I help you?"),
    "what's the time": lambda: speak(get_time()),
    "what's the weather like": lambda: speak(get_weather()),
    "goodbye": lambda: speak("Goodbye!"),
}
# Create a Recognizer object and set the microphone as the audio source
r = sr.Recognizer()
with sr.Microphone() as source:
    r.adjust_for_ambient_noise(source)
    print("Say something...")
    audio = r.listen(source)
# Use the Recognizer object to recognize the speech and convert it into text
try:
    text = r.recognize_google(audio)
    print("You said: ", text)
except sr.UnknownValueError:
    print("Could not understand audio")
except sr.RequestError as e:
    print("Could not request results; {0}".format(e))
# Use the recognized text to execute the corresponding command
if text.lower() in commands:
    commands[text.lower()]()
else:
    speak("Sorry, I didn't understand that command.")
# Convert the system's response into speech and play it through the user's speakers
speak("How else can I help you?")

Note that you will need to replace the YOUR_API_KEY_HERE placeholder in the get_weather() function with your own OpenWeatherMap API key if you want to use this feature.

Note that, this is just a starter code which you can use a foundation on this project.

Happy coding