Friday, October 6, 2017

Google/Sphinx Speech Recognition using PyAudio and SpeechRecognition module on Raspberry Pi

The following steps show you how to test Google/Sphinx Speech Recognition using PyAudio and SpeechRecognition module on Raspberry Pi using a C-Media USB Microphone.

1. Test USB Microphone

1.A: Use "cat /proc/asound/cards" to check if C-Media USB Microphone is listed.
1.B: Use "alsamixer" to addjust USB Microphone gain
1.C: Use "arecord -D sysdefault:CARD=1 -duration=10 -f cd -vv ~/mic.wav" to record something into mic.wav
1.D: Use "aplay ./mic.wav -D sysdefault:CARD=0" to play mic.wave to verify USB Microphone works fine.

2. Install python3-pip and SpeechRecognition module
sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install SpeechRecognition

3. Install PyAudio
sudo apt-get install git
sudo git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
sudo apt-get install python3-dev
cd pyaudio
sudo python3 setup.py install

4. Install flac which would be used by Google Speech Recognition.
sudo apt-get install flac

5. Test PyAudio and SpeechRecognition module using Googole: Using the following python code saved in google.py and run "python3 google.py"

#!/usr/bin/env python3

import speech_recognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
r.energy_threshold = 4000

while True:
  with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

  try:
    print("The audio file contains: " + r.recognize_google(audio))

  except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")

  except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))

6. Setup pocketsphinx
sudo apt-get install swig
sudo apt-get install libpulse-dev
sudo pip3 install pocketsphinx


7. Test PyAudio and SpeechRecognition module using pocketsphinx: Using the following python code saved in sphinx.py and run "python3 sphinx.py"

#!/usr/bin/env python3

import speech_recognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
r.energy_threshold = 4000

while True:
  with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

  try:
    print("Sphinx thinks you said " + r.recognize_sphinx(audio))
  except sr.UnknownValueError:
    print("Sphinx could not understand audio")
  except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))



No comments:

Post a Comment