3 Jun 2013

Siri-like Raspberry Pi Voice Regnition Control System For Home Automation

This is a project on Siri-like voice recognition using Raspberry Pi, it's useful for home automation control system. I use three components for the project, code is mainly scrapped out from various Internet sources
  1. A speech-to-text component that will do the voice recognition
  2. Some “brains” to analyze the so captured text
  3. A text to speech component that will speak out the result from component 2
The hardware required is a Raspberry Pi with Internet connectivity and a USB microphone.  Pi is running the 2012-12-16-wheezy-raspbian image; I don’t have a USB microphone, but I have a USB webcam (Logitech V-UAV35) with in-built microphone, so that worked out fine without any driver installation.

This is a post explaining this project in detail: Raspberry Pi Voice Recognition Works Like Siri
Speech recognition for Raspberry Pi can be done in number of ways, but I thought the most elegant would be to use Google’s voice recognition functions. I used this bash script to get that part done (source):
arecord -D "plughw:1,0" -q -f cd -t wav | ffmpeg -loglevel panic -y -i - -ar 16000 -acodec flac file.flac  > /dev/null 2>&1
wget -q -U "Mozilla/5.0" --post-file file.flac --header "Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium" | cut -d\" -f12  >stt.txt
cat stt.txt
rm file.flac  > /dev/null 2>&1
..and then set it to executable:
chmod +x stt.sh
You may need to install ffmpeg
sudo apt-get install ffmpeg
So what this does is to record to a flac file from the USB microphone until you press Ctrl+C and then passes that file to Google for analysis, which in turn returns the recognized text. Lets give it a try:
It work pretty good even with my bad accent. The output is saved to stt.txt file.
Now onto the “brains” section, this is with no doubt a task for Wolfram Aplha. I used Python to interface with it, there is already a library to use. It is pretty easy to install, just follow the instructions in the link. I had to get an API key, which is a 2 minute task and gives you 2000 queries a month.
import wolframalpha
import sys
#Get a free API key here http://products.wolframalpha.com/api/
#I may disable this key if I see lots of abuse

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"
    print texts
    print "I am not sure"
.. and lets try it out with the questions that keep me up at night:

yep, brains are there. Now to the last part: speaking that answer out. Sure enough, we use Google’s speech services again (source)
say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?tl=en&q=$*"; }
say $*
..you may need to “sudo apt-get install mplayer” first..
It sounds pretty cool indeed.
So finally a small script to put these to work together:
echo Please speak now and press Ctrl+C when done
./tts.sh $(./wa.py $(cat stt.txt))
So overall a fun project, maybe with some potential to use in home automation.. (4627)

2 Jun 2013

How to use Git and Gibhub on Raspberry Pi

How to use Git and GitHub on Raspberry Pi

Git is a free and open source distributed version control system. Let’s take that apart bit by bit.
Git is free and open source. You don’t need to pay to use it, and the source code is freely available. In practice that means that many pairs of eyes will be scanning the code and reading any bug reports, so defects in the code are likely to be found and fixed quickly.
But what does git do?
The fact that it’s a version control system means that it can help you keep track of files that are frequently changed. Most version control systems maintain a history of changes and make it easy for users to compare versions, to load previous versions, and to add new versions as appropriate. Most software developers use a version control system of some sort, and many use git.
Git has a range of features which make it attractive to developers, and one of those features is the fact that it supports distributed development. In other words, git is designed for use by teams in which the developers work in more than one location. 
You don’t need to know about we how we use git in our development process. The thing that matters is that you can use git, simply and easily, to get the latest version of our software on your Raspberry Pi.
Git expects developers to keep all the files for a project or library together in something called a repository. That’s just a grand name for a collection of files that belong together. All of our libraries are stored in git repositories, and those repositories are stored on GitHub.


Hopefully that’s given you a feel for git and why we use it, but what about GitHub?
GitHub is a web-based service for people who want to use git. It’s widely used by teams who want to make some or all of their work publicly available under an open source license. Since that’s what we do, we feel that GitHub is a natural choice for us to store our code.
If you want to keep up-to-date with the code we’ve published on GitHub, you will need to install and use git.

Installing Git on your Pi

To install git, just pen a terminal window by typing Ctrls-Alt-T and invoke
sudo apt-get install git-core
You’ll be asked for your password and so long as your Pi has access to the internet and the debian RasPi repository is on-line you’ll see some activity which will show what’s going on as git is installed.
Once that stops you’ll be ready to go.

Using git to download our code

When you first install a local copy of a repository using git, you do so by creating what’s called a clone of the repository.
In order to install a local copy of the repository, click on the button marked ‘Git Read-Only’ and copy the text of the URI on its right. In our case, that’s git://github.com/quick2wire/quick2wire-python-api.git
It’s probably best to keep all your GitHub-based repositores in one place, so you may want to create a directory called git within your home directory. (In a command window, type cd ~
and then type mkdir git)
When you have done so, open a command window if necessary, change to your git directory by typing cd ~/git and create a copy of the GitHub content by typing
git clone git://github.com/quick2wire/quick2wire-python-api.git
Git will tell you that it’s created an empty repository and will then pull down all the current files from the repository on GitHub.
The README file on Git should tell you what do do to install the software in the repository.  in our case the recommended approach is to add the location of your new cloned repository to your Python Path.
Now you can use the software.
But how do you keep it up to date?

Keeping current

If you take  look at the GitHub page for a repository you can see when the files were last changed. If you open an account at GitHub (which has a free account option) you can chose to watch one or more repositories, in which case you will get notified whenever the repsoitory changes.
If a repository you’ve cloned has changed since you last updated it, it’s really easy to get back up-to-date. In a command window, change to the directory in which you cloned the repository. In our case, you’d typecd ~/git/quick2wire-python-api in a command window. Now just type git pull and git will update your repository so that it’s an exact copy of the current version on GitHub.
Depending on the way type of software in the repository, you may or may not need to repeat the installation procedure. If you’ve updated out gpio-admin repository, you’ll need to run through the installation process again, as described in the README for that repository in GitHub. In the case of the quick2wire-python-api package, you’ve already placed the code on your PythonPath so you should be ready to use the updated library without doing anything else.


You can use most code on GitHub by downloading, unpacking and installing an archive containing the code.
If the code is changing fast, and you want to stay up-to-date you may prefer to install git, use the clone command to create a local copy of the repository, and use the git pull command to refresh your copy when the version on GitHub has changed.