Reconnaissance vocale

Bonjour,

Ca paraît se faire.
Ici :

Quelqu’un(e) a-t-il déjà fait cela avec ce module ?

Je l’installe ainsi :

$ pip3 install SpeechRecognition

Il faut aussi le module “pyAudio” :

$ pip3 install --user pyAudio

Et au final j’obtiens une erreur :

Collecting pyAudio
   Using cached PyAudio-0.2.14.tar.gz (47 kB)
   Installing build dependencies: started
   Installing build dependencies: finished with status 'done'
   Getting requirements to build wheel: started
   Getting requirements to build wheel: finished with status 'done'
   Preparing metadata (pyproject.toml): started
   Preparing metadata (pyproject.toml): finished with status 'done'
Building wheels for collected packages: pyAudio
   Building wheel for pyAudio (pyproject.toml): started
   Building wheel for pyAudio (pyproject.toml): finished with status 'error'
Failed to build pyAudio
ed-with-error

   × Building wheel for pyAudio (pyproject.toml) did not run successfully.
   │ exit code: 1
   ╰─> [17 lines of output]
       running bdist_wheel
       running build
       running build_py
       creating build
       creating build/lib.linux-x86_64-3.10
       creating build/lib.linux-x86_64-3.10/pyaudio
       copying src/pyaudio/__init__.py -> 
build/lib.linux-x86_64-3.10/pyaudio
       running build_ext
       creating build/temp.linux-x86_64-3.10
       creating build/temp.linux-x86_64-3.10/src
       creating build/temp.linux-x86_64-3.10/src/pyaudio
       x86_64-linux-gnu-gcc -Wno-unused-result -Wsign-compare -DNDEBUG 
-g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat 
-Werror=format-security -g -fwrapv -O2 -fPIC -I/usr/local/include 
-I/usr/include -I/usr/include/python3.10 -c src/pyaudio/device_api.c -o 
build/temp.linux-x86_64-3.10/src/pyaudio/device_api.o
       src/pyaudio/device_api.c:9:10: fatal error: portaudio.h: Aucun 
fichier ou dossier de ce type
           9 | #include "portaudio.h"
             >          ^~~~~~~~~~~~~
       compilation terminated.
       error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit 
code 1
       [end of output]

   note: This error originates from a subprocess, and is likely not a 
problem with pip.
   ERROR: Failed building wheel for pyAudio
ERROR: Could not build wheels for pyAudio, which is required to install 
pyproject.toml-based projects

Comment dois-je procéder ?

pierre estrem

Le README de PyAudio conseille soit de l’installer via apt (si tu as la chance d’utiliser Debian). Si tu es obligé de l’installer via pip le README te conseille d’installer les dépendances :

Alternatively, if the latest version of PyAudio is not available, install it using pip. Be sure to first install development libraries for portaudio19 and python3.

Si tu as la chance d’utiliser Debian, un simple apt install portaudio19-dev python3-dev devrait suffire.

Avec une base Debian, par chance oui.

Le paquet python3-pyaudio s’installe correctement.
Merci pour ce rappel.

Mais le petit script .py pour test échoue.

Je suis très curieux de savoir si les résultats sont probants même si
c’est en langue anglaise.

pierre estrem

python3-pyaudio

comment ?

Bonjour Julien,

Le script est celui-ci :

#!/usr/bin/python3.10

import speech_recognition as sr
import pyaudio

def listen():
     r = sr.Recognizer()
     mic = sr.Microphone(device_index=1)
     with mic as source:
         audio = r.listen(source, timeout=10)
         return r.recognize_google(audio)
print(listen())

listen()

Et j’obtiens :

 > ALSA lib pcm_dsnoop.c:601:(snd_pcm_dsnoop_open) unable to open slave
 > ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave
 > ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
 > ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM 
cards.pcm.center_lfe
 > ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
 > Cannot connect to server socket err = No such file or directory
 > Cannot connect to server request channel
 > jack server is not running or cannot be started
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > Cannot connect to server socket err = No such file or directory
 > Cannot connect to server request channel
 > jack server is not running or cannot be started
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
 > ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
 > ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
 > ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 
'card'
 > ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
 > ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 
'card'
 > ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave
 > Cannot connect to server socket err = No such file or directory
 > Cannot connect to server request channel
 > jack server is not running or cannot be started
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > ALSA lib pcm_dsnoop.c:601:(snd_pcm_dsnoop_open) unable to open slave
 > ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave
 > ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
 > ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM 
cards.pcm.center_lfe
 > ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
 > Cannot connect to server socket err = No such file or directory
 > Cannot connect to server request channel
 > jack server is not running or cannot be started
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > Cannot connect to server socket err = No such file or directory
 > Cannot connect to server request channel
 > jack server is not running or cannot be started
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
 > ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
 > ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
 > ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 
'card'
 > ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
 > ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 
'card'
 > ALSA lib pcm_dmix.c:1032:(snd_pcm_dmix_open) unable to open slave
 > Cannot connect to server socket err = No such file or directory
 > Cannot connect to server request channel
 > jack server is not running or cannot be started
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, 
skipping unlock
 > Expression 'parameters->channelCount <= maxChans' failed in 
'src/hostapi/alsa/pa_linux_alsa.c', line: 1514
 > Expression 'ValidateParameters( inputParameters, hostApi, 
StreamDirection_In )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', 
line: 2818
 > Traceback (most recent call last):
 >   File "/home/aciah/./test-speechrecognition.py", line 10, in listen
 >     audio = r.listen(source, timeout=10)
 >   File 
"/home/aciah/.local/lib/python3.10/site-packages/speech_recognition/__init__.py", 
line 465, in listen
 >     assert source.stream is not None, "Audio source must be entered 
before listening, see documentation for ``AudioSource``; are you using 
``source`` outside of a ``with`` statement?"
 > AssertionError: Audio source must be entered before listening, see 
documentation for ``AudioSource``; are you using ``source`` outside of a 
``with`` statement?
 >
 > During handling of the above exception, another exception occurred:
 >
 > Traceback (most recent call last):
 >   File "/home/aciah/./test-speechrecognition.py", line 12, in <module>
 >     print(listen())
 >   File "/home/aciah/./test-speechrecognition.py", line 9, in listen
 >     with mic as source:
 >   File 
"/home/aciah/.local/lib/python3.10/site-packages/speech_recognition/__init__.py", 
line 189, in __exit__
 >     self.stream.close()
 > AttributeError: 'NoneType' object has no attribute 'close'

Dans un autre exemple la ligne “index = pyaudio.get_device_count() - 1”
provoque l’erreur :

Traceback (most recent call last):
   File "/home/aciah/./test-speechrecognition2.py", line 6, in <module>
     index = pyaudio.get_device_count() - 1
AttributeError: module 'pyaudio' has no attribute 'get_device_count'

Problème de version de pyaudio ?

Merci Julien pour votre écoute :slight_smile:

Probable, c’est une bonne piste en tout cas.

Tu peux utiliser apt-cache policy python3-pyaudio pour savoir quelle version tu as, et fais bien attention à lire la doc de ladite version, par exemple via un apt install python-pyaudio-doc si leur doc en ligne ne permet pas de choisir la version facilement.