SPEECH RECOGNITION IN 5 STEPS!
Hello there folks..! Are you one of those developers working on Machine Learning, trying to catch up with the world? Are you looking for tips in Machine Learning?


Here's how you can do Speech Recognition in less than half an hour!
Ready to get started? Scroll down to speed up the process!

Before moving forward, let me tell you the environment we're working in..
We're going to use python, so if you don't have python, make sure you install it from- https://www.python.org/downloads/
So we are going to use Spyder(Anaconda) for the whole process.
To install Anaconda, visit - https://www.anaconda.com/products/individual
Now without any further due let's get started.
STEP 1: INSTALL THE NECESSARY PACKAGES
There are a few inbuilt packages in Anaconda that make the process easy, which means 30% of the code you need is already there for you!
Here are the commands you need to use to install it in the spyder, use any of the following commands:
conda install -c conda-forge speechrecognitionconda install -c conda-forge/label/cf201901 speechrecognitionconda install -c conda-forge/label/cf202003 speechrecognitionSTEP 2: IMPORT THE PACKAGE
So, the above step should take like 30sec-2min to process. After the installation is done you'll get something like this:
And then the first thing you need to do is, import the speech recognition package -
use import speech_recognition as sr
STEP 3: THE RECOGNIZER
Now, the first thing that runs in your mind might probably be what is the use of the package. So, here's the answer, the package consists of the recognizer class which can automatically recognize the speech from input (voice based) and can return text output.
There are different methods to get the speech input from various APIs for example- recognize_bing(), recognize_google(),recognize_ibm() etc.
r= sr.Recognizer()
STEP 4: INPUT
And now comes the interesting part, the input time..! But to do that we have to first install the python module called PyAudio.
you can install it using- conda install -c anaconda pyaudio
with sr.Microphone() as source:
print("You can speak now...")
message= r.listen(source)
So, let me break down the code for you, the first thing we're doing is we're trying to use our own Microphone as the source and on running the command, the when compiler becomes active, it returns a message saying "You can speak now..." and then when you start speaking, the method r.listen(source) is called and that takes your recording and stores in the variable called message.
STEP 5: THE TRY CATCH BLOCK
We have imported all the necessary packages, given the methods to take the input, now what? The last step is to print the speech.
Here's what you can do:
try:
print("TEXT"+r.recognize_google(audio))
except:
pass
The above code uses recognize_google() method to print the speech input.
FINAL CODE:
import speech_recognition as sr
r= sr.Recognizer()
with sr.Microphone() as source:
print("You can speak now...")
message= r.listen(source)
try:
print("TEXT: "+r.recognize_google(audio))
except:
pass
OUTPUT:
TEXT: Hello Quait
Yayyy! You did it!

To understand speech recognition better , take a look at the picture below:

For more topics like this- follow our blog and make sure you share it with your geeky friends too!
Do follow us on twitter - QuAIT
Instagram Handle- @quai_t
Comments
Post a Comment