Why India is not suitable for self driving cars.

VOICE RECOGNITION IN PYTHON

Bored of using mouse and keyboard ? Lets use our voice as input and put these input devices to rest. I was thinking of a way where i can provide my voice as input and finally got this. PYTHON, the language which is more commonly used for automation process plays a main role in this too. Python code along with installation of few modules help you provide input to your computer through voice. 

PYTHON

 Python can be learnt with ease but execution is a bit difficult. The errors which you usually get in java and c++ are totally different from python errors. A small space in the code can lead to error , i felt so bad for getting a few errors which made me think so deeply but was solved with a space between the code. But at last i succeeded in creating something which can control the system through my voice.

After completing the coding process you can initiate the processing of voice control through the python IDLE (the run button).

INSTALLATION

Install python idle "version 2" because the code provided below is compatible only with the second version . After installing python you have to install a few modules . These modules will play the back end part in running the code . Providing the voice and listening to your voice will be done only after installation of modules.
You have to install modules through the command prompt after downloading them. The download location doesn't matter, the thing which you should take care of, is the location where you install the modules."YOU HAVE TO INSTALL THE MODULES THROUGH COMMAND PROMPT IN THE SCRIPTS FOLDER OF PYTHON" . Enter the scripts folder of python - right click and enter command prompt - enter "pip install (module name)". If you are installing pyaudio module then you have to enter "pip install pyaudio". Note that , this can be done only after downloading pyaudio in your system.

MODULES

1) Speech recognition for python
2) Pygame
3)Pyaudio
4)Pyttsx ( this will be annoying so better enter into the command prompt of the scripts folder and install google text to speech directly) "pip install gtts"
 Google text to speech can provide  up to 50 conversations per day.

Download  these modules based on the version of your system (32 or 64 bit) and the version of python you have downloaded.
After downloading these modules enter into the scripts folder of python and enter into command prompt. After entering into command prompt of scripts folder enter pip install pypiwin32 . This installs a separate module. Followed by that, install all the above modules in the same way .

Now your developing area is ready to work as a speech recognition process.

CODE


import pyttsx
import speech_recognition as sr
import gtts
import pyaudio
import random,os

engine = pyttsx.init()
def listen():
    r=sr.Recognizer()
    with sr.Microphone() as source:
         a = r.listen(source)

         try:
                    return r.recognize_google(a)
                    print (r)
         except sr.UnknownValueError:
                print('could not understand audio')

         except sr.RequestError as e:
                print("Recog Error: (0)".format(e))
         return ""

    
def online():
    speak('yes')
   
def online1():
    speak('hi...i am soft....... your personnel assistant....')
    
def media():
    f=random.choice(['that will be good for this time!!!!','thats a good choice!!!!','good choice!!!!!'])
    speak(f)                               
    speak('connecting to player')                 
    os.system('starting wmplayer.exe')
    speak('which song do you want to play')
    os.startfile('C:/Users/AAKASH/Music/'+listen()+'.mp3')
    speak('playing'+listen()+'for you')
def shutdown():
    speak('ok')
    speak('connecting to command prompt')
    speak('shuttting down')
    os.system('shutdown -s')
def restart():
    speak('ok')
    speak('closing all applications')
    os.system('restart -r')
def gooffline():
      speak('ok')
      speak('closing all systems')
      quit()           

def speak(text):
    engine.say(text)
    engine.runAndWait()
def getonline():
    speak('ok')
    os.system('start C:/Rainmeter.exe') 
    speak('starting systems')
    speak('i am online')

def mainfunction():
      a = r.listen(source)
      user = r.recognize_google(a)
      print(user)
      
      if user == "soft":
          online()
      elif user == "introduce yourself":
          online1()
      elif user in ['music','play some music','play some melody','play a song','i want some music','song']:
            media()
      elif user in ['hey SOFT','wake up soft','wake up','oyee']:
            getonline()
      elif user == "down":
             gooffline()
      elif user in ['shutdown','see you tomorrow','bye soft','bye','ok bye','see you tomorrow soft','ok soft see you tomorrow','see you after some time','see you later','have some sleep soft']:
             shutdown()
      elif user in ['restart the system']:
            restart()
      elif user in ['hi','i','hey','hello']:
          d = random.choice(['hey','hi','hello'])
          speak(d)
speak('hi.......what can i do for you? .......!!')

if __name__ == "__main__":
    r = sr.Recognizer()
    with sr.Microphone() as source:
        while 1:

         mainfunction()


EXPLANATION

 speak(d)
speak('hi.......what can i do for you? .......!!') - This line of code initiates automatically when the coding is made to run. After initializing the code (running the code) the dialog ('hi ...what can  do for you') will be provided by the system.

The first few lines of code are the modules that are installed in the system.

In this code we create a class for the response of the system.

This class will be provided as def (class name)

The command of the user will be provided in the if statement (if user==(command):
 get (the class name)())
Lets take the first command as an example. I have created a class named online , now in the if statement i have provided the command the user provides. I have provided user == "soft" and below that i have given the class name  online(), now when you say soft , the system responds to you with the statement "yes" which is provided in the class online. (In this case "soft" is the name i have given to the system").

This coding can be used in different ways and you can add more classes and more if statements for providing commands to the system.

There are different codes for opening and closing the application, i have provided different code for opening music player. In this way you can modify your code. But before modifying your code, try the above code by installing the modules and get an understanding about the content. After processing the  above code without any error you can try different types of code.




Comments

  1. Hi i used this code but found many errors.can you help me out

    ReplyDelete
    Replies
    1. Hi keerthi, sorry for the late response , can we know what are the errors you got. If you have to send a screenshot , please send it to "support@asquareupload.com".

      Delete
    2. ma'am I'm getting an error as in __name__ not defined. Can u pls help me with that?

      Delete

Post a Comment