Ultimate Guide 8000+ words 60+ examples
Welcome to the most comprehensive SpeechRecognition tutorial on the web. This guide covers everything from pip install SpeechRecognition to building a full AI voice assistant. Whether you are a beginner or an advanced developer, you’ll find practical code, real‑world projects, and expert tips.
SpeechRecognition is a Python library that enables speech‑to‑text conversion. It supports multiple engines (Google, Whisper, Sphinx, Vosk, etc.) and works with both microphone input and audio files. This tutorial will take you from zero to building your own voice‑controlled applications.
SpeechRecognition is a wrapper library that provides a unified interface for various speech recognition APIs and engines. It abstracts away the complexity of each backend, allowing you to switch between Google, Whisper, Sphinx, Bing, and others with minimal code changes.
It’s the most popular Python speech‑to‑text library due to its simplicity, flexibility, and extensive engine support. Whether you need a quick prototype or a production‑grade voice assistant, SpeechRecognition is the go‑to choice.
pip install SpeechRecognitionFor microphone support, install PyAudio:
pip install PyAudioPyAudio – microphone inputpydub – audio file conversionwave – WAV file handlingspeechrecognition itselfOn Windows: pip install PyAudio (may need wheels). On Linux: sudo apt install python3-pyaudio. On macOS: brew install portaudio then pip install PyAudio.
import speech_recognition as sr
print(sr.__version__)import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
print("You said: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Could not understand audio")The Recognizer class is the main entry point. It holds the configuration (energy_threshold, pause_threshold) and provides methods like listen(), record(), and recognition methods.
Microphone() opens the default microphone. Use device_index to select a specific mic.
AudioFile('file.wav') allows reading audio from a file.
Captures audio from the microphone until silence is detected.
Reads audio data from a file.
Uses Google's free speech recognition API (requires internet).
Offline recognition using CMU Sphinx.
Microsoft Bing Speech API (requires key).
Uses OpenAI Whisper (local or API).
Calibrates the energy threshold based on background noise.
Minimum audio energy to consider as speech. Default 300.
Seconds of silence to end a phrase. Default 0.8.
Minimum length of a phrase.
timeout: seconds to wait for speech. phrase_time_limit: max seconds for a phrase.
with sr.AudioFile('hello.wav') as source:
audio = r.record(source)
print(r.recognize_google(audio))Use pydub to convert MP3 to WAV first.
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
print(r.recognize_google(audio))Use recognize_sphinx() or Vosk for offline.
Google, Whisper API, Bing require internet.
Free, no key required for limited usage.
High accuracy, supports multiple languages. Use recognize_whisper().
Offline, lightweight, supports many languages.
Use language='ur-PK' or 'en-US' in recognize methods.
r.recognize_google(audio, language='ur-PK')
r.recognize_google(audio, language='en-US')Use adjust_for_ambient_noise() and adjust energy_threshold.
Use a loop with listen() and a timeout.
while True:
with sr.Microphone() as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print(text)
except: passParse the recognized text and execute actions (e.g., open browser, play music).
Combine with pyttsx3 for TTS and build a JARVIS‑like assistant. Full code provided in the advanced section.
Feed the recognized text into an LLM (OpenAI, etc.) and speak the response.
Voice‑controlled home automation, file manager, etc.
Try different microphones, increase energy_threshold, or use a quieter environment.
Use threading for continuous listening. Lower phrase_time_limit for faster responses.
Offline recognition (Sphinx/Vosk) is more private. For Google API, no data is stored by default.
try/except around recognition.| Engine | Offline | Accuracy | Languages | Speed |
|---|---|---|---|---|
| Vosk | ✅ | High | 20+ | Fast |
| Whisper | ✅ | Very High | 100+ | Medium |
| DeepSpeech | ✅ | Good | English | Slow |
| ❌ | High | 125+ | Fast |
Q1: Is SpeechRecognition free?
A: Yes, MIT licensed.
Q2: Can I use it offline?
A: Yes, with Sphinx or Vosk.
Q3: Does it support Urdu?
A: Yes, with Google/Whisper/Vosk.
Q4: How to improve accuracy?
A: Use a good mic and adjust ambient noise.
1. Which method captures microphone audio? → listen()
2. Which class represents the recognizer? → Recognizer
Q1: What is SpeechRecognition?
A: A Python library for speech‑to‑text.
Q2: How do you install it?
A: pip install SpeechRecognition
Exercise 1: Write a program that listens for 5 seconds and prints the text.
Exercise 2: Read a WAV file and transcribe it.
# Complete Voice Assistant (simplified)
import speech_recognition as sr
import pyttsx3
engine = pyttsx3.init()
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Listening...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print(f"You said: {text}")
engine.say(f"You said {text}")
engine.runAndWait()
except:
print("Sorry, I didn't catch that.")You now have a complete understanding of the SpeechRecognition library. Start building your own voice‑enabled applications today. Combine it with pyttsx3, OpenAI, and automation to create powerful AI assistants.
Next steps: Check out our tutorials on PyAudio, pyttsx3, and OpenCV.
© 2026 LegalCodx – Complete SpeechRecognition Tutorial. All code is Python 3.10+ compatible.