Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. from tkinter import *
  2. import pyaudio
  3. import wave
  4. import speech_recognition as sr
  5. import os
  6. import time
  7.  
  8. # Global variables
  9. FORMAT = pyaudio.paInt16
  10. CHANNELS = 2
  11. RATE = 44100
  12. CHUNK = 1024
  13. WAVE_OUTPUT_FILENAME = "file.wav"
  14. waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
  15. frames = []
  16. audio = pyaudio.PyAudio()
  17.  
  18. # Creating file for statistics
  19. ctime = time.strftime('%d %B %y %H:%M:%S', time.gmtime())
  20. with open('speech_recogn_stats.txt', 'a+') as myFile:
  21. myFile.write(ctime + '\n')
  22.  
  23. #Functions
  24. def callback(in_data, frame_count, time_info, status):
  25. global frames
  26. frames.append(in_data)
  27. return (in_data, pyaudio.paContinue)
  28.  
  29.  
  30. def record():
  31. global stream
  32. stream = audio.open(format=FORMAT,
  33. channels=CHANNELS,
  34. rate=RATE,
  35. input=True,
  36. stream_callback=callback)
  37. stream.start_stream()
  38. statusLab.config(text="Recording")
  39.  
  40.  
  41. def stop():
  42. stream.stop_stream()
  43. stream.close()
  44. audio.terminate()
  45. statusLab.config(text='Finished recording')
  46.  
  47.  
  48. def createAudioFile():
  49. waveFile.setnchannels(CHANNELS)
  50. waveFile.setsampwidth(audio.get_sample_size(FORMAT))
  51. waveFile.setframerate(RATE)
  52. waveFile.writeframes(b''.join(frames))
  53. waveFile.close()
  54.  
  55.  
  56. def recongnize():
  57. statusLab.config(text='Recognizing the speech')
  58.  
  59. createAudioFile()
  60. r = sr.Recognizer()
  61.  
  62. with sr.AudioFile('file.wav') as source:
  63. audio = r.listen(source)
  64.  
  65. try:
  66. with open('speech_recogn_stats.txt', 'a+') as myFile:
  67. myFile.write("Command: " + commandEnt.get()+'\n')
  68.  
  69. # Google Speech
  70. str_google = r.recognize_google(audio)
  71. myFile.write("Google: " + str_google + '\n')
  72.  
  73. # Houndify
  74. str_hound = r.recognize_houndify(audio, client_id='9_wAj1R52ID30A6QdmQOeg==',
  75. client_key='o12nzb2eS5SggLYu5zuO5c08yUxlDN2vf86eAZtpyFKoe1dVENKGlpQgoMDeaBVNVUZLm2v8EJRuM_7EfMHT1g==')
  76. myFile.write("Houndify: " + str_hound + '\n')
  77.  
  78. # IBM
  79. str_ibm = r.recognize_ibm(audio, username = "ff9dc8ce-0abe-4636-bc89-21529a2744aa", password = "L0B6ZxQMVTRU")
  80. myFile.write("IBM: " + str_ibm + '\n')
  81.  
  82. #Wit.ai
  83. str_wit = r.recognize_wit(audio, key='JONVYPARJNOG76XSJRIWN2BIQHDC2AM3')
  84. myFile.write("WIt.ai: " + str_wit + '\n')
  85.  
  86. except sr.UnknownValueError:
  87. print("Engine did not understand the command")
  88. except sr.RequestError as e:
  89. print("Request Error; {0}".format(e))
  90. os.remove('file.wav')
  91.  
  92. # This is an interface
  93. root = Tk()
  94.  
  95. commandEnt = Entry(root, font=(22))
  96. commandLab = Label(root, font=(20), text="Type your command and press Record")
  97.  
  98. recognBut = Button(root, font=(20), text="Recognize", fg='blue', width=9, command=recongnize)
  99. recordBut = Button(root, font=(20), text="Record", fg='blue', width=9, command=record)
  100. stopBut = Button(root, font=(20), text="Stop", fg='blue', width=9, command=stop)
  101. statusLab = Label(root, font=(20), text="Status", fg="red", )
  102.  
  103. recognBut.grid(row=0, column=0, sticky=W)
  104. recordBut.grid(row=0, column=1, sticky=W+E+N+S)
  105. stopBut.grid(row=0, column=3)
  106. commandEnt.grid(row=3, column=0, columnspan=3, sticky=W)
  107. commandLab.grid(row=2, column=0, columnspan=3)
  108. statusLab.grid(row=4, column=0, sticky=W)
  109.  
  110. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement