【鼎革‧革鼎】︰ Raspbian Stretch 《六之 K.3-言語界面-7.0 》

且讓我們回到之前擱置的主題︰

Speech recognition module for Python, supporting several engines and APIs, online and offline. https://pypi.python.org/pypi/SpeechRe…

 

在仔細閱讀文件, sudo pip3 install SpeechRecognition 安裝後,請先確認 SpeechRecognition 程式庫找得到你的麥克風︰

Troubleshooting

The recognizer hangs on recognizer_instance.listen; specifically, when it’s calling Microphone.MicrophoneStream.read.

This usually happens when you’re using a Raspberry Pi board, which doesn’t have audio input capabilities by itself. This causes the default microphone used by PyAudio to simply block when we try to read it. If you happen to be using a Raspberry Pi, you’ll need a USB sound card (or USB microphone).

Once you do this, change all instances of Microphone() to Microphone(device_index=MICROPHONE_INDEX), where MICROPHONE_INDEX is the hardware-specific index of the microphone.

To figure out what the value of MICROPHONE_INDEX should be, run the following code:

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import speech_recognition as sr

In [2]: for index, name in enumerate(sr.Microphone.list_microphone_names()):
   ...:     print("Microphone with name \"{1}\" found for `Microphone(device_ind
   ...: ex={0})`".format(index, name))
   ...:

 

This will print out something like the following:

...
Microphone with name "bcm2835 ALSA: - (hw:0,0)" found for `Microphone(device_index=0)`
Microphone with name "bcm2835 ALSA: IEC958/HDMI (hw:0,1)" found for `Microphone(device_index=1)`
Microphone with name "seeed-4mic-voicecard: - (hw:1,0)" found for `Microphone(device_index=2)`
Microphone with name "sysdefault" found for `Microphone(device_index=3)`
Microphone with name "playback" found for `Microphone(device_index=4)`
Microphone with name "dmixed" found for `Microphone(device_index=5)`
Microphone with name "ac108" found for `Microphone(device_index=6)`
Microphone with name "dmix" found for `Microphone(device_index=7)`
Microphone with name "default" found for `Microphone(device_index=8)`
Microphone with name "/dev/dsp" found for `Microphone(device_index=9)`

In [3]: 

 

Now, to use the seeed-4mic-voicecard microphone, you would change Microphone() to Microphone(device_index=2).

 

若有問題須查核 PyAudio 之版本︰

Speech Recognition Library Reference

Microphone(device_index: Union[int, None] = None, sample_rate: int = 16000, chunk_size: int = 1024) -> Microphone

Creates a new Microphone instance, which represents a physical microphone on the computer. Subclass of AudioSource.

This will throw an AttributeError if you don’t have PyAudio 0.2.11 or later installed.

If device_index is unspecified or None, the default microphone is used as the audio source. Otherwise, device_index should be the index of the device to use for audio input.

A device index is an integer between 0 and pyaudio.get_device_count() - 1 (assume we have used import pyaudio beforehand) inclusive. It represents an audio device such as a microphone or speaker. See the PyAudio documentation for more details.

The microphone audio is recorded in chunks of chunk_size samples, at a rate of sample_rate samples per second (Hertz).

Higher sample_rate values result in better audio quality, but also more bandwidth (and therefore, slower recognition). Additionally, some machines, such as some Raspberry Pi models, can’t keep up if this value is too high.

Higher chunk_size values help avoid triggering on rapidly changing ambient noise, but also makes detection less sensitive. This value, generally, should be left at its default.

Instances of this class are context managers, and are designed to be used with with statements:

In [1]: import speech_recognition as sr

In [2]: m = sr.Microphone()
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
...

In [3]: m.get_pyaudio()
Out[3]: <module 'pyaudio' from '/usr/lib/python3/dist-packages/pyaudio.py'>

In [4]: m.list_microphone_names()
Out[4]: 
['bcm2835 ALSA: - (hw:0,0)',
 'bcm2835 ALSA: IEC958/HDMI (hw:0,1)',
 'seeed-4mic-voicecard: - (hw:1,0)',
 'sysdefault',
 'playback',
 'dmixed',
 'ac108',
 'dmix',
 'default',
 '/dev/dsp']

In [5]: with m as source:
   ...:     pass 
   ...: 

In [6]: 

 

承上篇, ReSpeaker 4Mic 可用 2ch 48k 系統預設也︰

Help on Microphone in module speech_recognition object:

class Microphone(AudioSource)
| Creates a new “Microphone“ instance, which represents a physical microphone on the computer. Subclass of “AudioSource“.
|
| This will throw an “AttributeError“ if you don’t have PyAudio 0.2.11 or later installed.
|
| If “device_index“ is unspecified or “None“, the default microphone is used as the audio source. Otherwise, “device_index“ should be the index of the device to use for audio input.
|
| A device index is an integer between 0 and “pyaudio.get_device_count() – 1“ (assume we have used “import pyaudio“ beforehand) inclusive. It represents an audio device such as a microphone or speaker. See the `PyAudio documentation <http://people.csail.mit.edu/hubert/pyaudio/docs/>`__ for more details.
|
| The microphone audio is recorded in chunks of “chunk_size“ samples, at a rate of “sample_rate“ samples per second (Hertz). If not specified, the value of “sample_rate“ is determined automatically from the system’s microphone settings.
|
| Higher “sample_rate“ values result in better audio quality, but also more bandwidth (and therefore, slower recognition). Additionally, some CPUs, such as those in older Raspberry Pi models, can’t keep up if this value is too high.
|
| Higher “chunk_size“ values help avoid triggering on rapidly changing ambient noise, but also makes detection less sensitive. This value, generally, should be left at its default.

 

驗證測試 OK ◎

pi@raspberrypi:~ $ ipython3 
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import speech_recognition as sr

In [2]: r = sr.Recognizer()

In [3]: with sr.Microphone() as source:
   ...:     r.adjust_for_ambient_noise(source)
   ...:     print("Say something!")
   ...:     audio = r.listen(source)
   ...:     
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
...

Say something!

In [4]: print("Sphinx thinks you said " + r.recognize_sphinx(audio))
Sphinx thinks you said oh oh

In [5]: