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:

echo “Recording…”
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

echo “Processing…”
wget -q -U “Mozilla/5.0″ –post-file test.flac –header “Content-Type: audio/x-flac; rate=16000″ -O – “” |cut -d\” -f8 >stt.txt
echo “You said: “
value=`cat stt.txt`
echo “$value”

The screen dump result to run 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

import wolframalpha
import sys

# Get a free API key here
# 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[1]
    if pod.text:
        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 “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 program as below:

echo “Recording… Press Ctrl+C to Stop.”
./ > /dev/null 2>&1
QUESTION=$(cat stt.txt)
echo “Me: “ $QUESTION
python3 $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 –> ./, then we can get a reply result as below screen dump, and with voice reply, too.


Reference Document:

Leave a Reply

Your email address will not be published.

Solve : *
2 + 21 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>