Maker Pro
Raspberry Pi

Make a Voice-Controlled Fridge List Using Raspberry Pi

May 21, 2018 by Robin Mitchell
Share
banner

Make this voice-controlled fridge list using a Raspberry Pi and Python to help you remember what you have to eat!

Hardware

Software

1 Python 3
1 Speech Recognition Python library
1 PyAudio Python library

One massive problem I face is food waste. It's not that I cook more than I can eat or needlessly throw good food away, it’s that I completely forget that I bought something, and then it spoils. In this Maker Pro Project, we will use a Raspberry Pi, Python, and voice recognition to create a system that adds items to a list of what’s in the fridge!Write an introduction for your project

Install Python Libraries and Configure Audio

Before we can run our Python program, we need to install two libraries: Speech Recognition and PyAudio. To install these two libraries, you need to run the following two commands in your terminal window:

$ sudo pip install SpeechRecognition
$ sudo pip install PyAudio

If you are having trouble installing PyAudio, you can install it using the following series of instructions:

$ sudo apt-get install git
$ sudo git clone http://people.csail.mit.edu/hubert/git/pyaudio.git
$ sudo apt-get install libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
$ sudo apt-get install python-dev
$ cd pyaudio
$ sudo python setup.py install

Once those two libraries are installed, you will then need to disable the onboard audio driver on the Raspberry Pi, as it can interfere with PyAudio. To do this, start by opening a new terminal and run the following commands:

$ cd /etc/modprobe.d
$ sudo nano alsa-blacklist.conf

Nano is a simple text editor for the terminal, and when it loads, you only need to enter one line:

blacklist snd_bcm2835

Press Ctrl + X to quit nano and save the file with the file name alsa-blacklist.conf. This simple file disables the Raspberry Pi’s Broadcom audio system so the only audio system available on the Pi is the USB sound card.

How the Voice-Controlled Fridge List Works

The Python script starts by importing the speech recognition module, which is used to convert spoken words into a string. When the module has been imported, we create an object, r, which is a speech recognizer object that is used to record audio from the microphone and then request the conversion. After our speech recognition object has been defined we also define our variables including the list of items, command, current item, and an array that holds parsed commands.

import speech_recognition as sr

r = sr.Recognizer()
items = dict()
command = ""
item = ""

With the initial configuration done, the next chunk of code to be executed is the main loop. The first task in the loop is to inform the user to speak by printing the word “Speak” and then create an audio object called “audio”, which holds our microphone stream.

while(1):
    with sr.Microphone() as source:                                            
        print("Speak:")
        audio = r.listen(source)

When the microphone has detected sound and finished recording (the recording stops when the sound level falls below a threshold), it passes the recorded audio to our recognizer object. Upon doing so, r will use Google services to try to convert the audio into a sentence, which is then passed to a variable called speechString. All of this code is done in a try/except block in case the audio was not understood or if the service is unavailable. The received string is also parsed into prasedCommands where the delimiter is space. So if the words “add bacon” are said, the result will be that parsedCommands[0] will be “add” and parsedCommands[1] will be “bacon”.

try:
        speechString =r.recognize_google(audio)
        parsedCommands = speechString.split(" ")
    except sr.UnknownValueError:
        print("Could not understand audio")
    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))
    
if(len(parsedCommands) > 0):  
        command = parsedCommands[0]

    if(len(parsedCommands) > 1):  
        item = parsedCommands[1]

Now that we have our parsed commands and items, we can add them to our items list. However, to keep things clean, we perform several checks that do the following:

  • If the item already exists and add is said then increment the item value
  • If the item does not exist and add is said then add the item to the list
  • If the item already exists and remove is said then subtract 1 if the total is greater than 1
  • If the item already exists, remove is said, and there is only item left, remove the item
  • If the item does not exist, ignore the command
if(command == "add"):
        if item in items:
            items[item] = str(int(items[item]) + 1)
        else:
            items[item] = str(1)

        print(item + " added")

    if(command == "remove"):
        if item in items:
            if(int(items[item]) > 1):
                items[item] = str(int(items[item]) - 1)
            else:
                try:
                    items.pop(item, None)
                except:
                    pass
        print(item + " removed")

The last command in this simple script is “display”, which prints the contents of the variable item to the display.

if(command == "display"):
        print(items)

Complete Code

import speech_recognition as sr

r = sr.Recognizer()
items = dict()
command = ""
item = ""


while(1):
    with sr.Microphone() as source:                                            
        print("Speak:")
        audio = r.listen(source)
    try:
        speechString =r.recognize_google(audio)
        parsedCommands = speechString.split(" ")
    except sr.UnknownValueError:
        print("Could not understand audio")
    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))

    if(len(parsedCommands) > 0):  
        command = parsedCommands[0]

    if(len(parsedCommands) > 1):  
        item = parsedCommands[1]

    if(command == "add"):
        if item in items:
            items[item] = str(int(items[item]) + 1)
        else:
            items[item] = str(1)

        print(item + " added")

    if(command == "remove"):
        if item in items:
            if(int(items[item]) > 1):
                items[item] = str(int(items[item]) - 1)
            else:
                try:
                    items.pop(item, None)
                except:
                    pass
        print(item + " removed")
        
    if(command == "display"):
        print(items)
    
    command = ""
    item = ""
    days = ""
    parsedCommands.clear()

Construction

This project is centered around the Raspberry Pi and requires no circuitry or hardware besides the microphone and display. While a normal monitor or TV display could be used, it is not very practical for mounting, so in this project, I got a small 3.5-inch Raspberry Pi display that has a resolution of 480x320. While this is too small for normal use, it is perfect for command line work, and if this Pi is programmed and used over a network (using SSH), you can run the Python program from any PC with an internet connection.

Author

Avatar
Robin Mitchell

Graduated from the University Of Warwick in Electronics with a BEng 2:1 and currently runs MitchElectronics.

Related Content

Comments


You May Also Like