Ticker

10/random/ticker-posts

Ad Code

Responsive Advertisement

Create Your Own Voice Assistant Using Python | Code Epic

Create Your Own Voice Assistant Using Python | Code Epic

We all have a dream to create voice assistants like Google Assistant, Alexa, Siri, etc. Python makes this simple and easy to create your voice assistants. Python is the most popular language for machine learning and artificial intelligence and the easiest language to learn and understand.

The first and foremost step is to install python on our PC [click here to downlode]

Then we are going to install and import some libraries to make our voice assistant. Use pip install to install all libraries.

  • speech_recognition -  helps python to record audio through your PC's microphone.
  • pyttsx3 -  this makes python convert text to audio, And it makes our voice assistant talk.
  • wikipedia - as the name say's this helps python to gather information from Wikipedia.
  • datetime - this used to fetch date and time.
  • pyjokes - this used to get one-line jokes on the internet.


Code for voice assistant 


import speech_recognition as sr
import pyttsx3
import datetime
import wikipedia
import pyjokes

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)


def talk(text):
    engine.say(text)
    engine.runAndWait()


def take_command():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            print(command)
    except:
        pass
    return command


def run_voi_ass():
    command = take_command()
    print(command)
    if 'time' in command:
        time = datetime.datetime.now().strftime('%I:%M %p')
        talk('Current time is ' + time)
    elif 'who is' in command:
        person = command.replace('who is', '')
        info = wikipedia.summary(person, 1)
        print(info)
        talk(info)
    elif 'date' in command:
        talk('sorry, I have a headache')
    elif 'are you single' in command:
        talk('I am in a relationship with wifi')
    elif 'joke' in command:
        talk(pyjokes.get_joke())
    else:
        talk('sorry, please say that again')


while True:
    run_alexa()
   



If you like to change voice for your voice assistant, its simple

Create Your Own Voice Assistant Using Python  Code Epic

change 0 for male voice 

    engine.setProperty('voice', voices[0].id)

change 1 for female voice

    engine.setProperty('voice', voices[1].id)

Awesome!! Now you Create Your Own Voice Assistant Using Python.


We just used some libraries in this code, you can add extra libraries like subprocess -  process various system commands or ecapture - capture images and more to add extra features for your voice assistant according to your needs.

Extra libraries :

subprocess

elif "log off" in statement or "sign out" in statement: speak("Ok , your pc will log off in 10 sec") subprocess.call(["shutdown", "/l"])

ecapture

elif "camera" in statement or "take a photo" in statement: ec.capture(0,"robo camera","img.jpg")


have a great day ;)





Post a Comment

0 Comments

Ad Code

Responsive Advertisement