Prerequisites
Basic Example
Here’s the simplest way to recognize speech from your microphone:Capture Audio from Microphone
Use the The
Microphone class as a context manager to capture audio:listen() method will automatically detect when you start and stop speaking.Complete Working Example
Here’s a complete script that demonstrates microphone recognition:microphone_recognition.py
Recognition Engines
The library supports multiple speech recognition engines. Here’s how to use each one:Google Speech Recognition (Default)
Free to use with a default API key (has usage limits):Google Cloud Speech
Requires Google Cloud authentication:CMU Sphinx (Offline)
Works offline without an internet connection:OpenAI Whisper
Use the Whisper API for high-quality transcription:Local Whisper (Offline)
Run Whisper locally without API calls:Wit.ai
Microsoft Azure Speech
Microsoft Bing Voice Recognition
Houndify
IBM Speech to Text
Error Handling
Always handle these two exceptions when recognizing speech:UnknownValueError: The engine could not understand the audioRequestError: Could not connect to the service or an API error occurred
Selecting a Specific Microphone
If you have multiple microphones, you can select which one to use:Improving Recognition Quality
For better recognition accuracy, adjust for ambient noise before listening:Next Steps
Background Listening
Listen continuously in the background while your program does other work
Custom Energy Threshold
Tune recognition sensitivity for your environment
File Transcription
Transcribe pre-recorded audio files