The SHEP AI Project

Home About Downloads Contact

Setting up a voice recognition Raspberry Pi AI

Posted 10/August/2020

Overview

In this tutorial we will be setting up a Raspberry Pi with a microphone and speaker to make your very own chatbot AI. We will be installing the needed drivers and software and talking about how you can take it further.
You will need a Raspberry Pi, a means of internet connection, and the respeaker 2-mic hat.

Installation

Firstly, we will need to install the software which provides us with audio tools.

sudo apt install pulseaudio
sudo reboot

After rebooting we can start installing our hardware card for sound. I am using the 2 mic respeaker for its inbuilt high-performance microphones designed for AI projects. It also offers speaker interfaces and an audio jack for the microphone.
sudo apt-get update
sudo apt-get upgrade
git clone https://github.com/respeaker/seeed-voicecard.git
cd seeed-voicecard
sudo ./install.sh --compat-kernel
reboot

If you experience errors installing just perform the action again. A way to avoid errors is to have a strong internet connection. My dongle struggled as my internet connection was not the best, therefore I used ethernet which worked a lot better for the install.
If using the newest raspberry Pi OS distribution this may downgrade some of your drivers to work with. This didn’t change anything for me when it downgraded my drivers – but its something to bare in mind if you already have specific hardware running on your Pi.
Once this is installed you should be able to see your card using arecord -l and aplay -l. If you cannot see your card, then the code has not installed, and you will need to do it again.
We will then want to install some libraries.
Sudo pip3 install pyaudio
Sudo pip3 install speechRecognition
Sudo apt-get install flac

Then we can try out what we have done so far using the following code:
m=Microphone()
r=Recognizer()
with m as source:
a=r.listen(source)
print(r.recognize_google(a))

If you are finding this to be slow then you can adjust the listening parameters. I find I get a lot of error messages about jack server when I call in the microphone. This hasn’t affected the ability for the code to recognize audio so I tend to ignore it.
If you are struggling to get audio to listen and recognize, add the following lines in underneath the with m as source. This will adjust the audio threshold to not wait around listening to background noise. The print will tell you when to talk.

r.adjust_for_ambient_noise(source)
print(“>”)



We will install espeak to give our AI audio output. This is done through
Sudo apt-get install espeak
Finally, we will install the nltk library and the data for it to use. This gives a database for our AI to recognize information to.
sudo pip3 install nltk
sudo python3
>>>import nltk
>>>nltk.download()

Make sure to download all
These are all the libraries installed, now we will install the AI code.
You can do this manually at shepai.github.io/AIlib.html, or you can type into your terminal the following lines. Both require unzipping in a file path of your choosing.
wget https://shepai.github.io/downloads/V0.0.9.zip

Writing code

Once we have installed all the libraries and drivers, we can then write our simple input output code.
from AI import CB
import speech_recognition as sr
import os
import sys
systemPath=sys.path[0]+"/"
m=sr.Microphone()
r=sr.Recognizer()
def INPUT(): #input method
    with r as source: #gather audio
        r.adjust_for_ambient_noise(source)
        print(">")
        audio=r.listen(source)
    try:
        return r.recognize_google(audio)
    except:
        return ""
def OUTPUT(string): #output to user
    print(String)
    os.system('espeak "'+string+'" 2>/dev/null')

bot=CB(systemPath+"testCB/")
while True:
    UI=INPUT()
    if UI!="":
        print(UI)
        OUTPUT(bot.chat(UI))
This code will chat with the bot we have created using the hardware.