In the last post I did a quick sopare intro and we controlled a robotic arm via voice. Today I want to focus on simple one word commands and how to add custom features to sopare. And because I need something to control I use a smart mirror web interface which is one of my next projects I’m working on. It’s not yet a mirror but the frame, screen, a usb mic and some more parts are already assembled and I think this is a perfect example how to use sopare with one word commands.
So, let’s start with the requirements. Obviously, you need a Raspberry Pi 2/3. And a microphone. All my sopare systems are using USB microphones. Here is a list that I’m using for different objectives:
- Blue Microphones Snowball USB Mic (light control)
- Samson Meteor Mic USB Studio (robotic arm control)
- Foxnovo Portable USB 2.0 Mic (magic mirror control)
And of course, you need sopare in the latest version. Before we start the training, it’s a good time to check and adjust the mic input. As I’m working most of the time with a headless Pi, „alsamixer“ is my preferred tool. Just make sure that the input is not too high and not too low. I get good results with mic input levels around the 2/3 mark when the mic input level is not yet in the red sector (see the video for a visual reference).
There is one more thing to check and to adjust: The „THRESHOLD“ value from the sopare/config.py file. This threshold defines when sopare „starts listening“. Or, in other words, everything below this threshold should be noise or environment sound. But for the training or, later on, the voice recognition, the threshold should be low enough to trigger the processing. This depends a lot on your environment. To figure out your ideal threshold you can start sopare in debug/verbose mode and just take a look what’s happening. If you see a lot of lines scrolling down the screen and you don’t say your voice command than the threshold is too low. If you start talking and nothing happens, the threshold is too high. Find your personal threshold by testing around with the following command:
./sopare.py -l -v
and adjust the config setting accordingly.
Now we are ready for training. Training is easy as eating cake. In my case I want to train a word and sopare should recognize the word with the identifier „main“. All I have to do is start the training by running the following command:
./sopare.py -t main
When you see the line „start endless recording“ on the screen it’s time to train by saying a word or playing a tone of making the sound that should be trained. Repeat this at least 3 times. After training, we need to compile the dictionary which includes the information about the trained sound. This can be done with the command:
We are now ready to test the trained command(s):
You should test if your trained command is recognized. Normally you should get quite far with the default settings. Short one word commands will result in false positives with the default settings. How to gain more precision is something for one of the next posts as this topic is quite complex and requires some more background which does not fit in this basic usage post.
CTRL-c to terminate the process and return to the command line. In the rare case that the process does not terminates completely, find out the PID with the command
ps aux | grep python
and kill the process(es) via
kill -3 PID
Now you may want to ask: How can I control stuff with sopare? Where does my code goes? Good questions. Sopare comes with a simple plugin interface. Just take a look into the directory „./plugins“. You see the standard plugin „print/__init__.py“. This plugin only contains two (2) lines of code that are relevant in terms of coding:
def run(readable_results, data, rawbuf): print readable_results
The interesting part for the basic usage is the list value „readable_results“. In my case the value contains something like
If you simply copy the print plugin you can add your own code and control stuff. Offline and in real time. In any language you want. Or any sound you want. Next time we will talk a bit more about configuration and customization but for today I have to stop. Have fun and tell me what you think and where/how you are using sopare 🙂