Raspberry Pi is an excellent automation control unit, and we can use it to build a voice recognition feature in order to make it as a voice automation control unit. Yeah, is it very interesting ! In the following, I will show you the script to build voice feature, including converting speech to text, converting text to speech, auto-reply a text question.
Script to convert Speech to Text
I take the benefit of using google speech recognition ver 2 feature and arecord feature of Raspberry Pi. To remind that you should apply your google api key for usage in this script, as below:
arecord -D plughw:1,0 -f cd -t wav -r 16000 –duration=4 test.wav
avconv -i test.wav -y -ar 16000 -ac 1 test.flac
wget -q -U “Mozilla/5.0″ –post-file test.flac –header “Content-Type: audio/x-flac; rate=16000″ -O – “https://www.google.com/speech-api/v2/recognize?client=chromium&lang=en_US&key=AIzaSyB0RJilwaAhMpftgmgRhgEzd4lZnia1MwQ” |cut -d\” -f8 >stt.txt
echo “You said: “
The screen dump result to run speech2text.sh program is as below:
Script to Auto-Reply a Query
It is a python program using Wolframalpha’s API add-on tools to process a question as below script, and you should apply a app ID from http://products.wolframalpha.com/api/:
# Get a free API key here http://products.wolframalpha.com/api/
# This is a fake ID, go and get your own, instructions on my blog.
client = wolframalpha.Client(app_id)
query = ‘ ‘.join(sys.argv[1:])
res = client.query(query)
if len(res.pods) > 0:
texts = “”
pod = res.pods
texts = pod.text
texts = “I have no answer for that”
# to skip ascii character in case of error
texts = texts.encode(‘ascii’, ‘ignore’)
print(“Sorry, I am not sure.”)
To run the python program as script –> python3 queryprocess.py “What is your name”, then we can get a reply result as “My name is Walfram|Alpha.”, as below screen dump.
Convert Text to Speech
We can use Espeak utility to convert text to speech under Raspberry Pi. Its installation is very simple as below:
$ sudo apt-get install espeak
After installation, you can run the espeak program to speech, for example,
$ espeak "Hello World"
Question and Answer Program with Speech and Voice Reply
I develop a script to combine the above three program into one, so that you can use it to speech a question and wait for a voice answer. Let’s see the script of main.sh program as below:
echo “Recording… Press Ctrl+C to Stop.”
./speech2text.sh > /dev/null 2>&1
echo “Me: “ $QUESTION
python3 queryprocess.py $QUESTION > ans1.txt
ANSWER=$(cut -c3- ans1.txt)
ANSWER1=$(echo “$ANSWER” | sed -e ‘s/\\n/ /g’)
echo “Robot: “ $ANSWER2
espeak “$ANSWER2″ > /dev/null 2>&1
To run this program as script –> ./main.sh, then we can get a reply result as below screen dump, and with voice reply, too.