|
| 1 | +# Importing required modules |
| 2 | +import os |
| 3 | +import pyttsx3 |
| 4 | +import speech_recognition as sr |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +# Creating class |
| 9 | +class Gfg: |
| 10 | + |
| 11 | + # Method to take coice commands as input |
| 12 | + def takeCommands(self): |
| 13 | + |
| 14 | + # Using Recognizer and Microphone Method for input voice commands |
| 15 | + r = sr.Recognizer() |
| 16 | + with sr.Microphone() as source: |
| 17 | + print('Listening') |
| 18 | + |
| 19 | + # Number pf seconds of non-speaking audio before |
| 20 | + # a phrase is considered complete |
| 21 | + r.pause_threshold = 0.7 |
| 22 | + audio = r.listen(source) |
| 23 | + |
| 24 | + # Voice input is identified |
| 25 | + try: |
| 26 | + |
| 27 | + # Listening voice commands in indian english |
| 28 | + print("Recognizing") |
| 29 | + Query = r.recognize_google(audio, language='en-in') |
| 30 | + |
| 31 | + # Displaying the voice command |
| 32 | + print("the query is printed='", Query, "'") |
| 33 | + |
| 34 | + except Exception as e: |
| 35 | + |
| 36 | + # Displaying exception |
| 37 | + print(e) |
| 38 | + # Handling exception |
| 39 | + print("Say that again sir") |
| 40 | + return "None" |
| 41 | + return Query |
| 42 | + |
| 43 | + |
| 44 | + # Method for voice output |
| 45 | + def Speak(self, audio): |
| 46 | + |
| 47 | + # Constructor call for pyttsx3.init() |
| 48 | + engine = pyttsx3.init('sapi5') |
| 49 | + |
| 50 | + # Setting voice type and id |
| 51 | + voices = engine.getProperty('voices') |
| 52 | + engine.setProperty('voice', voices[1].id) |
| 53 | + engine.say(audio) |
| 54 | + engine.runAndWait() |
| 55 | + |
| 56 | + |
| 57 | + # Method to self shut down system |
| 58 | + def quitSelf(self): |
| 59 | + self.Speak("do u want to switch off the computer sir") |
| 60 | + |
| 61 | + # Input voice command |
| 62 | + take = self.takeCommands() |
| 63 | + choice = take |
| 64 | + if choice == 'yes': |
| 65 | + |
| 66 | + # Shutting down |
| 67 | + print("Shutting down the computer") |
| 68 | + self.Speak("Shutting the computer") |
| 69 | + os.system("shutdown /s /t 30") |
| 70 | + if choice == 'no': |
| 71 | + |
| 72 | + # Idle |
| 73 | + print("Thank u sir") |
| 74 | + self.Speak("Thank u sir") |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +# Driver code |
| 79 | +if __name__ == '__main__': |
| 80 | + Maam = Gfg() |
| 81 | + Maam.quitSelf() |
0 commit comments