Python programming blog

Wednesday, March 24, 2021

Email voice assitant using python in 10 lines of code

 Hello python programmers, This is AK, In this blog,I'm going to show you how to create a simple voice assistant or a kind of automation.

 

Writing emails to colleagues and clients is a very time-consuming job. I planned to create a simple automation program that helps to send emails to clients without typing a single word on your computer. Instead of typing it can record your voices and convert that audio into a text form and after that, it will send the text into the receiver’s email id


This is an amazing project for everyone. You can definitely learn so many things in this project. 

For video demonstration : Send emails without using keyboard

 

The first process is the installation process, You’ve to install three libraries. The first one is the speech-recognition library provided by google’s API. So open your command prompt and just type pip install speech-recognition so this command is used to install the library on the path.

 

Now the second one is pyaudio. Pyaudio is a supporting library for the speech-recognition library. You can't install this library through pip, for installing the library you need to install another python package manager like pipwin. This is a kind of package manager like pip you need to install this through pip and after installing this package manager you need to use pipwin package manager to install the pyaudio.


The command to install the pyaudio is pipwin install pyaudio. After installing this package you’ve to install another package that is yagmail.


These 3 libraries are must be installed in your machine before performing the script.

 

Commands:

 

pip install speech-recognition

pip install pipwin 

pipwin install pyaudio

pip install yagmail

 


The first process is the importing process you need to import the speech recognition and yagmail.

 

Step:1 Importing the libraries 


import speech_recognition as sr
import yagmail
   




Step2: Initiating the speech_recognition engine

 

Next step we need to create a variable called a recognizer and in this variable, you need to create an instance called sr.Recognizer()

 

   recognizer=sr.Recognizer() 

 

Step3:Clearing & Recording background noises

 

Now we’ve to set the source for recording the real-time audio for this script. So write with sr.Microphone() as source. Now this statement explains that We’re converting our microphone as a source

And you can imagine that there will be a huge noise around you. And that situation this program fails to predict the audio accurately.  To make this script that catches accurate audio we must clear background noises. So it will help to make this script more accurate

For that, you need to write recognizer.adjust_for_ambient_noises( this object takes the parameter your source, You source means microphone that you connected to your computer).

 Next, we’re going to record audio, for that create a variable called as recorded_audio and in this variable, you need to call the object recognizer.listen( and inside this object pass your source).


  with sr.Microphone() as source:
print('Clearing background noise..')
recognizer.adjust_for_ambient_noise(source,duration=1)
print("waiting for your message...")
recordedaudio=recognizer.listen(source)
print('Done recording..!') 
 

When we run this script it can record audio from your microphone.

 

Step 4: Language settings 

 

Now we’re converting the recorded audio to the English language instead of typing we can convert that audio into text format We can save a lot of time when using this script for sending emails.


Now create a try block, and in this block, you need to create a text variable, In this variable, you need to type recognizer.recognize_google(recorded_audio). Here in this code, we’re using google’s API to convert the recorded audio into the text for sending the emails.

 

try:
print('Printing the message..')
text=recognizer.recognize_google(recordedaudio,language='en-US')

print('Your message:{}'.format(text))

except Exception as ex:
print(ex)

 

Step 5: Automate emails 

 

Firstly, create a variable called a receiver, and in this variable type the receiver’s email ID.

And next, you need to create a variable for the message and here you’ve to pass the variable called text.

After that, you need to create a variable for the sender and in this variable, you need to type the sender’s email.


And finally, you have to send the email to the receiver end for that just write this code.


reciever=' Receiver's email id'
message=text
sender=yagmail.SMTP('Sender's email id ')
sender.send(to=reciever,subject='This is an automated mail',contents=message) 

Full source code : Email assistant GitHub

 

Thanks for reading💗 


 
 

No comments:

Post a Comment