issue153:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue153:python [2020/02/01 17:22] – créée d52fr | issue153:python [2020/02/05 20:16] (Version actuelle) – d52fr | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | The other morning, while I was getting my shower, my mind went to a fairly dark place as it often does. If most people fall in the shower, or outside, or going down the stairs, they will end up with a bruise and be achy for a day or two. If I fall however, there is a very high probability that I will be paralysed or worse. And, as usual, when I get into that place, I wonder how I'll be able to continue my writing, and programming, | + | **The other morning, while I was getting my shower, my mind went to a fairly dark place as it often does. If most people fall in the shower, or outside, or going down the stairs, they will end up with a bruise and be achy for a day or two. If I fall however, there is a very high probability that I will be paralysed or worse. And, as usual, when I get into that place, I wonder how I'll be able to continue my writing, and programming, |
- | Luckily, today we have Siri, Amazon Alexa, Google Assistant, and more. Almost every smartphone has some kind of speech recognition. There are many pre-made packages out there for Linux and other operating systems. But I wanted to see what could be done via Python. | + | Luckily, today we have Siri, Amazon Alexa, Google Assistant, and more. Almost every smartphone has some kind of speech recognition. There are many pre-made packages out there for Linux and other operating systems. But I wanted to see what could be done via Python.** |
- | First, I want to hit the pause button and share a little history of speech recognition with you. Back when I was a child, when rainbows were in black and white, and we had to watch TV by candlelight, because there wasn’t any electricity (not really, but it confuses children to no end), computers were just getting started. In 1952, Bell labs created the Audrey system, which was able to understand | + | L' |
- | Enough of ancient history. Push the play button! | + | Heureusement, |
- | After a little web browsing, I found a library for Python called, surprisingly enough, SpeechRecognition. It can be installed via pip... | + | **First, I want to hit the pause button and share a little history of speech recognition with you. Back when I was a child, when rainbows were in black and white, and we had to watch TV by candlelight, |
+ | |||
+ | Enough of ancient history. Push the play button!** | ||
+ | |||
+ | D' | ||
+ | |||
+ | Ça suffit pour l' | ||
+ | |||
+ | **After a little web browsing, I found a library for Python called, surprisingly enough, SpeechRecognition. It can be installed via pip... | ||
pip install SpeechRecognition | pip install SpeechRecognition | ||
Ligne 16: | Ligne 24: | ||
I’ve “borrowed” the following snippet from the repository site... | I’ve “borrowed” the following snippet from the repository site... | ||
+ | |||
+ | “… with support for several engines and APIs, online and offline. | ||
+ | |||
+ | Speech recognition engine/API support: | ||
+ | |||
+ | CMU Sphinx (works offline) | ||
+ | Google Speech Recognition | ||
+ | Google Cloud Speech API | ||
+ | Wit.ai | ||
+ | Microsoft Azure Speech | ||
+ | Microsoft Bing Voice Recognition (Deprecated) | ||
+ | Houndify API | ||
+ | IBM Speech to Text | ||
+ | Snowboy Hotword Detection (works offline)”** | ||
+ | |||
+ | Suite à quelques recherches sur le Web, j'ai trouvé une bibliothèque pour Python appelée, quelle surprise, SpeechRecognition (reconnaissance vocale). Elle peut être installée via pip... | ||
+ | |||
+ | pip install SpeechRecognition | ||
+ | |||
+ | Tout le code source peut être trouvé sur https:// | ||
+ | |||
+ | J'ai continué de l' | ||
+ | |||
+ | J'ai emprunté le bout de code suivant sur le site du dépôt... | ||
“… with support for several engines and APIs, online and offline. | “… with support for several engines and APIs, online and offline. | ||
Ligne 31: | Ligne 63: | ||
Snowboy Hotword Detection (works offline)” | Snowboy Hotword Detection (works offline)” | ||
- | Now, there are somethings | + | **Now, there are some things |
+ | |||
+ | Once I saw the line "To quickly try it out, run python -m speech_recognition after installing.", | ||
+ | |||
+ | Maintenant, il y a des choses qui doivent être dites ici. La plupart des moteurs en ligne nécessitent que vous vous enregistriez comme utilisateur pour obtenir des clés permettant d'y accéder, avec de possibles coûts. Les seuls services hors ligne qui sont actuellement supportés sont CMU Sphinx (j'en parlerai dans un petit moment) et Snowboy. Si vous voulez voir exactement ce qui est nécessaire pour chaque moteur, téléchargez le code source depuis le dépôt GitHub et regardez dans le fichier __init__.py | ||
- | Once I saw the line "To quickly try it out, run python -m speech_recognition | + | Une fois que j'ai vu la ligne « pour un essai rapide, lancez |
- | “On Debian-derived Linux distributions (like Ubuntu and Mint), install PyAudio using APT: execute sudo apt-get install python-pyaudio python3-pyaudio in a terminal.” | + | **“On Debian-derived Linux distributions (like Ubuntu and Mint), install PyAudio using APT: execute sudo apt-get install python-pyaudio python3-pyaudio in a terminal.” |
I immediately copied the apt-get line from the bullet point and ran it in a terminal. I noticed that I could use pip to install the actual library. HOWEVER, because I was being stupid, I didn't notice the caveat below that. | I immediately copied the apt-get line from the bullet point and ran it in a terminal. I noticed that I could use pip to install the actual library. HOWEVER, because I was being stupid, I didn't notice the caveat below that. | ||
- | “If the version in the repositories is too old, install the latest release using Pip: execute sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && sudo pip install pyaudio (replace pip with pip3 if using Python 3).” | + | “If the version in the repositories is too old, install the latest release using Pip: execute sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && sudo pip install pyaudio (replace pip with pip3 if using Python 3).”** |
- | When I tried to use pip, I got a tonne of error messages. That's because I had not installed portaudio19-dev with the rest of it. | + | « Sur les distributions Linux dérivées de Debian (comme Ubuntu et Mint), installez PyAudio en utilisant APT : exécutez sudo apt-get install python-pyaudio python3-pyaudio dans un terminal. » |
+ | |||
+ | J'ai immédiatement copié la ligne apt-get depuis l' | ||
+ | |||
+ | « Si la version dans les dépôts est trop ancienne, installez la dernière publication en utilisant Pip : exécutez sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && sudo pip install pyaudio (remplacez pip par pip3 si vous utilisez Python3). » | ||
+ | |||
+ | **When I tried to use pip, I got a tonne of error messages. That's because I had not installed portaudio19-dev with the rest of it. | ||
I did another apt install, and ran the pip install command. It worked! | I did another apt install, and ran the pip install command. It worked! | ||
So to get it all on your system, here’s what you want to do… | So to get it all on your system, here’s what you want to do… | ||
+ | |||
+ | $ sudo apt-get install portaudio19-dev python-all-dev python3-all-dev | ||
+ | |||
+ | $ pip3 install pyaudio** | ||
+ | |||
+ | Quand j'ai essayé d' | ||
+ | |||
+ | J'ai fait un autre apt install et lancé la commande pip install . Ça marchait ! | ||
+ | |||
+ | Aussi, pour tout avoir sur votre système, voici ce que vous avez à faire... | ||
$ sudo apt-get install portaudio19-dev python-all-dev python3-all-dev | $ sudo apt-get install portaudio19-dev python-all-dev python3-all-dev | ||
Ligne 51: | Ligne 103: | ||
$ pip3 install pyaudio | $ pip3 install pyaudio | ||
- | Now, you can give the program a try... | + | **Now, you can give the program a try... |
$ python -m speech_recognition | $ python -m speech_recognition | ||
Ligne 72: | Ligne 124: | ||
Got it! Now to recognize it... | Got it! Now to recognize it... | ||
You said alright the time has come for all good men to come to the aid of the party | You said alright the time has come for all good men to come to the aid of the party | ||
- | Say something! | + | Say something!** |
- | Ok. Color me impressed. The warning messages didn’t worry me, they actually piqued my interest about all the possibilities. I wasn’t really happy with having to hit < | + | Maintenant, nous pouvons essayer le programme... |
+ | |||
+ | $ python -m speech_recognition | ||
+ | |||
+ | ALSA lib pcm.c: | ||
+ | ALSA lib pcm.c: | ||
+ | ALSA lib pcm.c: | ||
+ | ALSA lib pcm_route.c: | ||
+ | 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:: | ||
+ | A moment of silence, please... (un peu de silence, s'il vous plaît...) | ||
+ | Set minimum energy threshold to 1071.7441188823814 | ||
+ | |||
+ | Dites quelque chose ! | ||
+ | Ça y est ! Maintenant pour le reconnaître... | ||
+ | Vous avez dit Ok l' | ||
+ | Dites quelque chose ! | ||
+ | Ça y est ! Maintenant pour le reconnaître... | ||
+ | Vous avez dit Ok l' | ||
+ | Dites quelque chose ! | ||
+ | |||
+ | **Ok. Color me impressed. The warning messages didn’t worry me, they actually piqued my interest about all the possibilities. I wasn’t really happy with having to hit < | ||
Digging into the distribution folder, I found __main__.py, | Digging into the distribution folder, I found __main__.py, | ||
Ligne 83: | Ligne 159: | ||
m = sr.Microphone() | m = sr.Microphone() | ||
- | First, we import the library and create instances of the Recognizer and the microphone objects. Next, we use a loop to continually check and adjust the microphone level... | + | First, we import the library and create instances of the Recognizer and the microphone objects. Next, we use a loop to continually check and adjust the microphone level...** |
+ | |||
+ | Bon ! Je suis impressionné. Les messages d' | ||
+ | |||
+ | En fouillant dans le répertoire de la distribution, | ||
+ | |||
+ | import speech_recognition as sr | ||
+ | |||
+ | r = sr.Recognizer() | ||
+ | m = sr.Microphone() | ||
+ | |||
+ | D' | ||
- | Notice that in this step, the Recognizer uses the Google Speech Recognition system. The line “if str is bytes:” checks to see if this is running under Python 2.x to properly print any unicode characters. Now we have printed out what the Recognizer THINKS was said. Next. we can check that with either the phrase “please quit” or “please stop” to programmatically end the program. I tried using a single word, but that never triggered. I’m guessing that the system just figured that the input was just noise. A side note here. What if you don’t want to use English as the language that you speak to the program with. What about Spanish or Norwegian or some other language. It’s covered! Change the line: | + | **Notice that in this step, the Recognizer uses the Google Speech Recognition system. The line “if str is bytes:” checks to see if this is running under Python 2.x to properly print any unicode characters. Now we have printed out what the Recognizer THINKS was said. Next. we can check that with either the phrase “please quit” or “please stop” to programmatically end the program. I tried using a single word, but that never triggered. I’m guessing that the system just figured that the input was just noise. A side note here. What if you don’t want to use English as the language that you speak to the program with. What about Spanish or Norwegian or some other language. It’s covered! Change the line: |
value = r.recognize_google(audio) | value = r.recognize_google(audio) | ||
To | To | ||
+ | |||
+ | value = r.recognize_google(audio, | ||
+ | |||
+ | Notez qu'à cette étape, Recognizer utilise le système de reconnaissance vocale de Google. La ligne « if str is bytes: » fait un contrôle pour voir si ça tourne sous Python 2.x pour imprimer correctement tous les caractères Unicode. Puis, nous avons imprimé ce que Recognizer PENSE a été dit. Ensuite, nous pouvons vérifier s'il y a la phrase, soit « please quit » (merci d' | ||
+ | |||
+ | value = r.recognize_google(audio) | ||
+ | |||
+ | en | ||
value = r.recognize_google(audio, | value = r.recognize_google(audio, | ||
- | For Brittish english, “no-NO” for Norwegian, or “es-AR” for Spanish (Argentina). You can check this link (https:// | + | **For Brittish english, “no-NO” for Norwegian, or “es-AR” for Spanish (Argentina). You can check this link (https:// |
Finally, we check for exceptions (bottom right) | Finally, we check for exceptions (bottom right) | ||
Ligne 99: | Ligne 194: | ||
So the bottom line is that we get a string (value) back from the Recognizer. What we do with that information, | So the bottom line is that we get a string (value) back from the Recognizer. What we do with that information, | ||
- | At this point, before I forget it, I promised a while ago to talk about PocketSphinx. There are a lot of people who find that it is not very reliable. I tried to get it to install and I have to admit, there were issues. | + | At this point, before I forget it, I promised a while ago to talk about PocketSphinx. There are a lot of people who find that it is not very reliable. I tried to get it to install and I have to admit, there were issues.** |
+ | |||
+ | pour l' | ||
+ | |||
+ | Enfin, nous vérifions les exceptions (en bas à droite). | ||
+ | |||
+ | Au bout du compte, nous récupérons une chaîne (valeur) venant de Recognizer. Ce que nous en faisons, actuellement, | ||
+ | |||
+ | À ce stade, avant que j' | ||
- | First, I suggest that if you want to try PocketSphinx, | + | **First, I suggest that if you want to try PocketSphinx, |
There is an example program that is provided with the source distribution that, at least for me, would not run. I kept getting an error starting with the line decoder = Decoder(config). I did a search and found a number of people having the same issue, but not much in the way of an answer. After digging for much longer than I should have, I found a reference to the MODELDIR config settings. After looking into my Python library folders, I found the site package for PocketSphinx. I realized that the MODELDIR and DATADIR statements were not being set properly in the example. They were: | There is an example program that is provided with the source distribution that, at least for me, would not run. I kept getting an error starting with the line decoder = Decoder(config). I did a search and found a number of people having the same issue, but not much in the way of an answer. After digging for much longer than I should have, I found a reference to the MODELDIR config settings. After looking into my Python library folders, I found the site package for PocketSphinx. I realized that the MODELDIR and DATADIR statements were not being set properly in the example. They were: | ||
Ligne 109: | Ligne 212: | ||
DATADIR = " | DATADIR = " | ||
- | but for me, they needed to be... | + | but for me, they needed to be...** |
+ | D' | ||
+ | Un exemple de programme est fourni avec la distribution des sources qui, au moins pour moi, ne fonctionnait pas. Je reçois systématiquement une erreur commençant par la ligne decoder = Decoder(config). J'ai fait une recherche et j'ai trouvé beaucoup de gens avec le même problème, mais peu avec une piste de solution. Après avoir creusé beaucoup plus que je n' | ||
- | MODELDIR = "/ | + | MODELDIR = " |
+ | |||
+ | DATADIR = " | ||
+ | |||
+ | mais, pour moi, elles auraient dû être... | ||
+ | |||
+ | **MODELDIR = "/ | ||
DATADIR = "/ | DATADIR = "/ | ||
- | The package needs absolute path statements to where pip installed PocketSphinx. This can be a major problem if you are using something like pyenv and have multiple instances of Python or if you wish to distribute an app you wrote using PocketSphinx. | + | **The package needs absolute path statements to where pip installed PocketSphinx. This can be a major problem if you are using something like pyenv and have multiple instances of Python or if you wish to distribute an app you wrote using PocketSphinx. |
The next problem showed up on the line: | The next problem showed up on the line: | ||
Ligne 127: | Ligne 238: | ||
config.set_string(' | config.set_string(' | ||
- | After these changes were made and saved, the example program worked. | + | After these changes were made and saved, the example program worked.** |
- | You might wonder at this point, “Ok, so how do we actually do something with the data we’ve received? | + | MODELDIR = "/ |
+ | |||
+ | DATADIR = "/ | ||
+ | |||
+ | Le paquet nécessite une déclaration avec des chemins absolus vers l' | ||
+ | |||
+ | Le problème suivant se présente dans la ligne : | ||
+ | |||
+ | config.set_string(' | ||
+ | |||
+ | Celle-ci est incorrecte au regard de l' | ||
+ | |||
+ | config.set_string(' | ||
+ | |||
+ | Une fois ces modifications faites et enregistrées, | ||
+ | |||
+ | **You might wonder at this point, “Ok, so how do we actually do something with the data we’ve received? | ||
If you remember near the top of the article, one of the engines that is supported by the SpeechRecogintion library is Wit.ai . This is an interesting site. Basically, you provide speech or text to their API and it tries to match that input to something you have told the system that you expect the end user to enter. For example, let’s say that you want your end user to say things that would be along the lines of home automation like turning on or off a light, asking what the temperature is outside, changing the thermostat, and so on. | If you remember near the top of the article, one of the engines that is supported by the SpeechRecogintion library is Wit.ai . This is an interesting site. Basically, you provide speech or text to their API and it tries to match that input to something you have told the system that you expect the end user to enter. For example, let’s say that you want your end user to say things that would be along the lines of home automation like turning on or off a light, asking what the temperature is outside, changing the thermostat, and so on. | ||
- | Check out https:// | + | Check out https:// |
- | One other thing. While I was digging around on the web to get info to do this article, I found that Google Chrome now can support voice commands. I haven’t tried it yet, but it looks very interesting. From the website, it says “Use the magic of speech recognition to write emails and documents in Google Chrome. Dictation accurately transcribes your speech to text in real time. You can add paragraphs, punctuation marks, and even smileys using voice commands.” Check out the site at https:// | + | Peut-être, qu'en ce moment, vous vous demandez « Bon. Comment faisons-nous vraiment quelque chose des données que nous avons reçues ? » C'est en tant que tel, une question à réponse ouverte, qui est en fait hors de sujet pour cet article. CEPENDANT, je peux vous indiquer une direction intéressante. |
+ | |||
+ | Si vous vous souvenez du haut de cet article, un des moteurs qui est supporté par la bibliothèque SpeechRecognition est Wit.ai. C'est un site intéressant. En gros, vous fournissez de la parole ou un texte à leur API et elle essaie de faire correspondre cette entrée à quelque chose que vous avez dit au système que vous vous attendez à ce que l' | ||
+ | |||
+ | Regardez sur https:// | ||
+ | |||
+ | **One other thing. While I was digging around on the web to get info to do this article, I found that Google Chrome now can support voice commands. I haven’t tried it yet, but it looks very interesting. From the website, it says “Use the magic of speech recognition to write emails and documents in Google Chrome. Dictation accurately transcribes your speech to text in real time. You can add paragraphs, punctuation marks, and even smileys using voice commands.” Check out the site at https:// | ||
The code is, as always, on Pastebin at: https:// | The code is, as always, on Pastebin at: https:// | ||
- | Until next time, keep coding! | + | Until next time, keep coding!** |
+ | |||
+ | Autre chose. Pendant que je cherchais sur le Web pour trouver des info pour écrire cet article, j'ai découvert que Google Chrome supporte maintenant les commandes vocales. Je ne l'ai pas encore essayé, mais ça semble intéressant. Sur le site Web, il est dit « Utilisez la magie de la reconnaissance vocale pour écrire des mails ou des documents dans Google Chrome. Dictation retranscrit précisément votre parole en texte en temps réel. Vous pouvez ajouter des paragraphes, | ||
+ | |||
+ | Comme toujours, le code est sur Pastebin, à https:// | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | P 20, encart en haut à droite, 2 lignes noires : | ||
+ | |||
+ | Maintenant, nous créons une autre boucle pour obtenir quelque chose qui ressemble à des paroles dans un micro... | ||
+ | |||
+ | Il continue à écouter jusqu' | ||
+ | |||
+ | P 21, encart en bas à droite, 1 ligne noire : | ||
+ | Ça parait très facile, mais ça ne l'est pas. Voici un échantillon du programme en train de tourner (j'ai enlevé les messages d' |
issue153/python.1580574146.txt.gz · Dernière modification : 2020/02/01 17:22 de d52fr