Speech Recognition on a Raspberry Pi 3

Hello there buccaneers!
I know this isn’t really Pimoroni based, but you fellows seem to know plenty about allsorts, so I wonder if you can help me:

I’ve made a quaint little Python script for registering speech and fulfilling actions (a verbal interface, if you will). It works perfectly fine, if a little slowly, but the way I have programmed it to search for the proper action within the commands is a little slow. I was wondering if any of you have had any past experience with speech recognition and have any ideas on how to speed things up?

Here’s a snippet of the code as an example:

def recognise():
command_words = ["shutdown", "time"]

command = speechGoogle()
if command == None:
    main()
else:
    words = command.split()

    for word in words:
        for command_word in command_words:
            if word == command_word:
                if word == "shutdown":
                    os.system("mpg321 /home/pi/General\ Python\ Projects/Speech\ Recognition/Sounds/end_beep.mp3 &")
                    exit()
                elif word == "time":
                    now = datetime.datetime.now()
                    speak("The time is currently ")
                    speak(now.hour+1)
                    speak("hundred")
                    speak("and")
                    speak(now.minute)
                    speak("hours.")
                else:
                    main()
            else:
                main()

The full code can be found here: https://github.com/RaspberryPicardBox/SpeechRecognition

Thank you for all your help. :)

You can use if "shutdown" in words: which drops most of your looping and logic;

words = command.split()

if "shutdown" in words:
    os.system(...)
    exit()

if "time" in words:
    now = datetime.datetime.now()
    ....
    return

main()
2 Likes

Ah, much nicer. Why didn’t I think of that?
Thanks once again!