Here are some resources for more information about topics covered in this lesson:
Working With Microphones
00:00 Working with microphones. To access your microphone with SpeechRecognition, you’ll need to install the PyAudio package. Close the REPL you’ve been using, and let’s take a look at the steps needed to install PyAudio.
Installing on Windows can be a little involved, as the standard
pip install method doesn’t work on Python 3.7 and above, and attempting an install will lead to a number of errors of missing components on your system. However, the Unofficial Windows Binary page offers wheels which can be downloaded and installed using the command seen onscreen.
01:30 As with the installation of any software, you need to ensure that you’re happy that the software you’re installing is legitimate, but these are packages I’ve personally used on a number of projects involving Windows where no other solution was available, and they’ve worked well.
01:45 Once you’ve got PyAudio installed, you can test the installation from the console. Make sure your default microphone is on and unmuted. If the installation worked, you should see something like this.
02:01 “Hello from Real Python.” The microphone is working correctly. The microphone works. Go ahead and play around with it a little bit by speaking into your microphone and seeing how well SpeechRecognition transcribes your speech.
02:31 This output comes from the ALSA package installed with Ubuntu—not SpeechRecognition or PyAudio. In all reality, these messages may indicate a problem with your ALSA configuration, but in my experience, they don’t impact the functionality of your code.
Given the output you’ve seen onscreen, if you wanted to use the
'BlackHole 2ch' microphone, which has index
2 in the list, you’d create a
Microphone instance using the code seen onscreen now. For most projects, though, you’ll probably want to use the default system microphone.
.listen() to capture microphone input. Now that you’ve got a microphone instance ready to go, it’s time to capture some input. Just like the
Microphone is a context manager. You can capture input from the microphone using the
.listen() method of the
Recognizer class inside of the
After running this code, wait a second for
.adjust_for_ambient_noise() to do its thing, and then try speaking “hello” into the microphone. Again, you’ll have to wait a moment for the interpreter prompt to return before trying to recognize the speech.
The SpeechRecognition documentation recommends using a duration of no less than
0.5 seconds. In some cases, you may find that durations longer than the default of
1 second generate better results.
The minimum value you need depends on the microphone’s ambient environment. Unfortunately, this information is typically unknown during development. In my experience, the default duration of
1 second is adequate for most applications.
Handling unrecognizable speech. Try typing the previous code example into the interpreter and making some unintelligible noises into the microphone. You should get a response similar to seen onscreen. Audio that cannot be matched to text by the API raises an
The API works very hard to transcribe any vocal sounds, and even short grunts were transcribed as words like
"how" for me. Coughing, hand claps, and tongue clicks would consistently raise the exception.
Become a Member to join the conversation.