Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| pocketsphinx [Le 11/01/2014, 20:59] – [sphinxbase] mmaura | pocketsphinx [Le 11/09/2022, 11:43] (Version actuelle) – Suppression des espaces en fin de ligne (détecté et corrigé via le bot wiki-corrector (https://forum.ubuntu-fr.org/viewtopic.php?id=2067892) moths-art | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | {{tag> | ||
| <note important> | <note important> | ||
| + | ====== Pocketsphinx ====== | ||
| - | ====== Installation ====== | + | **Pocketsphinx** est une librairie permettant d' |
| - | ===== à partir | + | |
| - | <note warning> | + | ===== Installation ===== |
| + | ==== À partir des dépôts ==== | ||
| - | Installer les paquets **[[apt> | + | [[: |
| + | Ou dans un [[: | ||
| < | < | ||
| - | apt-get install python-pocketsphinx libpocketsphinx1 | + | sudo apt-get install python-pocketsphinx libpocketsphinx1 |
| </ | </ | ||
| - | ===== à partir des sources ===== | ||
| - | D' | + | ==== À partir des sources ==== |
| + | D' | ||
| < | < | ||
| sudo apt-get build-dep pocketsphinx | sudo apt-get build-dep pocketsphinx | ||
| - | sudo apt-get install bison python-pyaudio | + | sudo apt-get install bison python-pyaudio |
| </ | </ | ||
| - | ==== sphinxbase | + | === sphinxbase === |
| On récupère ensuite les sources de **sphinxbase-0.8** | On récupère ensuite les sources de **sphinxbase-0.8** | ||
| Ligne 28: | Ligne 31: | ||
| < | < | ||
| tar -xvzf sphinxbase-0.8.tar.gz | tar -xvzf sphinxbase-0.8.tar.gz | ||
| + | </ | ||
| + | On ce rend dans le dossier pour préparer l' | ||
| + | < | ||
| + | cd sphinxbase-0.8/ | ||
| </ | </ | ||
| On compile | On compile | ||
| + | |||
| + | < | ||
| + | ./configure --prefix=/ | ||
| + | make | ||
| + | sudo make install | ||
| + | </ | ||
| + | |||
| + | |||
| <note important> | <note important> | ||
| Si vous avez l' | Si vous avez l' | ||
| Ligne 39: | Ligne 54: | ||
| il faut régénérer le fichier | il faut régénérer le fichier | ||
| < | < | ||
| + | sudo apt-get install cython | ||
| mv python/ | mv python/ | ||
| </ | </ | ||
| + | puis relancer la compilation a partir de ./configure | ||
| </ | </ | ||
| + | <note tip>Par défaut au moment de la configuration de sphinxbase, si les librairies de compilation de pulseaudio sont installées elles seront utilisées. | ||
| + | Si comme moi vous devez utiliser Alsa, il faut supprimer la librairie pulseaudio : | ||
| < | < | ||
| - | ./ | + | sudo apt-get remove libpulse-dev |
| - | make | + | |
| - | sudo make install | + | |
| </ | </ | ||
| - | ==== pocketsphinx | + | </ |
| + | |||
| + | === pocketsphinx === | ||
| < | < | ||
| wget -O pocketsphinx-0.8.tar.gz http:// | wget -O pocketsphinx-0.8.tar.gz http:// | ||
| Ligne 58: | Ligne 78: | ||
| </ | </ | ||
| - | ===== modèles | + | === Modèles |
| Téléchargement: | Téléchargement: | ||
| < | < | ||
| - | wget -O lium_french_f0.tar.gz http:// | + | wget -O lium_french_f0.tar.gz http:// |
| tar -xvzf lium_french_f0.tar.gz | tar -xvzf lium_french_f0.tar.gz | ||
| cd lium_french_f0/ | cd lium_french_f0/ | ||
| - | sudo mkdir /usr/share/pocketsphinx/ | + | sudo mkdir -p `pkg-config --variable=modeldir |
| - | sudo mv * /usr/share/pocketsphinx/ | + | sudo mv * `pkg-config --variable=modeldir |
| - | wget -O french3g62K.lm.dmp.bz2 http:// | + | wget -O french3g62K.lm.dmp http:// |
| - | bzip2 -d french3g62K.lm.dmp.bz2 | + | sudo mkdir -p `pkg-config --variable=modeldir pocketsphinx`/ |
| - | sudo mv french3g62K.lm.dmp | + | sudo mv french3g62K.lm.dmp |
| - | wget -O frenchWords62K.dic http:// | + | wget -O frenchWords62K.dic http:// |
| - | sudo mv frenchWords62K.dic | + | sudo mv frenchWords62K.dic |
| + | </code> | ||
| + | ===== Bon alors, est ce que ça marche ? ===== | ||
| + | ==== pocketsphinx_continuous ==== | ||
| + | Vous pouvez alors lancer la reconnaissance vocale depuis le micro directement avec la commande pocketsphinx_continuous. | ||
| + | Il faut simplement préciser un dictionnaire à utiliser, un modèle de langage et un modèle de Markov caché (Hidden Markov Model ou HMM). | ||
| + | Si vous avez installé pocketsphinx avec le gestionnaire de paquets, le répertoire contenant les modèles est / | ||
| + | Pour utiliser les modèles en français que vous venez de télécharger en suivant les instructions ci-dessus, il faut exécuter la commande : | ||
| + | < | ||
| + | pocketsphinx_continuous -dict / | ||
| + | </ | ||
| - | wget -O lium_french_f2.tar.gz http://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/ | + | Si vous l'avez compilé depuis les sources comme indiqué plus haut, le répertoire contenant les modèles est /usr/local/share/pocketsphinx/model/ . Il faudra alors exécuter la commande : |
| + | < | ||
| + | pocketsphinx_continuous -dict / | ||
| </ | </ | ||
| - | ====== références ====== | ||
| - | Site officiel | + | Plutôt que d' |
| + | |||
| + | |||
| + | ==== scripts python ==== | ||
| + | === lister tous les périphériques audio === | ||
| + | |||
| + | [[http:// | ||
| + | <code language=' | ||
| + | # | ||
| + | """ | ||
| + | PyAudio Example: | ||
| + | |||
| + | Query and print PortAudio HostAPIs, Devices, and their | ||
| + | support rates. | ||
| + | """ | ||
| + | |||
| + | import pyaudio | ||
| + | |||
| + | standard_sample_rates = [8000.0, 9600.0, 11025.0, 12000.0, | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | p = pyaudio.PyAudio() | ||
| + | max_apis = p.get_host_api_count() | ||
| + | max_devs = p.get_device_count() | ||
| + | |||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | print(" | ||
| + | |||
| + | print(" | ||
| + | |||
| + | for i in range(max_apis): | ||
| + | apiinfo = p.get_host_api_info_by_index(i) | ||
| + | for k in list(apiinfo.items()): | ||
| + | print(" | ||
| + | print(" | ||
| + | |||
| + | print(" | ||
| + | |||
| + | for i in range(max_devs): | ||
| + | devinfo = p.get_device_info_by_index(i) | ||
| + | |||
| + | # print out device parameters | ||
| + | for k in list(devinfo.items()): | ||
| + | name, value = k | ||
| + | |||
| + | # if host API, then get friendly name | ||
| + | |||
| + | if name == ' | ||
| + | value = str(value) + \ | ||
| + | " (%s)" % p.get_host_api_info_by_index(k[1])[' | ||
| + | print(" | ||
| + | |||
| + | # print out supported format rates | ||
| + | |||
| + | input_supported_rates = [] | ||
| + | output_supported_rates = [] | ||
| + | full_duplex_rates = [] | ||
| + | |||
| + | for f in standard_sample_rates: | ||
| + | |||
| + | if devinfo[' | ||
| + | try: | ||
| + | if p.is_format_supported( | ||
| + | f, | ||
| + | input_device = devinfo[' | ||
| + | input_channels = devinfo[' | ||
| + | input_format = pyaudio.paInt16): | ||
| + | input_supported_rates.append(f) | ||
| + | except ValueError: | ||
| + | pass | ||
| + | |||
| + | if devinfo[' | ||
| + | try: | ||
| + | if p.is_format_supported( | ||
| + | f, | ||
| + | output_device = devinfo[' | ||
| + | output_channels = devinfo[' | ||
| + | output_format = pyaudio.paInt16): | ||
| + | output_supported_rates.append(f) | ||
| + | except ValueError: | ||
| + | pass | ||
| + | |||
| + | if (devinfo[' | ||
| + | | ||
| + | try: | ||
| + | if p.is_format_supported( | ||
| + | f, | ||
| + | input_device = devinfo[' | ||
| + | input_channels = devinfo[' | ||
| + | input_format = pyaudio.paInt16, | ||
| + | output_device = devinfo[' | ||
| + | output_channels = devinfo[' | ||
| + | output_format = pyaudio.paInt16): | ||
| + | full_duplex_rates.append(f) | ||
| + | except ValueError: | ||
| + | pass | ||
| + | |||
| + | if len(input_supported_rates): | ||
| + | print(" | ||
| + | if len(output_supported_rates): | ||
| + | print(" | ||
| + | if len(full_duplex_rates): | ||
| + | print(" | ||
| + | |||
| + | print(" | ||
| + | |||
| + | print(" | ||
| + | try: | ||
| + | def_index = p.get_default_input_device_info()[' | ||
| + | print(" | ||
| + | devinfo = p.get_device_info_by_index(def_index) | ||
| + | for k in list(devinfo.items()): | ||
| + | name, value = k | ||
| + | if name == ' | ||
| + | value = str(value) + \ | ||
| + | " (%s)" % p.get_host_api_info_by_index(k[1])[' | ||
| + | print(" | ||
| + | print(" | ||
| + | except IOError as e: | ||
| + | print(" | ||
| + | |||
| + | try: | ||
| + | def_index = p.get_default_output_device_info()[' | ||
| + | print(" | ||
| + | devinfo = p.get_device_info_by_index(def_index) | ||
| + | for k in list(devinfo.items()): | ||
| + | name, value = k | ||
| + | if name == ' | ||
| + | value = str(value) + \ | ||
| + | " (%s)" % p.get_host_api_info_by_index(k[1])[' | ||
| + | print(" | ||
| + | print(" | ||
| + | except IOError as e: | ||
| + | print(" | ||
| + | |||
| + | p.terminate() | ||
| + | |||
| + | </ | ||
| + | === essayer de faire une reconnaissance vocale === | ||
| + | |||
| + | [[http://pythonism.wordpress.com/2013/06/ | ||
| + | |||
| + | <code language=' | ||
| + | # | ||
| + | |||
| + | import sys,os | ||
| + | import pyaudio | ||
| + | import wave | ||
| + | |||
| + | hmdir = "/ | ||
| + | lmd = "/ | ||
| + | dictd = "/ | ||
| + | |||
| + | def decodeSpeech(hmmd, | ||
| + | |||
| + | import pocketsphinx as ps | ||
| + | import sphinxbase | ||
| + | |||
| + | speechRec = ps.Decoder(hmm = hmmd, lm = lmdir, dict = dictp) | ||
| + | wavFile = file(wavfile,' | ||
| + | wavFile.seek(44) | ||
| + | speechRec.decode_raw(wavFile) | ||
| + | result = speechRec.get_hyp() | ||
| + | |||
| + | return result[0] | ||
| + | |||
| + | #CHUNK = 1024 | ||
| + | CHUNK = 512 | ||
| + | #FORMAT = pyaudio.paInt16 | ||
| + | FORMAT = pyaudio.paALSA | ||
| + | CHANNELS = 1 | ||
| + | RATE = 16000 | ||
| + | #RATE = 44100 | ||
| + | RECORD_SECONDS = 10 | ||
| + | |||
| + | for x in range(10): | ||
| + | fn = " | ||
| + | p = pyaudio.PyAudio() | ||
| + | stream = p.open(format=FORMAT, | ||
| + | print(" | ||
| + | frames = [] | ||
| + | print str(RATE / CHUNK * RECORD_SECONDS) + " size\n" | ||
| + | for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): | ||
| + | data = stream.read(CHUNK) | ||
| + | frames.append(data) | ||
| + | print(" | ||
| + | stream.stop_stream() | ||
| + | stream.close() | ||
| + | wf = wave.open(fn, | ||
| + | wf.setnchannels(CHANNELS) | ||
| + | wf.setsampwidth(p.get_sample_size(FORMAT)) | ||
| + | p.terminate() | ||
| + | wf.setframerate(RATE) | ||
| + | wf.writeframes(b'' | ||
| + | wf.close() | ||
| + | wavfile = fn | ||
| + | recognised = decodeSpeech(hmdir, | ||
| + | print recognised | ||
| + | cm = ' | ||
| + | os.system(cm) | ||
| + | </code> | ||
| + | ===== Références ===== | ||
| - | Python audio : http:// | + | * Site officiel : http:// |
| + | * Github : https:// | ||
| + | * Python audio : http:// | ||
| + | * http:// | ||
| + | * installer sur raspberry pi : | ||
| - | Exemple en python: http:// | ||
| - | http:// | ||
