Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##required SpeechRecognition, pypiwin32, pyAudio, sendKeys
- ##required espeak windows commandline + python bindings. set espeak.exe in path
- import speech_recognition as sr
- import espeak
- import sys
- import os
- import SendKeys
- import win32com.client
- import time
- import threading
- r = sr.Recognizer()
- srname = "computer"
- for index, name in enumerate(sr.Microphone.list_microphone_names()):
- print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
- dindex = int(raw_input("Select device to use: "))
- def pthrd(tts):
- es = espeak.ESpeak()
- print("tts = %s") %tts
- es.say(tts)
- def talk(tts):
- t = threading.Thread(target=pthrd,args=[tts])
- t.start()
- def savetofile(r, sr, dindex):
- recording = True
- shell = win32com.client.Dispatch("WScript.Shell")
- shell.Run("notepad")
- shell.AppActivate("notepad")
- talk("recording audio in text file")
- while recording:
- with sr.Microphone(device_index=dindex) as source:
- r.adjust_for_ambient_noise(source)
- print("Listening to audio")
- audio = r.listen(source)
- try:
- command = r.recognize_google(audio).lower()
- except sr.UnknownValueError:
- talk("error: could not understand")
- print command
- if command == "stop recording":
- recording = False
- elif command == "new line":
- shell.SendKeys("{~}")
- elif command != "":
- shell.SendKeys(command)
- def active(r, sr, command, dindex):
- print("running command: " + command)
- if command == "record":
- savetofile(r, sr, dindex)
- else:
- responde(command)
- def pather(paths):
- user = os.environ.get('USERNAME')
- default_path = "\"C:\Users\%s\\" %user
- paths = paths.split()
- return full_path
- def responde(command):
- global srname
- print(command)
- if command == "stop":
- talk("stopping")
- sys.exit()
- elif command[:3] == "run":
- talk("running " + command[4:])
- os.system("start " + command[4:])
- elif command[:5] == "close":
- os.system("taskkill /F /IM " + command[6:] + ".exe")
- elif command[:6] == "rename":
- talk("my new name is " +command[7:])
- srname = command[7:]
- elif command[:4] == "open":
- open_file(command[5:])
- else:
- talk("unknown command")
- def open_file(command):
- path = pather(command)
- os.system("start explorer.exe " + path)
- print("opening " + path + location)
- talk("opening " + path + location)
- def transcribe(r, sr, audio):
- global srname
- command = ""
- i = len(srname)
- j = len(srname) + 1
- try:
- command = r.recognize_google(audio).lower()
- except sr.UnknownValueError:
- pass
- print command
- if command[:i] == srname:
- active(r, sr, command[j:], dindex)
- def main(r, sr, dindex):
- global srname
- command = ""
- i = len(srname)
- j = len(srname) + 1
- with sr.Microphone(device_index=dindex) as source:
- r.adjust_for_ambient_noise(source)
- print("Waiting for activation with command " + srname)
- audio = r.listen(source)
- t = threading.Thread(target=transcribe,args=[r, sr, audio])
- t.start()
- t.join()
- if __name__ == "__main__":
- while True:
- try:
- main(r, sr, dindex)
- except KeyboardInterrupt:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement