Voice Recognition with Python

When I saw Tony Stark (Iron Man) in Marvel movies, I always wanted a virtual assistant like Jarvis. Siri, Cortana, and Alexa still don’t have the same level of interaction. Searching the Internet, I found the project https://kripytonianojarvis.com/site/ which I found very interesting.

But I don’t think that’s what I want yet, so I decided to (try) create a virtual assistant. I know it won’t be easy, and this project will be something of a hobby.

The first part is voice communication with the computer, so I need the machine to understand my speech. In this post, I’m going to show code in python to capture speech from the microphone and transform it into text, recording it in a file.

Let’s go to code.

Installation of Libraries

pip install SpeechRecognition
pip install pyaudio

If you are on Windows you will need to use:

pip install pipwin 
pipwin install pyaudio

Although the code doesn’t explicitly call Pyaudio, we must access the microphone. Therefore, we must install it correctly.

Full Code

import speech_recognition as sr


def WriteMessage(message):
    try:
        with open("audio_transcription.txt", "a") as file:
            file.write(str(message) + "\n")
            file.close()
    except:
        print("Error on write " + message)


r = sr.Recognizer()
message = ""

while (message != "turn off"):
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        print("Say something:")
        audio = r.listen(source)
        print("Hello")

    try:
        message = r.recognize_google(audio)
        print("You spoke: " + message)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand message!")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition servic; {0}".format(e))

    WriteMessage(message)

Write Message

import speech_recognition as sr


def WriteMessage(message):
    try:
        with open("audio_transcription.txt", "a") as file:
            file.write(str(message) + "\n")
            file.close()
    except:
        print("Error on write " + message)

This first part of the code has the library import and the transcribed text function. It will be essential to send the text message with commands to the computer and other equipment. Have you thought about turning on the coffeemaker with a voice command? Great!

Voice Recognition

r = sr.Recognizer()
message = ""

while (message != "turn off"):
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        print("Say something:")
        audio = r.listen(source)
        print("Hello")

    try:
        message = r.recognize_google(audio)
        print("You spoke: " + message)
    except sr.UnknownValueError:
        print("Google Speech Recognition could not understand message!")
    except sr.RequestError as e:
        print("Could not request results from Google Speech Recognition servic; {0}".format(e))

Google Speech Recognition performs voice recognition. There are two error handles. The first error refers to the fact that the machine does not understand the message. The second is related to the connection to the Google network. For everything to work correctly, you need an Internet connection.

Google Speech Recognition is a neural network for speech recognition. It is pretty robust as it will most of the time recognize speech without a voice timbre calibration step.

Interesting to note that the program works until you receive, by voice, the command “Turn off.”

Conclusion
We wrote a simple code that captures what was said and displays it in text format on the screen. In addition, everything that we told the machine records in a text file, and the program is closed when we say “Turn off.”

For future work, we will add commands for the computer to perform tasks through voice commands.

One thought on “Voice Recognition with Python

  1. Nice post. I was checking continuously this blog and I am impressed! Very useful info specially the last part 🙂 I care for such information a lot. I was looking for this certain info for a very long time. Thank you and good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *