0
0
Iot-protocolsHow-ToBeginner · 4 min read

Raspberry Pi Voice Assistant Project: Setup and Code Example

To create a voice assistant on a Raspberry Pi, use Python with the speech_recognition library to capture voice commands and pyttsx3 for text-to-speech responses. Connect a microphone and speaker, install required libraries, and write a script to listen, process, and respond to your voice.
📐

Syntax

This is the basic syntax to set up a voice assistant on Raspberry Pi using Python:

  • import speech_recognition as sr: Imports the library to recognize speech from microphone input.
  • import pyttsx3: Imports the library to convert text to speech.
  • recognizer = sr.Recognizer(): Creates a recognizer object to process audio.
  • with sr.Microphone() as source:: Opens the microphone to listen for audio.
  • audio = recognizer.listen(source): Captures the audio input.
  • recognizer.recognize_google(audio): Converts audio to text using Google's free API.
  • engine = pyttsx3.init(): Initializes the text-to-speech engine.
  • engine.say(text) and engine.runAndWait(): Speak the response aloud.
python
import speech_recognition as sr
import pyttsx3

recognizer = sr.Recognizer()
engine = pyttsx3.init()

with sr.Microphone() as source:
    print("Listening...")
    audio = recognizer.listen(source)

try:
    text = recognizer.recognize_google(audio)
    print(f"You said: {text}")
    engine.say(f"You said {text}")
    engine.runAndWait()
except sr.UnknownValueError:
    print("Sorry, I did not understand.")
    engine.say("Sorry, I did not understand.")
    engine.runAndWait()
Output
Listening... You said: hello (Voice output: "You said hello")
💻

Example

This example shows a simple voice assistant that listens to your voice, converts it to text, and repeats it back using speech.

python
import speech_recognition as sr
import pyttsx3

# Initialize recognizer and text-to-speech engine
recognizer = sr.Recognizer()
engine = pyttsx3.init()

print("Say something!")

with sr.Microphone() as source:
    recognizer.adjust_for_ambient_noise(source)
    audio = recognizer.listen(source)

try:
    # Convert speech to text
    command = recognizer.recognize_google(audio)
    print(f"You said: {command}")
    # Speak back the command
    engine.say(f"You said {command}")
    engine.runAndWait()
except sr.UnknownValueError:
    print("Sorry, I could not understand your voice.")
    engine.say("Sorry, I could not understand your voice.")
    engine.runAndWait()
except sr.RequestError:
    print("Could not request results from the speech service.")
    engine.say("Could not request results from the speech service.")
    engine.runAndWait()
Output
Say something! You said: hello world (Voice output: "You said hello world")
⚠️

Common Pitfalls

  • Microphone not detected: Ensure your microphone is connected and recognized by Raspberry Pi.
  • Ambient noise: Background sounds can cause recognition errors; use adjust_for_ambient_noise() to reduce this.
  • Missing libraries: Install speech_recognition and pyttsx3 using pip3 install SpeechRecognition pyttsx3.
  • Internet connection: Google's speech recognition requires internet; no connection causes errors.
  • Audio device permissions: Make sure your user has permission to access audio devices.
python
import speech_recognition as sr
import pyttsx3

recognizer = sr.Recognizer()
engine = pyttsx3.init()

with sr.Microphone() as source:
    recognizer.adjust_for_ambient_noise(source)
    print("Listening...")
    audio = recognizer.listen(source)

try:
    text = recognizer.recognize_google(audio)
    print(f"You said: {text}")
    engine.say(f"You said {text}")
    engine.runAndWait()
except sr.UnknownValueError:
    print("Sorry, I did not understand.")
    engine.say("Sorry, I did not understand.")
    engine.runAndWait()

# Wrong way example:
# Missing adjust_for_ambient_noise causes poor recognition in noisy places
# with sr.Microphone() as source:
#     print("Listening...")
#     audio = recognizer.listen(source)  # No noise adjustment

# Right way:
# with sr.Microphone() as source:
#     recognizer.adjust_for_ambient_noise(source)
#     print("Listening...")
#     audio = recognizer.listen(source)
📊

Quick Reference

Here are quick tips for building a Raspberry Pi voice assistant:

  • Use speech_recognition for capturing and converting speech.
  • Use pyttsx3 for offline text-to-speech output.
  • Call adjust_for_ambient_noise() before listening to improve accuracy.
  • Ensure microphone and speaker are properly connected and configured.
  • Test with simple commands first to verify setup.

Key Takeaways

Install and import speech_recognition and pyttsx3 libraries to handle voice input and output.
Use adjust_for_ambient_noise() to improve speech recognition accuracy in noisy environments.
Google's speech recognition requires internet; plan for offline alternatives if needed.
Test microphone and speaker hardware before running your voice assistant script.
Handle exceptions to manage recognition errors gracefully.