Create simple speech recognition program

buddhiashen

Buddhi Ashen

Posted on August 7, 2022

Create simple speech recognition program

if you are an ironman fan you probably know about his two AI systems called 'Jarvis' & 'friday'. tony use speech recognition to give commands to his AI systems.

how to do it in the real world, is it posibble? the answer is yes it is. in here we use python to make a program that recognizes your speech and prints it out as text. let's get to work.

requirements for this project :

  1. Python 3
  2. Python speech recognition module.

now let's code.

to install the python speech recognition model, type this in your terminal

pip install SpeechRecognition
Enter fullscreen mode Exit fullscreen mode

and we use the microphone as our input method. in python to use a microphone we need another module name called "Pyaudio". to install that type this in your terminal.

pip install PyAudio
Enter fullscreen mode Exit fullscreen mode

now all set. we have to write some code to make a speech recognition system.

first of all import all necessary libraries.

import speech_recognition as sr
Enter fullscreen mode Exit fullscreen mode

now initialize the recognizer.

listener = sr.Recognizer()
Enter fullscreen mode Exit fullscreen mode

now we use an error handler in case there is an error. we use to try and except

try:
    with sr.Microphone() as source:
        print('listning...')
        voice = listener.listen(source)
        command = listener.recognize_google(voice)
        print(command)

except:
    pass
Enter fullscreen mode Exit fullscreen mode

Now, let's break down this one by one;

with sr.Microphone() as source:
Enter fullscreen mode Exit fullscreen mode

this means with the speech recognition model (we rename speech recognition as "sr" in the beginning) gets the microphone as a source of input.

print('listning...')
Enter fullscreen mode Exit fullscreen mode

print out the text "listing"

voice = listener.listen(source)
Enter fullscreen mode Exit fullscreen mode

get the user's

voice as input through the microphone

command = listener.recognize_google(voice)
Enter fullscreen mode Exit fullscreen mode

now we pass the user's voice into the google speech recognizer, by the way, we need an internet connection for this project.

print(command)
Enter fullscreen mode Exit fullscreen mode

now print out what the user said

the output will be like this :

PS C:\Users\User\Desktop\python practice\ai> & "C:/Program Files/Python39/python.exe" "c:/Users/User/Desktop/python practice/ai/speech-recognition.py"
listning...
hello
PS C:\Users\User\Desktop\python practice\ai> 
Enter fullscreen mode Exit fullscreen mode

congratulations, now you build your own voice recognition system by using python. if you face any kind of trouble or error please inform me. about that. I like to help you with that.

the complete code :

import speech_recognition as sr

listener = sr.Recognizer()
try:
    with sr.Microphone() as source:
        print('listning...')
        voice = listener.listen(source)
        command = listener.recognize_google(voice)
        print(command)

except:
    pass
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
buddhiashen
Buddhi Ashen

Posted on August 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related